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 |
import numpy as np # Load NumPy library
from scipy import stats |
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]] |
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]] |
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]] |
print(stats.mode(x, axis = 0)[0]) # Find column-wise mode of array # [[1 3 1 6]]