Numpy Any, Explained

In this tutorial, I’ll explain how to use the Numpy any function (AKA, np.any).

I’ll explain the what this function does, how the syntax works, and I’ll show you clear, step-by-step examples of how to use it.

If you need something specific, just click on any of the following links. The link will take you to that specific part of the tutorial.

Table of Contents:

With that said, if you really want to understand how this function works, or if you’re new to Numpy, I recommend that you read the whole tutorial.

A Quick Introduction to Numpy Any

First, let’s start with a quick overview of what the Numpy any function does.

Essentially, the Numpy any function tests a Numpy array and evaluates if any of the elements are True.

Having said that, there is a little nuance to this function and how it works.

Let’s quickly review a few things so you can understand better

A Quick Review of Numpy Arrays

First, let’s quickly review Numpy arrays.

Although you probably know a little about arrays, there are a few important details about Numpy arrays that are relevant to the np.any function.

At a high level, a Numpy array is just a data structure that we use to store data in Python.

Numpy arrays have a grid-line structure with rows and columns.

An example of a Numpy array with 3 rows and 4 columns, containing random integers.

Typically, Numpy arrays contain numeric data. For example, we almost always use Numpy arrays to store and manipulate Python ints and floats.

But in some special instances we can create Numpy arrays that have boolean data. So sometimes we have Numpy arrays with True/False values.

An example of a Numpy array with boolean, True/False values.

It’s important that you understand these details about Numpy array structure and Numpy data types, because (as you’ll see), they influence how the Numpy .all() function works in certain cases.

Numpy Arrays have Axes

Next, let’s quickly review Numpy axes.

Numpy arrays have what we call “axes.”

The best way to think about axes, is that axes are like directions along a Numpy array. Just a Cartesian coordinate space has an x-axis and a y-axis that define directions in the space, a Numpy array also has axes that define directions along the array.

Now 1-dimensional arrays are sort of a special case, in that they only have a single axis … axis-0. For a 1D array, axis-0 points horizontally along the elements.

An image that shows a 1D Numpy array, with an arrow showing how axis-0 points horizontally.

2-dimensional arrays and higher-dimensional arrays are a little different.

For 2D arrays, axis-0 points downward and axis-1 points horizontally.

An example of a 2-dimensional array, with labels indicating axis-0 pointing down, and axis-1 pointing horizontally.

This is important to understand, because we can use the np.any() function to perform logical tests along particular directions. That is, we can test Numpy data directionally along the rows and columns.

I’ll explain this more in the syntax section and show you example in the examples section.

This is relevant, because when we use np.any(), we can use the axis parameter, which will cause the function to operate in one direction or another. I’ll show you how this works in the syntax section and show you examples in the examples section.

The Numpy Any Function Tests if Any Elements Evaluate as True

Now that we’ve reviewed a few important details about Numpy arrays, let’s return to the Numpy any function.

If you use np.any() on an array with boolean values, the function will test if any of the elements are True.

A visual example showing how we can apply np.any to a Numpy array to test if any elements are 'True'.

Additionally, you can pass in a conditional statement that operates on a Numpy array. For example, if you have a Numpy array with numeric data called myarray, you can use a conditional statement like np.any(myarray > 2) to test if any of the values meet that particular condition.

An image showing np.any, testing if any of the numeric elements of an array are greater than 3.

When you get right down to it, the np.any function tests if any of the elements of a Numpy array meet some condition or evaluate as True.

But, the function can change its behavior slightly depending on exactly how we type the syntax.

That being the case, let’s look at the syntax.

The Syntax of Numpy Any

In this section, we’ll look at the syntax of the Numpy any() function.

I’ll explain how the syntax works generally, but I’ll also explain some of the specific parameters.

A quick note

I need to mention one thing before we look at the function itself.

How you import Numpy will affect the exact form of the syntax.

That said, the common convention among Python programmers is to import Numpy with the alias np.

