Issue
Why in Ipython does:
In [1]: 1E6
Out[1]: 1000000
work but:
In[2]: import numpy as np
rng = np.random.RandomState(42)
In[3]: rng.rand([1E6])
produces: TypeError: 'float' object cannot be interpreted as an integer
however if I type in:
In[4]: rng.rand(1000000)
It works?
Solution
Remember that scientific can also be used to represent tiny numbers:
1E-3
so using a float
here makes sense. If you now check the type of 1E6
you will notice
type(1E6) # <class 'float'>
whereas
type(1000000) # <class 'int'>
And your code complains
TypeError: 'float' object cannot be interpreted as an integer
So if you explicitly cast 1E6
to an int
it will work as expected
rng.rand(int(1E6))
Answered By - Nils Werner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.