内心崩溃,刚才写了好久的都没了。/(ㄒoㄒ)/~~
第二遍快速编辑,啊,好烦呐
1.np.random.rand()
#np.random.rand(d0,d1,d2,...,dn) output of the shape
m=np.random.rand(3)
print(m)
n=np.random.rand(2,3)
print(n)
q=np.random.rand(3,2,1)
print(q)
output:
[0.04146033 0.18494041 0.5770587 ]
[[0.99455764 0.48759934 0.75566885]
[0.18905628 0.87811976 0.13206557]]
[[[0.13159995]
[0.07004505]]
[[0.91311543]
[0.87614226]]
[[0.55860032]
[0.65532598]]]
2.np.random.randn()
#np.random.randn(d0,d1,d2,...,dn) shape of the output
#Return a sample (or samples) from the “standard normal” distribution.
#返回均值(mean)为0,方差(variance)为1的高斯分布
m=np.random.randn(3)
print(m)
n=np.random.randn(2,3)
print(n)
q=np.random.randn(3,2,1)
print(q)
output:
[ 1.93660058 1.77966488 -0.77442304]
[[ 1.04153657 0.82183362 -1.17856142]
[ 1.11482213 -0.66228066 -0.14942978]]
[[[-4.52073202e-02]
[ 5.49980033e-04]]
[[ 2.62088523e+00]
[-6.28488648e-01]]
[[ 1.33228915e+00]
[-5.70399605e-01]]]
3.np.random.randint()
#np.random.random_integers()用法和这个函数一毛一样。
#np.random.randint(low,high=None,size=None)
#从[low,hight)的左闭半开区间中取值,size为length/tuple 一个整数,或者[]
m=np.random.randint(1,9,2)
print(m)
n=np.random.randint(2)
print(n)
#从[0,2)中选值
n2=np.random.randint(2,4)
#从[2,4)中选值
n2=np.random.randint(2,[2,3])
#从[0,2)中选值
q=np.random.randint(1,9,[2,3])
print(q)
output:
[4 4]
1
[[4 4 8]
[1 6 2]]
4.np.random.shuffle
#np.random.shuffle(x) x是ndarray的name
x=[[1,7,8,9][2,6,8,0,4]]
m=np.random.shuffle()
print(x)
print(m)
#返回None
output:
[[2, 6, 8, 0, 4], [1, 7, 8, 9, 5]]
5.np.random.permutation()
#np.random.permutation(x) x是ndarray的name
x=[[1,7,8,9,5],[2,6,8,0,4]]
x2=[1,7,8,9,5,2,6,8,0,4]
m=np.random.permutation(x)
n=np.random.permutation(x2)
print(x)
print(m)
print(x2)
print(n)
output:
[[1, 7, 8, 9, 5], [2, 6, 8, 0, 4]]
[[1 7 8 9 5]
[2 6 8 0 4]]
[1, 7, 8, 9, 5, 2, 6, 8, 0, 4]
[8 7 4 6 8 0 1 5 2 9]
5,6的区别
shuffle()操作直接改变x的值
permutation()不改变x的值,应该是先拷贝再改变,会返回新的ndarray。并且ndarray只改变一个维度,如果是二维,就改变第一个维度。一维就是一维啦。
单纯从改变顺序来讲,推荐shuffle()的使用。
6.np.random.seed()
#np.ranom.seed(num) 该函数的作用是生成随机数并保持这个随机数可以再次生成。其实就是具有记忆功能吧。
e=[1,2,3,4,5]
np.random.seed(10101)
np.random.shuffle(e)
e2=np.random.randn(2,5)
np.random.seed(10101)
e3=np.random.randn(2,5)
e4=np.random.rand(2,5)
print(e)
print(e2)
print(e3)
print(e4)
output:
[5, 4, 3, 2, 1]
[[-0.80798286 -0.715672 -0.05469627 -0.02866989 -0.60651316]
[-0.07166339 -0.90191647 -1.44930689 0.47544392 -0.69504091]]
[[-0.80798286 -0.715672 -0.05469627 -0.02866989 -0.60651316]
[-0.07166339 -0.90191647 -1.44930689 0.47544392 -0.69504091]]
e4,没有被标记,所以每次运行生成的都不一样。
[[0.1631669 0.15944332 0.62425849 0.41839641 0.37485372]
[0.87241615 0.76129331 0.97924951 0.89686177 0.63438622]]
e1,e2在整个大程序运行没结束以前,无论几次运行都是这一组数。如果换一个np.random.seed(0),则生成的与10101不一样,但是他也被固定住了。
np.random.seed(None)有这个用法,这个用法没有限制,不会被记住。其实跟没有差不多。
其他理解还没理解到,暂时只get了这一点。
第二遍finishi。