I have a numpy array. When I print it in this way;
print len(numpy.unique(all_data[:, 3]).astype(int))
I get 6278. But when I print the minimum and maximum int values of the same array with numpy.amax and numpy.amin;
print numpy.amax(numpy.unique(all_data[:, 3]).astype(int))
print numpy.amin(numpy.unique(all_data[:, 3]).astype(int))
I get 286, and 0. Is it possible to have 6278 unique values between 0, and 286? Of course, not!
What should I do to get the number of unique values?
Thanks,
1 Answer 1
You should be using astype(int)
before calling unique, not after.
This is not the same (unique applied to the original data type):
print numpy.amax(numpy.unique(all_data[:, 3]).astype(int))
print numpy.amin(numpy.unique(all_data[:, 3]).astype(int))
as this (unique applied to ints):
print numpy.amax(numpy.unique(all_data[:, 3].astype(int)))
print numpy.amin(numpy.unique(all_data[:, 3].astype(int)))
Note: Pay attention to how in the latter as type
is applied before: all_data[:, 3].astype(int)
calling unique