NumPy - 统计函数

NumPy - 统计函数 首页 / Numpy入门教程 / NumPy - 统计函数

NumPy具有许多有用的统计函数,用于从数组中的给定元素中查找最小值,最大值,百分位数标准偏差和方差等。

numpy.amin()和numpy.amax()

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

import numpy as np 
a = np.array([[3,7,5],[8,4,3],[2,4,9]]) 

print 'Our array is:' 
print a  
print '\n'  

print 'Applying amin() function:' 
print np.amin(a,1) 
print '\n'  

print 'Applying amin() function again:' 
print np.amin(a,0) 
print '\n'  

print 'Applying amax() function:' 
print np.amax(a) 
print '\n'  

print 'Applying amax() function again:' 
print np.amax(a, axis = 0)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]

Applying amin() function:
[3 3 2]

Applying amin() function again:
[2 4 3]

Applying amax() function:
9

Applying amax() function again:
[8 7 9]

numpy.ptp()

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

import numpy as np 
a = np.array([[3,7,5],[8,4,3],[2,4,9]]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying ptp() function:' 
print np.ptp(a) 
print '\n'  

print 'Applying ptp() function along axis 1:' 
print np.ptp(a, axis = 1) 
print '\n'   

print 'Applying ptp() function along axis 0:'
print np.ptp(a, axis = 0) 

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]

Applying ptp() function:
7

Applying ptp() function along axis 1:
[4 5 7]

Applying ptp() function along axis 0:
[6 3 6]

numpy.percentile()

百分位数(或百分位数)是统计中使用的一种度量,指示一组观察值中给定的观察值百分比下降到的值,函数numpy.percentile()采用以下参数。

numpy.percentile(a, q, axis)
Sr.No.Argument & 描述
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 'Our array is:' 
print a 
print '\n'  

print 'Applying percentile() function:' 
print np.percentile(a,50) 
print '\n'  

print 'Applying percentile() function along axis 1:' 
print np.percentile(a,50, axis = 1) 
print '\n'  

print 'Applying percentile() function along axis 0:' 
print np.percentile(a,50, axis = 0)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[30 40 70]
 [80 20 10]
 [50 90 60]]

Applying percentile() function:
50.0

Applying percentile() function along axis 1:
[ 40. 20. 60.]

Applying percentile() function along axis 0:
[ 50. 40. 60.]

numpy.median()

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

import numpy as np 
a = np.array([[30,65,70],[80,95,10],[50,90,60]]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying median() function:' 
print np.median(a) 
print '\n'  

print 'Applying median() function along axis 0:' 
print np.median(a, axis = 0) 
print '\n'  
 
print 'Applying median() function along axis 1:' 
print np.median(a, axis = 1)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[30 65 70]
 [80 95 10]
 [50 90 60]]

Applying median() function:
65.0

Applying median() function along axis 0:
[ 50. 90. 60.]

Applying median() function along axis 1:
[ 65. 80. 60.]

numpy.mean()

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

import numpy as np 
a = np.array([[1,2,3],[3,4,5],[4,5,6]]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying mean() function:' 
print np.mean(a) 
print '\n'  

print 'Applying mean() function along axis 0:' 
print np.mean(a, axis = 0) 
print '\n'  

print 'Applying mean() function along axis 1:' 
print np.mean(a, axis = 1)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[1 2 3]
 [3 4 5]
 [4 5 6]]

Applying mean() function:
3.66666666667

Applying mean() function along axis 0:
[ 2.66666667 3.66666667 4.66666667]

Applying mean() function along axis 1:
[ 2. 4. 5.]

numpy.average()

加权平均值是每个分量乘以反映其重要性的因子得出的平均值。

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

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

import numpy as np 
a = np.array([1,2,3,4]) 

print 'Our array is:' 
print a 
print '\n'  

print 'Applying average() function:' 
print np.average(a) 
print '\n'  

# this is same as mean when weight is not specified 
wts = np.array([4,3,2,1]) 

print 'Applying average() function again:' 
print np.average(a,weights = wts) 
print '\n'  

# 如果返回的参数设置为 True,则返回权重的总和。
print 'Sum of weights' 
print np.average([1,2,3, 4],weights = [4,3,2,1], returned = True)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[1 2 3 4]

Applying average() function:
2.5

Applying average() function again:
2.0

Sum of weights
(2.0, 10.0)

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

import numpy as np 
a = np.arange(6).reshape(3,2) 

print 'Our array is:' 
print a 
print '\n'  

print 'Modified array:' 
wt = np.array([3,5]) 
print np.average(a, axis = 1, weights = wt) 
print '\n'  

print 'Modified array:' 
print np.average(a, axis = 1, weights = wt, returned = True)

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

Our array is:
[[0 1]
 [2 3]
 [4 5]]

Modified array:
[ 0.625 2.625 4.625]

Modified array:
(array([ 0.625, 2.625, 4.625]), array([ 8., 8., 8.]))

标准偏差

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

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

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

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

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

1.1180339887498949 

方差

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

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

它将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-statistical-functions.html

来源:LearnFk无涯教程网

1.25

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

趣谈网络协议 -〔刘超〕

即时消息技术剖析与实战 -〔袁武林〕

全栈工程师修炼指南 -〔熊燚(四火)〕

现代C++编程实战 -〔吴咏炜〕

遗留系统现代化实战 -〔姚琪琳〕

Python实战 · 从0到1搭建直播视频平台 -〔Barry〕

结构学习力 -〔李忠秋〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)