NumPy - 统计函数

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

numpy.amin() 和 numpy.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()函数返回沿轴的值的范围(最大值 - 最小值)。

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

输出如下:

我们的数组是:
[[30 40 70]
[80 20 10]
[50 90 60]] 调用 percentile() 函数:
50.0 沿轴 1 调用 percentile() 函数:
[ 40. 20. 60.] 沿轴 0 调用 percentile() 函数:
[ 50. 40. 60.]

numpy.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],通过将相应元素的乘积相加,并将和除以权重的和,来计算加权平均值。

加权平均值 = (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.1180339887498949

示例

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

输出如下:

1.1180339887498949
Python

方差

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

示例

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

输出如下:

1.25
Python
 

NumPy统计函数的更多相关文章

  1. NumPy 统计函数

    NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下: numpy.amin() 和 numpy.amax() numpy.a ...

  2. 14、numpy——统计函数

    NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下:(沿哪条轴执行,就是是最后结果的形式) 1.numpy.amin() 和 ...

  3. NumPy——统计函数

    引入模块import numpy as np 1.numpy.sum(a, axis=None)/a.sum(axis=None) 根据给定轴axis计算数组a相关元素之和,axis整数或元组,不指定 ...

  4. Lesson17——NumPy 统计函数

    NumPy 教程目录 1 NumPy 统计函数 NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. 函数说明如下 1.1 统计 method descripti ...

  5. 吴裕雄--天生自然Numpy库学习笔记:NumPy 统计函数

    NumPy 提供了很多统计函数,用于从数组中查找最小元素,最大元素,百分位标准差和方差等. numpy.amin() 用于计算数组中的元素沿指定轴的最小值. numpy.amax() 用于计算数组中的 ...

  6. 数据分析 大数据之路 四 numpy 2

    NumPy 数学函数 NumPy 提供了标准的三角函数:sin().cos().tan(import numpy as np a = np.array([0,30,45,60,90])print (' ...

  7. numpy学习笔记(三)

    (1)numpy的位操作 序号         操作及描述 1.      bitwise_and 对数组元素执行位与操作 2.      bitwise_or 对数组元素执行位或操作 3.      ...

  8. NumPy教程目录

    NumPy Ndarray对象 NumPy数组属性 NumPy数据类型 NumPy数组创建例程 NumPy来自现有数据的数组 NumPy来自数值范围的数组 NumPy切片和索引 NumPy - 高级索 ...

  9. Python之Numpy详细教程

    NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前 ...

随机推荐

  1. URAL 2040 Palindromes and Super Abilities 2(回文树)

    Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d ...

  2. Fluent Ribbon 第八步 其他控件

    前七节将Ribbon的功能大致介绍了一番,本节来介绍一些特殊控件的使用 DropDownButton控件 当前控件是显示下拉功能的基本组件,其配合Gallery能实现诸多特殊功能,代码如下所示 < ...

  3. node.js开发学习一HelloWorld

    前言:由于公司业务需求,最近启动了node.js的开发任务,想把自己的开发学习历程记录记录下来,可以增加记忆,也方便查找.虽然对javascript有一定的了解,但是刚接触node.js的时候,发现还 ...

  4. 巨蟒python全栈开发数据库前端3:CSS基础2

    1.文本属性 2.背景属性 3.边框属性 4.display属性 5.盒子模型

  5. 网站行为跟踪 Website Activity Tracking Log Aggregation 日志聚合 In comparison to log-centric systems like Scribe or Flume

    网站行为跟踪 Website Activity Tracking 访客信息处理 Log Aggregation   日志聚合 Apache Kafka http://kafka.apache.org/ ...

  6. Linux network 资料链接

    1.iptables 基础 https://wiki.centos.org/HowTos/Network/IPTables 2.HOWTOs on netfilter site http://www. ...

  7. Keras网络层之常用层Core

    常用层 常用层对应于core模块,core内部定义了一系列常用的网络层,包括全连接.激活层等 Dense层 keras.layers.core.Dense(units, activation=None ...

  8. Android—Http连接之GET/POST请求

    在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块.在这个模块中涉及到两个重要的类:HttpGet和HttpPost.    创建步骤:    ...

  9. Angular路由参数传递

    一.路由时传递参数的方式 1.在查询参数中传递数据 //页面 <a routerLink="/product" [queryParams]="{id:1}" ...

  10. python全栈开发从入门到放弃之元组的内置应用

    1.元组的字符类型tuple t=(1,[1,3],'sss',(1,2)) print(type(t)) <class 'tuple'> 2.按索引号取值 t=(1,[1,3],'sss ...