python数据分析————numpy的(随机数,统计)函数
随机数函数np.random.randint()>>> b= np.random.randint(100,200,(3,4))#生成100-200之间随机数的3行4列数组>>> barray([[134, 183, 138, 181],[132, 107, 194, 159],[154, 170, 130, 177]])np.random.seed(s)>
·
随机数函数
- np.random.randint()
>>> b= np.random.randint(100,200,(3,4)) # 生成100-200之间随机数的3行4列数组
>>> b
array([[134, 183, 138, 181],
[132, 107, 194, 159],
[154, 170, 130, 177]])
- np.random.seed(s)
>>> np.random.randint(10,20,(3,4)) # 随机数种子,s为给定种子值
array([[13, 16, 16, 10],
[19, 18, 14, 17],
[10, 10, 17, 11]])
>>> np.random.seed(5)
>>> np.random.randint(10,20,(3,4))
array([[13, 16, 16, 10],
[19, 18, 14, 17],
[10, 10, 17, 11]])
使用共同的seed值,则随机数数值相同
设置的seed值只能有效一次
- random.rand()
>>> np.random.rand(3,4) # 生成3行4列的数组,随机数为浮点数 范围[0,1),均匀分布
array([[0.08074127, 0.7384403 , 0.44130922, 0.15830987],
[0.87993703, 0.27408646, 0.41423502, 0.29607993],
[0.62878791, 0.57983781, 0.5999292 , 0.26581912]])
>>> np.random.rand(3,3,4) # 生成3维3行4列的数组,随机数为浮点数 范围[0,1),均匀分布
array([[[0.28468588, 0.25358821, 0.32756395, 0.1441643 ],
[0.16561286, 0.96393053, 0.96022672, 0.18841466],
[0.02430656, 0.20455555, 0.69984361, 0.77951459]],
[[0.02293309, 0.57766286, 0.00164217, 0.51547261],
[0.63979518, 0.9856244 , 0.2590976 , 0.80249689],
[0.87048309, 0.92274961, 0.00221421, 0.46948837]],
[[0.98146874, 0.3989448 , 0.81373248, 0.5464565 ],
[0.77085409, 0.48493107, 0.02911156, 0.08652569],
[0.11145381, 0.25124511, 0.96491529, 0.63176605]]])
- random.shuffle()
>>> b= np.random.randint(100,200,(3,4))
>>> b
array([[191, 129, 188, 197],
[192, 179, 170, 135],
[120, 149, 172, 132]])
>>> np.random.shuffle(b) # 数组第一轴行随排列,改变原数组
>>> b
array([[120, 149, 172, 132],
[192, 179, 170, 135],
[191, 129, 188, 197]])
- random.permutation(x)
>>> b
array([[199, 142, 124, 176],
[129, 106, 191, 180],
[117, 118, 177, 156]])
>>> c =np.random.permutation(b) # 数组第一轴随机排列,乱序数组,不改变原数组
>>> c
array([[129, 106, 191, 180],
[199, 142, 124, 176],
[117, 118, 177, 156]])
- random.choice(a[,size,replace,p])
>>> x = np.random.randint(100,200,(8,))
>>> x
array([145, 185, 114, 191, 105, 141, 115, 122])
>>> y=np.random.choice(x,(3,2)) # 从一维数组中以概率p抽取元素,形成size形状的新数组
>>> y
array([[114, 114],
[122, 105],
[122, 191]])
>>> y=np.random.choice(x,(3,2),replace=False) # replace表示是否可以重用元素,默认为False
>>> y
array([[102, 127],
[141, 197],
[129, 141]])
>>> y=np.random.choice(x,(3,2),p=x/np.sum(x))
>>> y
array([[160, 198],
[198, 150],
[172, 160]])
统计函数
求和函数sum
>>> a =np.arange(15).reshape(5,3)
>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
>>> b=np.sum(a,axis=None) #默认求和所有
>>> b
105
数组期望mean
>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
>>> c= np.mean(a,axis=1) #计算数组期望,axis=1时输出第2维度数组的期望
>>> c
array([ 1., 4., 7., 10., 13.])
数组加权平均值
>>> d=np.average(a,axis=None,weights=None)
>>> d
7.0
>>> e=np.std(a,axis=None)
>>> e
4.320493798938574
方差var
>>> f=np.var(a,axis=None)
>>> f
18.666666666666668
最大值max,最小值min
>>> x=np.max(a)
>>> y=np.min(a)
>>> x
14
>>> y
0
最小值,最大值降维之后的下标
>>> xx=np.argmax(a)
>>> yy=np.argmin(a)
>>> xx
14
>>> yy
0
一维下标转换多维下标
>>> multiindex = np.unravel_index(1,a.shape)
>>> multiindex
(0, 1)
最大值与最小值的差
>>> cha=np.ptp(a)
>>> cha
14
数组的中位数
>>> mnumber=np.median(a)
>>> mnumber
7.0
更多推荐
所有评论(0)