You can do that by running the following code.

import numpy as np

When you import Numpy with the alias np, you’ll be able to call Numpy functions with np as the prefix (which you’ll see in just a moment).

np.any syntax

Ok. Let’s look at the actual syntax.

To call the function, you simply type the name of the function as np.any(). As I just mentioned, this assumes that you’ve imported Numpy with the alias np.

An image that explains the syntax of the np.any function.

Then, inside of the parenthesis, there are some arguments and parameters that enable us to control the exact operation of the function.

The Parameters of np.any

The np.any function has a few parameters and arguments that control its behavior:

  • input_array
  • axis
  • out
  • keepdims

Let’s take a look at each of these individually.

input_array (required)

As I already mentioned, the first argument to the function is the input array that you want to operate on, which here I’ve called input_array (in official documentation, they refer to this as a).

The object that you pass in as this argument can take several forms. You can provide a proper Numpy array, including 1D, 2D, or n-dimensional arrays.

The function will also accept “array-like” objects, such as Python lists or Python tuples.

Either way, be aware that you need to provide something. An input is required.

axis (optional)

The axis parameter enables you to specify a direction along which the function will operate.

Recall that earlier, in the Introduction section, I mentioned that Numpy arrays have axes. Axes are like directions along the Numpy array.

An example of a 2-dimensional array, with labels indicating axis-0 pointing down, and axis-1 pointing horizontally.

By using the axis parameter when we call this function, we can cause it to operate in one of those directions.

Effectively, the axis parameter enables us to use the function along the rows or columns.

So for example, if you have a 2D array and you set axis = 0, the function will operate downward along axis-0. This effectively applies the function down the columns.

And if you have a 2D array and you set axis = 1, the function will operate horizontally along axis-1. This effectively applies the function across the rows.

I’ll show you clear examples of this in the examples section to help you understand.

Additionally, if you want to learn more about Numpy axes, you should read our tutorial that explains Numpy axes.

out (optional)

The out parameter enable you to specify a particular array in which to put the output.

This parameter is somewhat rarely used, so we won’t discuss it much.

keepdims (optional)

When you use the keepdims parameter, you can force the function to “keep” the original number of dimensions in the output as there were in the input array.

Essentially, when you set keepdims = True, the output will have the same number of dimensions as the input.

The output of np.any

By default, the any() returns a logical True or False value that indicates whether any of the values of the input are True or meet the specified condition.

However, there are also some instances where the any() function will output an array of boolean values instead. In particular, the any function is likely to output an array of boolean values when you use the axis parameter. I’ll show you some examples of this, so you can see how and why this happens.

Ok. Having explained the syntax and the parameters, let’s look at some step-by-step examples.

Examples of How to Use Numpy Any

In this section, I’ll show you a few clear examples of how to use the any() function.

If you need something specific, you can click on the appropriate link and it will take you to that example.

Examples:

Run this code first

Before you get started running the examples, there’s one thing you’ll need to do.

As I mentioned previously in the syntax section, in order to use the Numpy functions, you need to import Numpy first.

We typically import Numpy with the alias np, which you can do with the following code:

import numpy as np

Once you do that, you’ll be ready to run these examples.

EXAMPLE 1: Use np.any on a 1-dimensional array

First, we’ll start by applying np.any to a 1-dimensional “array like” object.

Technically, we’re going to use a Python list for the sake of clarity. When I’m showing you an example like this, it’s easier to directly see what’s going on if I use a Python list.

Having said that, the following code will work in almost exactly the same way if we used a proper Numpy array instead of the list.

Ok, let’s take a look.

Here, we’ll call the function with a list of 3 boolean values as the input:

np.any([False, True, True])

OUT:

True
Explanation

So what happened here?

The Numpy any() function evaluates if any of the input elements are True.

In this case, the input list had the values [False, True, True].

Although one of the values was False, the two other values were True.

Remember: this function should return True if any of the inputs are true.

