码迷,mamicode.com
首页 > 其他好文 > 详细

numpy 统计函数与随机数

时间:2020-04-08 09:35:43      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:nump   数据   hover   pytho   数组   shuf   传递   除了   numpy   

一、统计函数

可以通过以下的基本统计方法对整个数组或者数组的某个轴的数据进行统计:

方法 说明
sum 求和
mean 算术平均数
std 标准差
var 方差
min 最小值
max 最大值
argmax 最大元素在指定轴上的索引
argmin 最小元素在指定轴上的索引
cumsum 累积的和
cumprod 累积的乘积

示例:

>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.sum()
66
>>> a.sum(axis=1)
array([ 6, 22, 38])
>>> a.mean()
5.5
>>> a.mean(axis=1)
array([1.5, 5.5, 9.5])
>>> a.std()
3.452052529534663
>>> a.var()
11.916666666666666
>>> a.max()
11
>>> a.cumsum()
array([ 0,  1,  3,  6, 10, 15, 21, 28, 36, 45, 55, 66], dtype=int32)
>>> a.cumsum(axis=1)
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]], dtype=int32)
>>> a.cumprod()
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)
>>> a.cumprod(axis=1)
array([[   0,    0,    0,    0],
       [   4,   20,  120,  840],
       [   8,   72,  720, 7920]], dtype=int32)
>>> a.argmin()
0
>>> a.argmax()
11

除了以上的统计方法,还有针对布尔数组的三个重要方法:sum、any和all:

  • sum : 统计数组或数组某一维度中的True的个数
  • any: 统计数组或数组某一维度中是否存在一个/多个True,只要有则返回True,否则返回False
  • all:统计数组或数组某一维度中是否都是True,都是则返回True,否则返回False

实际上,对于普通的数组,以上三个操作也是可以的。对于any和all函数,将非0的数字都看作True,0看作False。

>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> a.any()
True
>>> a.all()
False
>>> b = np.arange(1,3)
>>> b
array([1, 2])
>>> b.all()
True
>>> b.any()
True
>>> x = np.array([[True,False],[True,False]])
>>> x.sum()
2
>>> x.any()
True
>>> x.all()
False
>>> c = np.array([-1,2,3+1j])
>>> c.any()
True
>>> c.all()
True

二、随机数

numpy带有一个random模块,它弥补了Python标准自建random模块的一些不足,效率更高、速度更快、功能更强大。当然这些数都是伪随机数,可以通过seed种子来初始化。下表是numpy.random中的部分函数:

函数 功能
random 返回一个区间[0.0, 1.0)中的随机浮点数
seed 向随机数生成器传递随机状态种子
permutation 返回一个序列的随机排列,或者返回一个乱序的整数范围序列
shuffle 随机排列一个序列
rand 从均匀分布中抽取样本
randint 根据给定的由低到高的范围抽取随机整数
randn 从均值0,方差1的正态分布中抽取样本
binomial 从二项式分布中抽取样本
normal 从正态分布中抽取样本
beta 从beta分布中抽取样本
chisquare 从卡方分布中抽取样本
gamma 从伽马分布中抽取样本
uniform 从均匀[0,1)中抽取样本
>>> import numpy.random as np   # 常用导入方法
>>> np.random()  # 生成1个
0.8898080952787953
>>> np.random(5)   # 生成5个
array([0.32815036, 0.47386   , 0.06808472, 0.3827107 , 0.11855414])
>>> np.random((2,3))   # 生成2行3列
array([[0.89632852, 0.76430853, 0.37540494],
       [0.02581418, 0.90653093, 0.78641778]])
>>> np.seed(1234)  # 设置种子
>>> np.randn()     # 生成1个
-0.008678581361935722
>>> np.randn(3)    # 生成3个
array([-1.60921761, -1.26864685,  0.52483734])
>>> np.randn(2,3)      # 生成2行3列
array([[-0.32106129,  1.05697037, -0.59017955],
       [-0.38786434, -0.04653935, -0.99871643]])
>>> np.randint(4)      # 生成不大于4的整数
2
>>> np.randint(1,10)  # 生成一个1到10之间的整数
1
>>> np.randint(1,10,5) # 生成5个1到10之间的整数
array([1, 4, 3, 4, 2])
>>> np.randint(1,10,(2,3)) # 生成2行3列1到10之间的整数,或者用size参数
array([[2, 6, 8],
       [5, 8, 8]])
>>> np.rand() 
0.4527298092633677
>>> np.rand(3)
array([0.53814784, 0.7906221 , 0.46583634])
>>> np.rand(2,3)
array([[0.76453077, 0.59973081, 0.08094696],
       [0.70454447, 0.16401332, 0.03234935]])
>>> np.normal()
-0.8241256032186633
>>> np.normal(3,4) # 指定正态分布的两个重要参数
7.059397334847745
>>> np.normal(3,4,(2,3)) # 生成2行3列
array([[0.77190185, 1.30157588, 3.54998357],
       [2.71794779, 4.30157729, 1.1059138 ]])

numpy 统计函数与随机数

标签:nump   数据   hover   pytho   数组   shuf   传递   除了   numpy   

原文地址:https://www.cnblogs.com/lavender1221/p/12657826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!