Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.unique doesn't give the expected output

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,

like image 985
yusuf Avatar asked Jun 02 '26 10:06

yusuf


1 Answers

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

like image 159
Juan Leni Avatar answered Jun 05 '26 00:06

Juan Leni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!