Find Mode of NumPy Array in Python (2 Examples)

In this Python tutorial you’ll learn how to get the mode of a NumPy array.

Preparing the Examples

import numpy as np                   # Load NumPy library
from scipy import stats
x = np.array([[1, 3, 1, 6],          # Construct example NumPy array
              [5, 2, 5, 6],
              [1, 3, 1, 1]])
print(x)
# [[1 3 1 6]
#  [5 2 5 6]
#  [1 3 1 1]]

Example 1: Calculate Mode of Columns in NumPy Array

print(stats.mode(x, axis = 1)[0])    # Find row-wise mode of array
# [[1]
#  [5]
#  [1]]

Example 2: Calculate Mode of Columns in NumPy Array

print(stats.mode(x, axis = 0)[0])    # Find column-wise mode of array
# [[1 3 1 6]]

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top