sciPy stats.zscore() function | Python

scipy.stats.zscore(arr, axis=0, ddof=0) function computes the relative Z-score of the input data, relative to the sample mean and standard deviation.

Its formula:

Parameters :
arr : [array_like] Input array or object for which Z-score is to be calculated.
axis : Axis along which the mean is to be computed. By default axis = 0.
ddof : Degree of freedom correction for Standard Deviation.

Results : Z-score of the input data.

Code #1: Working



filter_none

edit
close

play_arrow

link
brightness_4
code

# stats.zscore() method  
import numpy as np
from scipy import stats
    
arr1 = [[20, 2, 7, 1, 34],
        [50, 12, 12, 34, 4]]
  
arr2 = [[50, 12, 12, 34, 4], 
        [12, 11, 10, 34, 21]]
  
print ("\narr1 : ", arr1)
print ("\narr2 : ", arr2)
  
print ("\nZ-score for arr1 : \n", stats.zscore(arr1))
print ("\nZ-score for arr1 : \n", stats.zscore(arr1, axis = 1))

chevron_right


Output :

arr1 :  [[20, 2, 7, 1, 34], [50, 12, 12, 34, 4]]

arr2 :  [[50, 12, 12, 34, 4], [12, 11, 10, 34, 21]]

Z-score for arr1 : 
 [[-1. -1. -1. -1.  1.]
 [ 1.  1.  1.  1. -1.]]

Z-score for arr1 : 
 [[ 0.57251144 -0.85876716 -0.46118977 -0.93828264  1.68572813]
 [ 1.62005758 -0.61045648 -0.61045648  0.68089376 -1.08003838]]

 
Code #2 : Z-score

filter_none

edit
close

play_arrow

link
brightness_4
code

import numpy as np
from scipy import stats
   
arr2 = [[50, 12, 12, 34, 4], 
        [12, 11, 10, 34, 21]]
  
print ("\nZ-score for arr2 : \n", stats.zscore(arr2, axis = 0))
print ("\nZ-score for arr2 : \n", stats.zscore(arr2, axis = 1))

chevron_right


Output :


Z-score for arr2 : 
 [[ 1.  1.  1. nan -1.]
 [-1. -1. -1. nan  1.]]

Z-score for arr2 : 
 [[ 1.62005758 -0.61045648 -0.61045648  0.68089376 -1.08003838]
 [-0.61601725 -0.72602033 -0.83602341  1.80405051  0.37401047]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.