Numpy入門教程:10. 統計相關

背景

什麼是 NumPy 呢?

NumPy 這個詞來源於兩個單詞 – NumericalPython。其是一個功能強大的 Python 庫,可以幫助程序員輕鬆地進行數值計算,通常應用於以下場景:

  • 執行各種數學任務,如:數值積分、微分、內插、外推等。因此,當涉及到數學任務時,它形成了一種基於 Python 的 MATLAB 的快速替代。
  • 計算機中的圖像表示爲多維數字數組。NumPy 提供了一些優秀的庫函數來快速處理圖像。例如,鏡像圖像、按特定角度旋轉圖像等。
  • 在編寫機器學習算法時,需要對矩陣進行各種數值計算。如:矩陣乘法、求逆、換位、加法等。NumPy 數組用於存儲訓練數據和機器學習模型的參數。

統計相關

次序統計

  • numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])Return the minimum of an array or minimum along an axis.

【例】計算最小值

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.amin(x)
print(y)  # 11

y = np.amin(x, axis=0)
print(y)  # [11 12 13 14 15]

y = np.amin(x, axis=1)
print(y)  # [11 16 21 26 31]
  • numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])Return the maximum of an array or maximum along an axis.

【例】計算最大值

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.amax(x)
print(y)  # 35

y = np.amax(x, axis=0)
print(y)  # [31 32 33 34 35]

y = np.amax(x, axis=1)
print(y)  # [15 20 25 30 35]
  • numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue) Range of values (maximum - minimum) along an axis. The name of the function comes from the acronym for ‘peak to peak’.

【例】計算極差

import numpy as np

np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10  2  1  1 16]
#  [18 11 10 14 10]
#  [11  1  9 18  8]
#  [16  2  0 15 16]]

print(np.ptp(x))  # 18
print(np.ptp(x, axis=0))  # [ 8 10 10 17  8]
print(np.ptp(x, axis=1))  # [15  8 17 16]
  • numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False) Compute the q-th percentile of the data along the specified axis. Returns the q-th percentile(s) of the array elements.
    • a:array,用來算分位數的對象,可以是多維的數組。
    • q:介於0-100的float,用來計算是幾分位的參數,如四分之一位就是25,如要算兩個位置的數就[25,75]。
    • axis:座標軸的方向,一維的就不用考慮了,多維的就用這個調整計算的維度方向,取值範圍0/1。

【例】計算分位數

import numpy as np

np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10  2  1  1 16]
#  [18 11 10 14 10]
#  [11  1  9 18  8]
#  [16  2  0 15 16]]

print(np.percentile(x, [25, 50]))  
# [ 2. 10.]

print(np.percentile(x, [25, 50], axis=0))
# [[10.75  1.75  0.75 10.75  9.5 ]
#  [13.5   2.    5.   14.5  13.  ]]

print(np.percentile(x, [25, 50], axis=1))
# [[ 1. 10.  8.  2.]
#  [ 2. 11.  9. 15.]]

均值與方差

  • numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False) Compute the median along the specified axis. Returns the median of the array elements.

【例】計算中位數

import numpy as np

np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10  2  1  1 16]
#  [18 11 10 14 10]
#  [11  1  9 18  8]
#  [16  2  0 15 16]]
print(np.percentile(x, 50))
print(np.median(x))
# 10.0

print(np.percentile(x, 50, axis=0))
print(np.median(x, axis=0))
# [13.5  2.   5.  14.5 13. ]

print(np.percentile(x, 50, axis=1))
print(np.median(x, axis=1))
# [ 2. 11.  9. 15.]
  • numpy.mean(a[, axis=None, dtype=None, out=None, keepdims=np._NoValue)])Compute the arithmetic mean along the specified axis.

【例】計算平均值(沿軸的元素的總和除以元素的數量)。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.mean(x)
print(y)  # 23.0

y = np.mean(x, axis=0)
print(y)  # [21. 22. 23. 24. 25.]

y = np.mean(x, axis=1)
print(y)  # [13. 18. 23. 28. 33.]
  • numpy.average(a[, axis=None, weights=None, returned=False])Compute the weighted average along the specified axis.

meanaverage都是計算均值的函數,在不指定權重的時候averagemean是一樣的。指定權重後,average可以計算加權平均值。

【例】計算加權平均值(將各數值乘以相應的權數,然後加總求和得到總體值,再除以總的單位數。)

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.average(x)
print(y)  # 23.0

y = np.average(x, axis=0)
print(y)  # [21. 22. 23. 24. 25.]

y = np.average(x, axis=1)
print(y)  # [13. 18. 23. 28. 33.]


y = np.arange(1, 26).reshape([5, 5])
print(y)
# [[ 1  2  3  4  5]
#  [ 6  7  8  9 10]
#  [11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]]

