简体   繁体   中英

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM