NumPy Mean Median mode Statistical function

Numpy

In this article we will learn about NumPy Mean Medain mode statistical function operation on NumPy array. We will learn about sum(), min(), max(), mean(), median(), std(), var(), corrcoef() function. All these functions are provided by NumPy library to do the Statistical Operations.

1. NumPy mean()

Example how to use mean() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('ODI average scores of Players: ')
print(np.mean(player_average))

Output

ODI average scores of Players: 
47.455

2. NumPy median()

Example how to use median() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('Median ODI scores of Players: ')
print(np.median(player_average))

Output

Median ODI scores of Players: 
48.175

# NumPy median() with aixs

import numpy as np

player_average = np.array([[51, 18, 21], [17, 29, 26]])
print('Median ODI scores of Players: ')


print('Numpy median with axis 0:',np.median(player_average,axis = 0))

print('Numpy median with axis 1:',np.median(player_average,axis = 1 ))

Output

Median ODI scores of Players: 
Numpy median with axis 0: [34.  23.5 23.5]
Numpy median with axis 1: [21. 26.]

3. Numpy mode()

Numpy has not any built in function for calculate mode,So we are using scipy library

from scipy import stats
import numpy as np

player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])

result = stats.mode(player_average)
print('mode ODI scores of Players: ',result)


Output

mode ODI scores of Players:  ModeResult(mode=array([23.7]), count=array([1]))

4. NumPy sum()

Example how to use sum() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('SUM of average scores of Players: ')
print(np.sum(player_average))

5. NumPy min()

Example how to use min() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('Minimum of average scores of Players: ')
print(np.min(player_average))

5. NumPy array max()

Example how to use max() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('Maximum of average scores of Players: ')
print(np.max(player_average))

6. NumPy Standard Deviation

Example how to use std() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('Standard Deviation ODI scores of Players: ')
print(np.std(player_average))

7. NumPy Variance

Example how to use var() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('Variance ODI scores of Players: ')
print(np.var(player_average))

#NumPy array Co-relation coefficient

Example how to use corrcoef() function of NumPy array

import numpy as np
player_average = np.array([42.12, 36.28, 51.22, 64.37,
45.13, 23.70, 53.27, 63.55])
print('correlation coefficient ODI scores of Players: ')
print(np.corrcoef(player_average))

Output:

SUM of average scores of Players: 
379.64
Minimum of average scores of Players: 
23.7
Maximum of average scores of Players: 
64.37
ODI average scores of Players: 
47.455
Median ODI scores of Players: 
48.175
Standard Deviation ODI scores of Players: 
12.82675036788352
Variance ODI scores of Players: 
164.52552500000002
correlation coefficient ODI scores of Players: 
1.0

We can read the data from a data file and then perform the operations on that data:

data = np.genfromtxt('**.csv',delimiter=',')
np.mean(arr,axis=0) # Returns mean along specific axis
arr.max(axis=0) # Returns maximum value of specific axis
np.var(arr) # Returns the variance of array
np.std(arr,axis=1) # Returns the standard deviation of specific axis
arr.corrcoef() # Returns correlation coefficient of array