为什么你用不好Numpy的random函数?
阅读量:次 Authors: 阳哥 DATAANALYSIS
Numpy
阅读量:次 Authors: 阳哥 DATAANALYSIS
Numpy
为什么你用不好Numpy的random函数?
Table of Contents
在python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下。
import numpy as np
numpy.random.rand(d0,d1,…,dn)
np.random.rand(4,2)
array([[ 0.02173903, 0.44376568],
[ 0.25309942, 0.85259262],
[ 0.56465709, 0.95135013],
[ 0.14145746, 0.55389458]])
np.random.rand(4,3,2) # shape: 4*3*2
array([[[ 0.08256277, 0.11408276],
[ 0.11182496, 0.51452019],
[ 0.09731856, 0.18279204]],
[[ 0.74637005, 0.76065562],
[ 0.32060311, 0.69410458],
[ 0.28890543, 0.68532579]],
[[ 0.72110169, 0.52517524],
[ 0.32876607, 0.66632414],
[ 0.45762399, 0.49176764]],
[[ 0.73886671, 0.81877121],
[ 0.03984658, 0.99454548],
[ 0.18205926, 0.99637823]]])
numpy.random.randn(d0,d1,…,dn)
np.random.randn() # 当没有参数时,返回单个数据
-1.1241580894939212
np.random.randn(2,4)
array([[ 0.27795239, -2.57882503, 0.3817649 , 1.42367345],
[-1.16724625, -0.22408299, 0.63006614, -0.41714538]])
np.random.randn(4,3,2)
array([[[ 1.27820764, 0.92479163],
[-0.15151257, 1.3428253 ],
[-1.30948998, 0.15493686]],
[[-1.49645411, -0.27724089],
[ 0.71590275, 0.81377671],
[-0.71833341, 1.61637676]],
[[ 0.52486563, -1.7345101 ],
[ 1.24456943, -0.10902915],
[ 1.27292735, -0.00926068]],
[[ 0.88303 , 0.46116413],
[ 0.13305507, 2.44968809],
[-0.73132153, -0.88586716]]])
标准正态分布介绍
numpy.random.randint(low, high=None, size=None, dtype=’l’)
np.random.randint(1,size=5) # 返回[0,1)之间的整数,所以只有0
array([0, 0, 0, 0, 0])
np.random.randint(1,5) # 返回1个[1,5)时间的随机整数
4
np.random.randint(-5,5,size=(2,2))
array([[ 2, -1],
[ 2, 0]])
numpy.random.random_integers(low, high=None, size=None)
该函数在最新的numpy版本中已被替代,建议使用randint函数
np.random.random_integers(1,size=5)
array([1, 1, 1, 1, 1])
print('-----------random_sample--------------')
print(np.random.random_sample(size=(2,2)))
print('-----------random--------------')
print(np.random.random(size=(2,2)))
print('-----------ranf--------------')
print(np.random.ranf(size=(2,2)))
print('-----------sample--------------')
print(np.random.sample(size=(2,2)))
-----------random_sample--------------
[[ 0.34966859 0.85655008]
[ 0.16045328 0.87908218]]
-----------random--------------
[[ 0.25303772 0.45417512]
[ 0.76053763 0.12454433]]
-----------ranf--------------
[[ 0.0379055 0.51288667]
[ 0.71819639 0.97292903]]
-----------sample--------------
[[ 0.59942807 0.80211491]
[ 0.36233939 0.12607092]]
numpy.random.choice(a, size=None, replace=True, p=None)
np.random.choice(5,3)
array([4, 1, 4])
np.random.choice(5, 3, replace=False)
# 当replace为False时,生成的随机数不能有重复的数值
array([0, 3, 1])
np.random.choice(5,size=(3,2))
array([[1, 0],
[4, 2],
[3, 3]])
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
np.random.choice(demo_list,size=(3,3))
array([['moto', 'iphone', 'xiaomi'],
['lenovo', 'xiaomi', 'xiaomi'],
['xiaomi', 'lenovo', 'iphone']],
dtype='<U7')
demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])
array([['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'sansumg', 'sansumg'],
['sansumg', 'xiaomi', 'iphone']],
dtype='<U7')
np.random.seed(0)
np.random.rand(5)
array([ 0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])
np.random.seed(1676)
np.random.rand(5)
array([ 0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])
对我的文章感兴趣的朋友,可以关注我的微信公众号「Python数据之道」(ID:PyDataLab),接收我的更新通知。