A visual example showing how we can apply np.any to a Numpy array to test if any elements are 'True'.

In this case, since the input has at least one True value, the function returns True as the output.

An alternative version

As I mentioned previously, we could also run this instead with a proper Numpy array instead of a list.

You can try it with this code:

my_boolean_array = np.array([False, True, True])
np.any(my_boolean_array)

EXAMPLE 2: Test an array for a specific condition

Next, let’s use the Numpy any() function to test a specific condition.

To do this, we’re going to use a comparison operation as the input to the function, instead of a simple Numpy array.

I’ll explain why this works in just a second.

Create array

First, we’re going to create a Numpy array with some integer values.

my_1d_array = np.array([1,5,7])

Let’s quickly print it out, so you can see the contents:

print(my_1d_array)

OUT:

[1 5 7]

As you can see, my_1d_array is a 1D Numpy array with three integers.

Run function

Now that we have our array, we’re going to use the np.any() function to check if a comparison operation is true for any elements of the array.

Specifically, we’re going to check if any of the values are greater than 2.

np.any(my_1d_array > 2)

OUT:

True
Explanation

So what happened here and why did this work?

Here, instead of inputting a Numpy array with True or False values, we inputted a comparison operation instead. Specifically, we checked if any of the values in the array are greater than 2 with the input syntax my_1d_array > 2.

The function returned True, and it’s probably obvious that it did so because one of the values is greater than 2 (the values 5 and 7 are both greater than 2).

But technically, why did it work and why is this possible?

Take a look at the input to the function.

The input to the function was a comparison operation: my_1d_array > 2.

my_1d_array is a Numpy array.

When we use a comparison operation on a Numpy array, the output is actually a Numpy array with boolean values.

To see this, run the following code:

my_1d_array > 2

OUT:

array([False,  True,  True])

Do you see that?

When Python runs the code my_1d_array > 2, it produces an array with True or False values.

In this case, that boolean Numpy array actually serves as the input to the function, when we run the code np.any(my_1d_array > 2)!

From there, the Numpy any function checks if any of the values are True. In this case, one of the values is true for the comparison output, so Numpy any() returns True.

EXAMPLE 3: Use np.any on a 2-dimensional array

In this example, we’ll now use np.any() on a 2-dimensional Numpy array.

This is similar to example 2, but here, we’re using a 2D array instead of a 1D array.

Create array

First, we’ll create our 2D array.

To do this, we’ll use the Numpy arange function to create a 1D Numpy array filled with a sequence of numbers.

After using Numpy arange, we’ll use the Numpy reshape method to reshape the 1D array into a 2D array.

my_2d_array = np.arange(start = 1, stop = 7).reshape((2,3))

We can print it out to see the contents:

print(my_2d_array)

OUT:

[[1 2 3]
 [4 5 6]]

So my_2d_array contains the integers from 1 to 6, arranged into a 2D array with 2 rows and 3 columns.

Use Function

Next, let’s use the Numpy any() function to check if a condition applies to any of the values of the array.

np.any(my_2d_array > 2)

OUT:

True
Explanation

Here, the output is True, and if you’ve been following along, it should be obvious why.

As we saw in example 2, we can actually use a comparison operation as the input to the function, instead of just a Numpy array.

When we do that, the Numpy any() function will check if the condition is True for any of the values.

In this case, several of the values of my_2d_array are greater than 2.

To see this, you can run the comparison operation by itself:

my_2d_array > 2

OUT:

array([[False, False,  True],
       [ True,  True,  True]])

So the comparison operation, my_2d_array > 2, creates a Numpy array of True/False values that state if the corresponding of my_2d_array is greater than 2.

This boolean array then serves as the input to the function. The Numpy any() then tests if any of the inputs are True, and returns an output.

In this case, several of the results of the comparison operation were True, so the function ultimately returned True.

EXAMPLE 4: Apply np.any along axis-0

