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

y = np.amin(x,axis=0)
print(y)

y = np.amin(x,axis=1)
print(y)
11
[11 12 13 14 15]
[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]

35
[31 32 33 34 35]
[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(20201127)
x = np.random.randint(0,20,size=[4,5])
print(x)
print(np.ptp(x))
print(np.ptp(x,axis=0))
print(np.ptp(x,axis=1))
[[15 13  2  2 16]
 [ 5  5 13 10 16]
 [ 1 19  5 12  0]
 [17  8  7 14  2]]
19
[16 14 11 12 16]
[14 11 19 15]
计算分位数
  • 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(20201127)
x = np.random.randint(0,20,size=[4,5])
print(x)

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

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

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

print(np.percentile(x,[25,50],axis=0))

print(np.percentile(x,[20,50],axis=1))
[[15 13  2  2 16]
 [ 5  5 13 10 16]
 [ 1 19  5 12  0]
 [17  8  7 14  2]]
[4.25 9.  ]
[4.25 9.  ]
[4.25 9.  ]
[[ 4.    7.25  4.25  8.    1.5 ]
 [10.   10.5   6.   11.    9.  ]]
[[ 2.   5.   0.8  6. ]
 [13.  10.   5.   8. ]]

均值与方差

计算中位数
  • 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(20201127)
x = np.random.randint(0,20,size=[4,5])
print(x)

print(np.percentile(x,50))
print(np.median(x))

print(np.percentile(x,50,axis=0))
print(np.median(x,axis=0))

print(np.percentile(x,50,axis=1))
print(np.median(x,axis=1))
[[15 13  2  2 16]
 [ 5  5 13 10 16]
 [ 1 19  5 12  0]
 [17  8  7 14  2]]
9.0
9.0
[10.  10.5  6.  11.   9. ]
[10.  10.5  6.  11.   9. ]
[13. 10.  5.  8.]
[13. 10.  5.  8.]
计算平均值
  • 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)

y = np.mean(x,axis=0)
print(y)

y = np.mean(x, axis=1)
print(y)
23.0
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
计算加权平均值
  • numpy.average(a[, axis=None, weights=None, returned=False])

Compute the weighted average along the specified axis.

mean 和average 都是计算均值的函数,在不指定权重的时候average 和mean 是一样的。指定权重后, 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)
print(np.mean(x))

y = np.average(x,axis=0)
print(y)
y = np.average(x,axis=1)
print(y)

y = np.arange(1,26).reshape([5,5])
print(y)

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

z = np.average(x,axis=0,weights=y)
print(z)

z = np.average(x,axis=1,weights=y)
print(z)
23.0
23.0
[21. 22. 23. 24. 25.]
[13. 18. 23. 28. 33.]
[[ 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]]
27.0
[25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]
[13.66666667 18.25       23.15384615 28.11111111 33.08695652]
[13.66666667 18.25       23.15384615 28.11111111 33.08695652]
计算方差
  • numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])

Computethe variance along the specified axis.

  • ddof=0:是“Delta Degrees of Freedom”,表示自由度的个数。

要注意方差和样本方差的无偏估计,方差公式中分母上是n ;样本方差无偏估计公式中分母上是n‐1 ( n 为样本个数)。

# 【例】计算方差
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)
y = np.mean((x - np.mean(x)) ** 2)
print(y)

y = np.var(x,ddof=1)
print(y)

y = np.var(x,axis=0)
print(y)
y = np.sum((x-np.mean(x))**2) / (x.size - 1)
print(y)

y = np.var(x,axis=0)
print(y)

y = np.var(x,axis=1)
print(y)
52.0
52.0
54.166666666666664
[50. 50. 50. 50. 50.]
54.166666666666664
[50. 50. 50. 50. 50.]
[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)
y = np.sqrt(np.var(x))
print(y)

y = np.std(x,axis=0)
print(y)

y = np.std(x,axis=1)
print(y)

7.211102550927978
7.211102550927978
[7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]
[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))
print(np.cov(y))
print(np.cov(x,y))

print(np.var(x))
print(np.var(x,ddof=1))
print(np.var(y))
print(np.var(y,ddof=1))

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

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

z = np.dot(x - np.mean(x),y - np.mean(y))/(len(x)-1)
print(z)
3.7
8.5
[[3.7  5.25]
 [5.25 8.5 ]]
2.96
3.7
6.8
8.5
4.2
5.25
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(20201127)
x, y = np.random.randint(0,20,size=[2,4])
print(x)
print(y)

z = np.corrcoef(x,y)
print(z)

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))
[15 13  2  2]
[16  5  5 13]
[[1.        0.2465653]
 [0.2465653 1.       ]]
0.246565302221662
直方图
  • 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)
for n in range(x.size):
    print(bins[inds[n]-1],'<=',x[n],'<',bins[inds[n]])
[1 4 3 2]
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 2 3 4 4]
[1 3 3 4 5]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值