TypeError:'float'对象不能解释为numpy randon.randn python的整数

时间:2018-04-02 17:18:28

标签: python python-3.x numpy

在此代码中

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) 

我收到此错误

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

我不确定这个问题,因为我觉得我不会把任何漂浮物变成randn。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

>>> 1/2
0.5

>>> 1//2
0