简体   繁体   English

numpy.unique没有给出预期的输出

[英]numpy.unique doesn't give the expected output

I have a numpy array. 我有一个numpy数组。 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; 我得到6278.但是当我使用numpy.amax和numpy.amin打印同一数组的最小和最大int值时;

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? 我得到286和0.是否有可能在0和286之间有6278个唯一值? 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. 你应该在调用unique之前使用astype(int) ,而不是之后。

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): as this(独特应用于int):

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 注意:注意后者as type 之前的应用方式: all_data[:, 3].astype(int)调用唯一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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