Code前端首页关于Code前端联系我们

NumPy 教程:统计函数(查找最小值、最大值、百分比标准差和方差)与示例

terry 2年前 (2023-09-25) 阅读数 50 #后端开发

NumPy - 统计函数

NumPy 有许多有用的统计函数,用于查找数组中给定元素的最小值、最大值、百分比标准差和方差等。函数说明如下:

Kongzi.YaoMing()Kongzi.amax()

这些函数返回沿指定方向的最小值和最大值给定数组中元素的轴。

示例

import numpy as np 
a = np.array([[3,7,5],[8,4,3],[2,4,9]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 amin() 函数:'  
print np.amin(a,1)  
print  '\n'  
print  '再次调用 amin() 函数:'  
print np.amin(a,0)  
print  '\n'  
print  '调用 amax() 函数:'  
print np.amax(a)  
print  '\n'  
print  '再次调用 amax() 函数:'  
print np.amax(a, axis =  0)
Python

输出如下:

我们的数组是:
[[3 7 5]
[8 4 3]
[2 4 9]]

调用 amin() 函数:
[3 3 2]

再次调用 amin() 函数:
[2 4 3]

调用 amax() 函数:
9

再次调用 amax() 函数:
[8 7 9]
Python

numpy.ptp()

numpy.ptp()函数返回al的值范围翁轴(最大值 - 最小值)。

import numpy as np 
a = np.array([[3,7,5],[8,4,3],[2,4,9]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 ptp() 函数:'  
print np.ptp(a)  
print  '\n'  
print  '沿轴 1 调用 ptp() 函数:'  
print np.ptp(a, axis =  1)  
print  '\n'  
print  '沿轴 0 调用 ptp() 函数:'  
print np.ptp(a, axis =  0)
Python

输出如下:

我们的数组是:
[[3 7 5]
[8 4 3]
[2 4 9]]

调用 ptp() 函数:
7

沿轴 1 调用 ptp() 函数:
[4 5 7]

沿轴 0 调用 ptp() 函数:
[6 3 6]

numpy.percentile()

百分位数是统计中使用的一种度量,表示低于该值的一定比例的观测值。函数numpy.percentile()接受以下参数。

numpy.percentile(a, q, axis)
Python

包括:

序列号 参数及说明
1.a 输入数组
2.q 计算的百分比数字,介于 0 ~ 100 之间
3.axis 计算百分位数的轴

示例

import numpy as np 
a = np.array([[30,40,70],[80,20,10],[50,90,60]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 percentile() 函数:'  
print np.percentile(a,50)  
print  '\n'  
print  '沿轴 1 调用 percentile() 函数:'  
print np.percentile(a,50, axis =  1)  
print  '\n'  
print  '沿轴 0 调用 percentile() 函数:'  
print np.percentile(a,50, axis =  0)
python

de 输出如下:

我们的数组是:
[[30 40 70]
 [80 20 10]
 [50 90 60]]

调用 percentile() 函数:
50.0

沿轴 1 调用 percentile() 函数:
[ 40. 20. 60.]

沿轴 0 调用 percentile() 函数:
[ 50. 40. 60.]

confu cius。 median()

中位数定义为将数据样本的上半部分与下半部分分开的值。函数numpy.median()的使用如下程序所示。

示例

import numpy as np 
a = np.array([[30,65,70],[80,95,10],[50,90,60]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 median() 函数:'  
print np.median(a)  
print  '\n'  
print  '沿轴 0 调用 median() 函数:'  
print np.median(a, axis =  0)  
print  '\n'  
print  '沿轴 1 调用 median() 函数:'  
print np.median(a, axis =  1)
Python

输出如下:

我们的数组是:
[[30 65 70]
 [80 95 10]
 [50 90 60]]

调用 median() 函数:
65.0

沿轴 0 调用 median() 函数:
[ 50. 90. 60.]

沿轴 1 调用 median() 函数:
[ 65. 80. 60.]

numpy.mean()

算术平均值是沿轴的元素之和除以元素数量。 numpy.mean()函数返回数组中元素的算术平均值。如果指定了轴,则沿着该轴进行计算。

示例

import numpy as np 
a = np.array([[1,2,3],[3,4,5],[4,5,6]])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 mean() 函数:'  
print np.mean(a)  
print  '\n'  
print  '沿轴 0 调用 mean() 函数:'  
print np.mean(a, axis =  0)  
print  '\n'  
print  '沿轴 1 调用 mean() 函数:'  
print np.mean(a, axis =  1)
Python

输出如下:

我们的数组是:
[[1 2 3]
 [3 4 5]
 [4 5 6]]

调用 mean() 函数:
3.66666666667

沿轴 0 调用 mean() 函数:
[ 2.66666667 3.66666667 4.66666667]

沿轴 1 调用 mean() 函数:
[ 2. 4. 5.]

numpy.average()

加权平均值是每个分量乘以代表其重要性显示的因子得到的平均值。函数 numpy.average() 根据另一个数组中给定的各自权重计算数组中元素的加权平均值。该函数可以接受轴参数。如果未指定轴,则扩展数组。

查看数组[1,2,3,4]及其权重[4,3,2,1],通过将相应元素的乘积相加add,并将总和除以权重之和,计算加权平均值。

加权平均值 = (1*4+2*3+3*2+4*1)/(4+3+2+1)

示例

import numpy as np 
a = np.array([1,2,3,4])  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '调用 average() 函数:'  
print np.average(a)  
print  '\n'  
# 不指定权重时相当于 mean 函数
wts = np.array([4,3,2,1])  
print  '再次调用 average() 函数:'  
print np.average(a,weights = wts)  
print  '\n'  
# 如果 returned 参数设为 true,则返回权重的和  
print  '权重的和:'  
print np.average([1,2,3,  4],weights =  [4,3,2,1], returned =  True)
Python

输出如下:

我们的数组是:
[1 2 3 4]

调用 average() 函数:
2.5

再次调用 average() 函数:
2.0

权重的和:
(2.0, 10.0)

在多维数组中,您可以指定用于计算的轴。

示例

import numpy as np 
a = np.arange(6).reshape(3,2)  
print  '我们的数组是:'  
print a 
print  '\n'  
print  '修改后的数组:' 
wt = np.array([3,5])  
print np.average(a, axis =  1, weights = wt)  
print  '\n'  
print  '修改后的数组:'  
print np.average(a, axis =  1, weights = wt, returned =  True)
Python

输出如下:

我们的数组是:
[[0 1]
 [2 3]
 [4 5]]

修改后的数组:
[ 0.625 2.625 4.625]

修改后的数组:
(array([ 0.625, 2.625, 4.625]), array([ 8., 8., 8.]))

标准差

标准差是与平均值的偏差平方的平均值的平方根。标准差的公式如下:

std = sqrt(mean((x - x.mean())**2))
Python

如果数组为 [1, 2, 3, 4],则平均值为 2.5。因此,差值的平方为 [2.25,0.25,0.25,2.25],平均值除以 4 的平方根,即 sqrt(5/4 )1.118033988749 8949

示例

import numpy as np 
print np.std([1,2,3,4])
Python

输出如下:

1.1180339887498949
Python

方差

方差是偏差平方的平均值,即 平均值((x- x.mean())* *2)。换句话说,标准差是方差的平方根。

示例

import numpy as np 
print np.var([1,2,3,4])
Python

输出如下:

1.25

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门