概述
如果解决了你的问题,点个赞再走嘛٩(๑❛ᴗ❛๑)۶
目录
1.rand(d0,d1,...,dn)
2.randn(d0,d1,...dn)
3.randint(low,high=None,size=None,dtype='l')
4.uniform(low,high=None,size=None,dtype='l')
5.normal(loc=0.0,scale=1.0,size=None)
6.shuffle(x)
1.rand(d0,d1,...,dn)
生成n+1维的[0,1)上均匀分布的随机数
import numpy as np
a = np.random.rand(3)
b = np.random.rand(3, 4)
print(a)
print(b)
结果:
[0.53764895 0.57992525 0.95080756]
[[0.10560294 0.81405897 0.2362602 0.60134521]
[0.46502367 0.756167 0.63984261 0.27547203]
[0.40654433 0.46825262 0.5901501 0.36764949]]
2.randn(d0,d1,...dn)
生成n+1维的标准正态分布(均值为0,方差为1)的随机数
import numpy as np
a = np.random.randn(3)
b = np.random.randn(3, 4)
print(a)
print(b)
结果:
[ 1.00909711 -0.68616487 1.71452377]
[[-0.35903461 1.03645835 0.50309653 0.192538 ]
[ 0.13412644 -1.04574046 0.9179926 -0.97724862]
[-0.35891045 1.17398658 0.1928502 -0.18945865]]
3.randint(low,high=None,size=None,dtype='l')
生成区间[low,high)上size个的随机整数,若不填size则默认为1
import numpy as np
a = np.random.randint(0, 10, size=10)
b = np.random.randint(-5, 5)
print(a)
print(b)
结果:
[3 6 4 2 1 2 0 8 3 6]
-1
4.uniform(low,high=None,size=None,dtype='l')
生成区间[low,high)上均匀分布的随机数
import numpy as np
a = np.random.uniform(0, 10, size=10)
b = np.random.uniform(-5, 5)
print(a)
print(b)
结果:
[7.57778973 6.70388913 7.97374359 7.97675231 3.96311451 2.49652624
6.9862527 5.40859836 1.46221567 0.61838655]
1.102540596182454
5.normal(loc=0.0,scale=1.0,size=None)
生成正态分布随机数,loc是均值,scale是方差,不填默认为标准正态分布
import numpy as np
a = np.random.normal(0, 1, size=10)
b = np.random.normal(-5, 5)
print(a)
print(b)
结果:
[-0.03398742 -0.80711951 0.28590543 -1.30518972 -0.50824908 1.05418397
-1.26035905 0.88178835 -1.34705773 -0.3798696 ]
-14.3333409720805
6.shuffle(x)
随机打乱原先的列表或数组x
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
b = [1, 2, 3, 4, 5, 6, 7, 8]
np.random.shuffle(a)
np.random.shuffle(b)
print(a)
print(b)
结果:
[2 3 6 4 5 7 1 8]
[7, 2, 8, 4, 5, 3, 6, 1]
最后
以上就是高大小白菜为你收集整理的【Python之numpy库】9.np.random 随机数生成模块常见函数1.rand(d0,d1,...,dn)2.randn(d0,d1,...dn)3.randint(low,high=None,size=None,dtype='l')4.uniform(low,high=None,size=None,dtype='l')5.normal(loc=0.0,scale=1.0,size=None)6.shuffle(x)的全部内容,希望文章能够帮你解决【Python之numpy库】9.np.random 随机数生成模块常见函数1.rand(d0,d1,...,dn)2.randn(d0,d1,...dn)3.randint(low,high=None,size=None,dtype='l')4.uniform(low,high=None,size=None,dtype='l')5.normal(loc=0.0,scale=1.0,size=None)6.shuffle(x)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复