In this example, I’ll show you how to use Numpy any downward along the columns. We’re literally going to test if a condition is true for the values in the columns.

To do this, we’re going to use the axis parameter.

Create array

First, we’ll create a 2D Numpy array.

This array, my_2d_array, has the values from 1 to 6 arranged into a 2-dimensional shape with 2 rows and 3 columns.

(This is the same array that we created in example 3, so if you created it there, you don’t need to create it again.)

my_2d_array = np.arange(start = 1, stop = 7).reshape((2,3))

Let’s print it out in order to look at the contents:

print(my_2d_array)

OUT:

[[1 2 3]
 [4 5 6]]
Use function

Next, we’re going to use a comparison operation, and test of that operation is true for any of the columns of the array.

Specifically, we’re going to test if any of the values are greater than 2, in the axis-0 direction.

np.any(my_2d_array > 2, axis = 0)

OUT:

array([ True,  True,  True])
Explanation

So what happened here?

In this code, the any function tested if a comparison operation was true for any of the columns (i.e., along the axis-0 direction).

Remember: for a 2D array, axis-0 points downward.

An image of a 2D numpy array, with an arrow and a label showing that axis-0 points downward.

That being said, if we use the function with the axis parameter set to axis = 0, the function will test if the comparison operation is true for any of the elements, along that direction.

In effect, using the any() function with axis = 0, is like running the function along each individual column.

EXAMPLE 5: Apply np.any along axis-1

In this final example, we’ll use Numpy any in the axis-1 direction.

Essentially, will apply the function to each of the rows.

Create array

First, we’ll create a 2D Numpy array.

(This is the same array that we created in example 3 and example 4. If you created it already for those examples, you don’t need to create it again.)

my_2d_array = np.arange(start = 1, stop = 7).reshape((2,3))

And we can print it out:

print(my_2d_array)

OUT:

[[1 2 3]
 [4 5 6]]

As you can see, my_2d_array has the integers from 1 to 6 arranged into a 2-dimensional shape.

Use function

Now, we’ll test a comparison operation for the values of the array, but we’ll do this in the axis-1 direction.

This will apply the function to each of the rows, and check if any of the values in each row are greater than 2.

np.any(my_2d_array > 2, axis = 1)

OUT:

array([ True,  True])
Explanation

Here, the function tested the comparison operation in the axis-1 direction.

As I mentioned earlier in the Introduction section, for a 2D array, axis-1 points horizontally.

An image of a 2D numpy array, with an arrow and a label showing that axis-1 points horizontally.

That being the case, when we use Numpy any() with a comparison operation and set axis = 1, the function tests if the comparison operation is True for any of the elements, in the axis-1 direction.

When we do this, it’s like running np.any() on each of the rows of the input.

Leave your other questions in the comments below

Do you have other questions about Numpy any?

If you do, just leave your question in the comments section near the bottom of the page.

Join our course to learn more about Numpy

Do you want to increase your skill with Numpy?

This tutorial will tell you how to use one Numpy function … the Numpy any() function.

But if you really want to master data wrangling with numeric data, there’s a lot more to learn. There are a lot more Numpy functions.

That said, if you’re ready to master Numpy you should join our premium course, Numpy Mastery.

In this course, you’ll learn everything you need to know about Numpy.

  • How to create Numpy arrays
  • What the “Numpy random seed” function does
  • How to use the Numpy random functions
  • How to compute statistics on Numpy arrays, like mean, median and max
  • What Numpy axes are, and how to use them
  • How to reshape, split, and combine your Numpy arrays
  • and much more …

When you join the course, you’ll also get access to our unique practice system.

This system will show you how to practice all of the syntax you learn. And it will ultimately enable you to memorize all of this.

If you take this course and practice like we show you, you’ll be able to write Numpy code fluently, accurately, and 100% from memory.

Find out more here:

Learn More About Numpy Mastery

Leave a Comment