3

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,

asked Mar 1, 2016 at 15:25

1 Answer 1

2

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

answered Mar 1, 2016 at 15:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.