1

Hi so Im fairly new to python and an assignment require me to print elements that are less than a variable from a numpy array.

I made a 20x10 numpy array of random integers between -5 and 50

x = np.random.randint (-5, 50, (20, 10))
x

array([[17, 23, 15, 13, -1, 17, 30, 14,  2,  3],
   [ 8,  0, -5,  3, 10, 10, 48,  6, -1, 34],
   [23, 40, 21,  5, 47, 41, 44, 22, 46, 30],
   [36, 13, 48, 29, 46, 25, 48, 38, 13, 40],
   [18, -4,  1, 37, 48, 43, 25, 11, 21, 30],
   [44, 37,  4, 39,  8,  1, 33, 34,  3,  8],
   [ 2, 11, 17, 10, 20,  3, 30,  1, 12,  2],
   [15, 20, -3, 11, 45, 40, 18, 19, -1, 31],
   [39, 44, 18, 25, 49, 20, 15, 28, 32, 18],
   [22, 24, 28, 46, 48, 46, 17, 49,  2, 36],
   [44,  4, 49, -5, 14, 31, 12, 15, 48, 43],
   [-2, 37, -4, 15, 31, -1, 11, 43, 42,  5],
   [40, 35, 25, 22, 38, 26, 15,  1,  4, 22],
   [42, 30, 14,  7, 13, 44,  5, 29, 28, 38],
   [-2,  7, 31, -4, 44, -5, 34, 19, 31, 30],
   [ 0,  1, -2, 29, 35, 28, 23, -1, 21, 27],
   [40, 46,  4, 48,  0, 28,  2, 25,  3, 49],
   [15,  2, -2, 16, 22, 39, -2, 33, 15,  2],
   [14, 26, -5,  0, 22, 38, 25,  4, 14,  2],
   [16, 32, 23,  3, 38, 41, -5, 35, 46, 33]])

above is the result. Now i want to print the number of elements that are less than 5 in each row.

I managed to do this

print (x[0, :] < 5)

[False False False False  True False False False  True  True]

the result is as shown above but what i wanted was for it to show the number of elements that is less than 5. I wanted for it to give me 3 since there are 3 elements.

Can anyone help me with this? Thank you

2 Answers 2

2

It's possible to use np.sum for arrays of type bool like yours. So, at first I have tried the following:

[np.sum(n<5) for n in x]

This gives me a list [3, 4, 0, 0, 2, 3, 4, 2, 0, 1, 2, 3, 2, 0, 3, 4, 4, 4, 4, 2] which is correct but the bad thing is that you need to avoid list comprehensions in numpy actions. Here is the best way to do this in numpy:

np.sum(x<5, axis=1)

This command makes bool array out of x and then calculates True values for each row along y axis (axis number 1)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much man this is the answer that I am looking for
Hi, if you dont mind me asking again. why does this code that you give np.sum(x<5, axis=1) works perfectly but when i try to do np.sum ((x>=0) and (x<=50), axis=1) it gives me the error?
x<5 is a mask array. It contains Trues and Falses. Such kind of arrays doesn't support and operator unlike bools True and False. Try to use arr1 & arr2 or np.logical_and(arr1, arr2) instead.
1

You can use your boolean mask to index the array and then count the elements. Alternatively, you can use numpy.where(). Similar to your approach, it will give you a boolean mask where a certain condition is met.

For your example:

indices = numpy.where(x < 3)
values_greater_than_3 = x[indices]
count = len(values_greater_than_3)
print(count)

Comments

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.