📜  sciPy stats.zscore()函数| Python

📅  最后修改于: 2022-05-13 01:54:39.170000             🧑  作者: Mango

sciPy stats.zscore()函数| Python

scipy.stats.zscore(arr, axis=0, ddof=0)函数计算输入数据的相对Z 分数,相对于样本均值和标准差。

其公式:

代码 #1:工作

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

输出 :

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


代码 #2:Z 分数

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

输出 :

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