0

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.

5
  • python3 i presume? try looking at the output of print(M/3) Commented Apr 2, 2018 at 17:19
  • yes it is python3 Commented Apr 2, 2018 at 17:20
  • 1
    integer division in python 3 returns a float. If you want to truncate (as python2 did) you need to use //. Try replacing M/3 with M//3 everywhere. Commented Apr 2, 2018 at 17:21
  • 1
    Even though this is a dupe, and OP should have tried debugging the program with a few print statements, I think the downvotes here are a little harsh. The change in behavior of the / operator is not obvious. Commented Apr 2, 2018 at 17:28
  • @pault It's not the change in behavior of / that's the problem, it's numpy not accepting 30/3 = 10.0 as an integer (even though it is an integer) Commented Mar 6, 2021 at 16:12

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.