sciPy stats.zscore()函数| Python
scipy.stats.zscore(arr, axis=0, ddof=0)函数计算输入数据的相对Z 分数,相对于样本均值和标准差。
其公式:
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.
代码 #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]]