# Mode of NumPy Array in Python (2 Examples)

In this tutorial, Iâ€™ll show how to **calculate the mode of a NumPy array** in the Python programming language.

Table of contents:

Letâ€™s get started:

## Example Data & Libraries

In order to use the functions of the NumPy library, we first have to load NumPy:

import numpy as np # Load NumPy library

Furthermore, we have to import stats from the SciPy library:

from scipy import stats

Now, we can create an example array as shown below:

my_array = np.array([[1, 5, 2, 2, 1, 6], # Create example array [7, 2, 3, 1, 8, 6], [1, 3, 4, 2, 7, 1], [1, 3, 2, 2, 8, 5]]) print(my_array) # Print example array # [[1 5 2 2 1 6] # [7 2 3 1 8 6] # [1 3 4 2 7 1] # [1 3 2 2 8 5]]

Have a look at the previous output of the Python console. It shows that our example array contains 24 values in four rows and six columns.

## Example 1: Mode of Columns in NumPy Array

The following Python programming code illustrates how to calculate the mode of each column in our NumPy array.

For this task, we can apply the mode function as shown in the following Python code:

print(stats.mode(my_array)[0]) # Get mode of array columns # [[1 3 2 2 8 6]]

As you can see, the previous syntax has returned the mode value of each column in our example array.

## Example 2: Mode of Columns in NumPy Array

In Example 2, Iâ€™ll demonstrate how to find the mode of a NumPy array by row.

For this, we have to set the axis argument within the mode function to 1:

print(stats.mode(my_array, axis = 1)[0]) # Get mode of array rows # [[1] # [1] # [1] # [2]]

## Video & Further Resources

Would you like to know more about the calculation of the mode of a NumPy array? Then I recommend having a look at the following video that I have published on my YouTube channel. Iâ€™m illustrating the Python codes of this post in the video:

In addition, you might want to have a look at the other articles which I have published on Statistics Globe:

- Calculate Mode in Python
- Calculate Mode by Group in Python
- Get Median of Array with np.median Function of NumPy Library in Python
- Convert pandas DataFrame to NumPy Array in Python
- Convert pandas DataFrame Index to List & NumPy Array in Python
- Python Programming Tutorials

In this post you have learned how to **find the mode of a NumPy array** in Python. Let me know in the comments below, if you have further questions.