Get Mode of NumPy Array in Python (2 Examples) | SciPy Library | How to Calculate by Column & Row

Published: 04 July 2022
on channel: Statistics Globe
291
6

How to calculate the mode of a NumPy array in the Python programming language. More details: https://statisticsglobe.com/mode-nump...
Python code of this video:

import numpy as np # Load NumPy library

from scipy import stats

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]]

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

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

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com