0

I have already checked this thread How to count values in a certain range in a Numpy array? but their answer does not seem to work.

I have a numpy array of 2000 floats called data:

print(type(data)) --> <type 'list'>
print(type(data[0])) --> <type 'numpy.float64'>

And I have 2 variables to form a range, minV and maxV:

print(type(minV)) --> <type 'float'>
print(type(maxV)) --> <type 'float'>

If I try the solution given in the link mentioned above, I receive this exception:

((minV < data) & (data < maxV)).sum()

AttributeError: 'bool' object has no attribute 'sum'

And indeed, that expression is a boolean:

print(type( (minV < data) & (data < minV) ) ) --> <type 'bool'>
print( ( (minV < data) & (data < minV) ) ) --> True

The python version I am using is Python 2.7.3 -- EPD 7.3-2 (64-bit) Numpy version is 1.6.1

System is Linux (Although I ignore if that is important).

Thanks.

2
  • 2
    You are operating on lists instead of numpy.arrays! Try data = np.array(data) before trying the < & > operations. Commented Aug 9, 2016 at 18:58
  • Yep. That's it. Sorry for my silliness. Commented Aug 9, 2016 at 19:07

2 Answers 2

1

I suspect you are using python 2 because comparing a list and a number doesn't raise a TypeError in your case.

But in order to use element-wise comparison (<, >, &) you need to convert your list to a numpy array:

import numpy as np
data = np.array(data)
((minV < data) & (data < maxV)).sum()

should work. For example:

data = list(range(1000))
minV = 100
maxV = 500
data = np.array(data)
((minV < data) & (data < maxV)).sum() # returns 399
Sign up to request clarification or add additional context in comments.

Comments

0

It seems that data is not an array. Please check it. The suggested solution does work with arrays. Perhaps at some point data became a number or something.

1 Comment

Let's keep opened minds here, yes? He said data is an array. The code print shows a list. Conclusion - data is maybe tampered with and changed type.

Your Answer

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.