scipy.stats.variation(arr, axis = None)
函數計算變異係數。定義為標準偏差與平均值之比。
參數:
arr :[數組]輸入數組。
axis :[int或int元組]軸,我們要沿著該軸計算變異係數。
->軸= 0沿列的變化係數。
->軸= 1沿行工作的變化係數。
結果:沿指定軸具有值的數組變化係數。
代碼1:使用variation()
from scipy.stats import variation
import numpy as np
arr = np.random.randn(5, 5)
print ("array : \n", arr)
# rows: axis = 0, cols: axis = 1
print ("\nVariation at axis = 0: \n", variation(arr, axis = 0))
print ("\nVariation at axis = 1: \n", variation(arr, axis = 1))
輸出:
array : [[-1.16536706 -1.29744691 -0.39964651 2.14909277 -1.00669835] [ 0.79979681 0.91566149 -0.823054 0.9189682 -0.01061181] [ 0.9532622 0.38630077 -0.79026789 -0.70154086 0.79087801] [ 0.53553389 1.46409899 1.89903817 -0.35360202 -0.14597738] [-1.53582875 -0.50077039 -0.23073327 0.32457064 -0.43269088]] Variation at axis = 0: [-12.73042404 5.10272979 -14.6476392 2.15882202 -3.64031032] Variation at axis = 1: [-3.73200773 1.90419038 5.77300406 1.29451485 -1.27228112]
代碼2:如何在沒有variation()的情況下實現
import numpy as np
arr = np.random.randn(5, 5)
print ("array : \n", arr)
# this function works similar to variation()
cv = lambda x: np.std(x) / np.mean(x)
var1 = np.apply_along_axis(cv, axis = 0, arr = arr)
print ("\nVariation at axis = 0: \n", var1)
var2 = np.apply_along_axis(cv, axis = 1, arr = arr)
print ("\nVariation at axis = 0: \n", var2)
輸出:
array : [[ 0.51268414 -1.93697931 0.41573223 2.14911168 0.15036631] [-0.50407207 1.51519879 -0.42217231 -1.09609322 1.93184432] [-1.07727163 0.27195529 -0.1308108 -1.75406388 0.94046395] [ 1.23283059 -0.03112461 0.59725109 0.06671002 -0.97537666] [ 1.1233506 0.97658799 -1.10309113 -1.33142901 -0.28470146]] Variation at axis = 0: [ 3.52845174 7.40891024 -4.74078192 -3.57928544 2.85092056] Variation at axis = 0: [ 5.04874565 4.22763514 -2.74104828 4.10772935 -8.24126977]
相關用法
- Python Scipy stats.sem()用法及代碼示例
- Python Scipy stats.mean()用法及代碼示例
- Python Scipy stats.relfreq()用法及代碼示例
- Python Scipy stats.tvar()用法及代碼示例
- Python Scipy stats.nanstd()用法及代碼示例
- Python Scipy stats.gmean()用法及代碼示例
- Python Scipy stats.trimboth()用法及代碼示例
- Python Scipy stats.bayes_mvs()用法及代碼示例
- Python Scipy stats.scoreatpercentile()用法及代碼示例
- Python Scipy stats.nanmean()用法及代碼示例
- Python Scipy stats.moment()用法及代碼示例
- Python Scipy stats.trim1()用法及代碼示例
- Python Scipy stats.kurtosistest()用法及代碼示例
注:本文由純淨天空篩選整理自vishal3096大神的英文原創作品 sciPy stats.variation() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。