Reputation: 3973
I want to select a small part of a large array and find the average value of that subset. I have tried to specify integers when defining the subset:
import numpy as np
x = np.linspace(-10,10,1e6) # whole dataset
x0 = x[int(len(x)//2-5):int(len(x)//2+5)] # subset
print(x0)
xm = np.mean(x0) # average value of data subset
print(xm)
but my code gives a deprecation warning that says:
DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
x = np.linspace(-10,10,1e6)
Is there a better way of calculating the average of the data subset? What should I do about this warning and will it become a problem in newer versions of Python? I'm using Spyder 3.2.8.
Upvotes: 2
Views: 5002
Reputation: 4196
The problem is that np.linspace
expect the number of points it should produce as its third argument. Thus, this should be a whole number (an integer). However 1e6
is parsed as a float, hence the need to convert to an integer, hence the warning.
The solution is to write 1e6
as an integer, i.e. 1000000
. If you’re using Python 3, you can write 1_000_000
instead to make the number more readable.
Upvotes: 4