z = np.average(x, weights=y)
print(z)  # 27.0

z = np.average(x, axis=0, weights=y)
print(z)
# [25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]

z = np.average(x, axis=1, weights=y)
print(z)
# [13.66666667 18.25       23.15384615 28.11111111 33.08695652]
  • numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])Compute the variance along the specified axis.
    • ddof=0:是“Delta Degrees of Freedom”,表示自由度的個數。

要注意方差和樣本方差的無偏估計,方差公式中分母上是n;樣本方差無偏估計公式中分母上是n-1n爲樣本個數)。

【例】計算方差

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.var(x)
print(y)  # 52.0
y = np.mean((x - np.mean(x)) ** 2)
print(y)  # 52.0

y = np.var(x, ddof=1)
print(y)  # 54.166666666666664
y = np.sum((x - np.mean(x)) ** 2) / (x.size - 1)
print(y)  # 54.166666666666664

y = np.var(x, axis=0)
print(y)  # [50. 50. 50. 50. 50.]

y = np.var(x, axis=1)
print(y)  # [2. 2. 2. 2. 2.]
  • numpy.std(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])Compute the standard deviation along the specified axis.

標準差是一組數據平均值分散程度的一種度量,是方差的算術平方根。

【例】計算標準差

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.std(x)
print(y)  # 7.211102550927978
y = np.sqrt(np.var(x))
print(y)  # 7.211102550927978

y = np.std(x, axis=0)
print(y)
# [7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]

y = np.std(x, axis=1)
print(y)
# [1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]

相關

  • numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,aweights=None) Estimate a covariance matrix, given data and weights.

【例】計算協方差矩陣

import numpy as np

x = [1, 2, 3, 4, 6]
y = [0, 2, 5, 6, 7]
print(np.cov(x))  # 3.7
print(np.cov(y))  # 8.5
print(np.cov(x, y))
# [[3.7  5.25]
#  [5.25 8.5 ]]

print(np.var(x))  # 2.96
print(np.var(x, ddof=1))  # 3.7
print(np.var(y))  # 6.8
print(np.var(y, ddof=1))  # 8.5

z = np.mean((x - np.mean(x)) * (y - np.mean(y)))
print(z)  # 4.2

z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1)
print(z)  # 5.25

z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1)
print(z)  # 5.25
  • numpy.corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue) Return Pearson product-moment correlation coefficients.

理解了np.cov()函數之後,很容易理解np.correlate(),二者參數幾乎一模一樣。

np.cov()描述的是兩個向量協同變化的程度,它的取值可能非常大,也可能非常小,這就導致沒法直觀地衡量二者協同變化的程度。相關係數實際上是正則化的協方差,n個變量的相關係數形成一個n維方陣。

【例】計算相關係數

import numpy as np

np.random.seed(20200623)
x, y = np.random.randint(0, 20, size=(2, 4))

print(x)  # [10  2  1  1]
print(y)  # [16 18 11 10]

z = np.corrcoef(x, y)
print(z)
# [[1.         0.48510096]
#  [0.48510096 1.        ]]

a = np.dot(x - np.mean(x), y - np.mean(y))
b = np.sqrt(np.dot(x - np.mean(x), x - np.mean(x)))
c = np.sqrt(np.dot(y - np.mean(y), y - np.mean(y)))
print(a / (b * c))  # 0.4851009629263671

直方圖

  • numpy.digitize(x, bins, right=False)Return the indices of the bins to which each value in input array belongs.
    • x:numpy數組
    • bins:一維單調數組,必須是升序或者降序
    • right:間隔是否包含最右
    • 返回值:x在bins中的位置。

【例】

import numpy as np

x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
inds = np.digitize(x, bins)
print(inds)  # [1 4 3 2]
for n in range(x.size):
    print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])

# 0.0 <= 0.2 < 1.0
# 4.0 <= 6.4 < 10.0
# 2.5 <= 3.0 < 4.0
# 1.0 <= 1.6 < 2.5

【例】

import numpy as np

x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
bins = np.array([0, 5, 10, 15, 20])
inds = np.digitize(x, bins, right=True)
print(inds)  # [1 2 3 4 4]

inds = np.digitize(x, bins, right=False)
print(inds)  # [1 3 3 4 5]

當前活動


我是 終身學習者“老馬”,一個長期踐行“結伴式學習”理念的 中年大叔

我崇尚分享,渴望成長,於2010年創立了“LSGO軟件技術團隊”,並加入了國內著名的開源組織“Datawhale”,也是“Dre@mtech”、“智能機器人研究中心”和“大數據與哲學社會科學實驗室”的一員。

願我們一起學習,一起進步,相互陪伴,共同成長。

後臺回覆「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章