Kodeclik Logo

Our Programs

Learn More

Schedule

Kodeclik Blog

numpy.any() in Python

numpy is a popular library for numerical computing in Python. It provides a wide range of functions and tools to work with arrays and matrices efficiently. One of the most useful functions in numpy is numpy.any(). In this blog post, we will explore the numpy.any() function, its syntax, usage, and examples.

The numpy.any() function returns True if any element in the input array a evaluates to True. Otherwise, it returns False. The function can be used with one-dimensional and multi-dimensional arrays.

Here is a very simple usage of numpy.any():

import numpy as np

# Example 1
a = np.array([[False, False, False], 
              [True, False, False], 
              [False, False, False]])
print(np.any(a))

This yields:

True

Let us try it with an array of numbers, like so:

import numpy as np

magic_square = np.array([[8,1,6], [3,5,7], [4,9,2]])

print(np.any(magic_square))

The output is again:

True

This is because the current array elements all evaluate to True and thus any() returns True. The more natural way to use np.any() is as follows:

import numpy as np

magic_square = np.array([[8,1,6], [3,5,7], [4,9,2]])

print(np.any(magic_square>=9))
print(np.any(magic_square>9))
Python numpy.any()

In the last two lines of the above program, we are first filtering the magic_square array to check if a given element passes the specified filter and thus replacing the magic square of numbers with an array of booleans. Then any() applied on that array returns whether any of the given elements are True. As expected the output is:

True
False

The first one returns True because there is at least one element that is greater than or equal to 9 (namely 9). The second line returns False because there are no elements greater than 9.

The numpy.any() function also comes with an optional “axis” parameter. The axis parameter specifies the axis or axes along which to apply the function. If axis=None, the function applies to all elements of the array (this is the default). If axis=0, the function applies to each column, and if axis=1, the function applies to each row.

Thus in the following code:

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.any(a==6, axis=0))

We are checking to see if any element is equal to 6, along the columns. The output is:

[False False  True]

meaning that only the third column has an element equal to 6. If we change the axis setting:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(np.any(a==6, axis=1))

the output changes to:

[False  True False]

because only the second row has an element equal to 6.

There are two additional parameters for numpy.any() - namely the out and the keepdims parameters.

The out parameter specifies the output array. If out=None, the function creates a new array to store the output. If out is specified, the function stores the output in the specified array. The keepdims parameter specifies whether to keep the dimensions of the output array the same as the input array. If keepdims=True, the function returns an array with the same number of dimensions as the input array. Otherwise, the function returns a flattened array.

Thus, in summary the numpy.any() function is a useful tool in Python that allows us to quickly check if any element in an array satisfies a given condition. It is a simple yet powerful function that can be used in a variety of scenarios, making it a must-have tool in any data analyst or scientist's toolkit.

If you liked learning about numpy.any(), learn about numpy.isnan() which is one of the conditions you can use with numpy.any().

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.