Skip to content
Related Articles

Related Articles

sciPy stats.zscore() function | Python
  • Last Updated : 20 Feb, 2019

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






# 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))

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




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

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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :