类型错误:'float' 对象不能被解释为整数作为 numpy randon.randn python [英] TypeError: 'float' object cannot be interpreted as an integer as numpy randon.randn python

查看:68
本文介绍了类型错误:'float' 对象不能被解释为整数作为 numpy randon.randn python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中

N = 4米 = 30# Generar datos a partir de 3 cluster different:X1 = 10 + 2*np.random.randn(M/3,N)X2 = -10 + 5*np.random.randn(M/3,N)X3 = 1*np.random.randn(M/3,N)

我收到此错误

回溯(最近一次调用最后一次): 中的文件/home/user/PycharmProjects/tema3/3.18_MDS.py",第 16 行X1 = 10 + 2*int(np.random.randn(M/3,N)) #簇1(分散介质)文件mtrand.pyx",第 1420 行,在 mtrand.RandomState.randn 中文件mtrand.pyx",第 1550 行,在 mtrand.RandomState.standard_normal 中文件mtrand.pyx",第 167 行,在 mtrand.cont0_array 中类型错误:float"对象不能解释为整数

我不确定这个问题,因为我认为我没有将任何浮点数传递给 randn.

解决方案

在 Python3 整数除法中用 / 返回一个浮点数.您需要使用 // 来获取整数(向下取整).

<预><代码>>>>类型(1)<类'int'>>>>类型(1/1)<类'浮动'>>>>类型(1//1)<类'int'>>>>1/20.5>>>1//20

In this code

N = 4
M = 30

# Generar datos a partir de 3 clusters diferentes:

X1 = 10 + 2*np.random.randn(M/3,N)
X2 = -10 + 5*np.random.randn(M/3,N) 
X3 = 1*np.random.randn(M/3,N) 

I get this error

Traceback (most recent call last):
  File "/home/user/PycharmProjects/tema3/3.18_MDS.py", line 16, in <module>
    X1 = 10 + 2*int(np.random.randn(M/3,N)) # cluster 1 (dispersion media)
  File "mtrand.pyx", line 1420, in mtrand.RandomState.randn
  File "mtrand.pyx", line 1550, in mtrand.RandomState.standard_normal
  File "mtrand.pyx", line 167, in mtrand.cont0_array
TypeError: 'float' object cannot be interpreted as an integer

I'm not sure about the problem because I think I'm not pasing any float to randn.

解决方案

In Python3 integer division with / returns a float. You need to use // to get an integer (rounded down).

>>> type(1)
<class 'int'>

>>> type(1/1)
<class 'float'>

>>> type(1//1)
<class 'int'>

>>> 1/2
0.5

>>> 1//2
0

这篇关于类型错误:'float' 对象不能被解释为整数作为 numpy randon.randn python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