Python numpy.random.rand()
Python numpy.random.rand()函数创建指定形状的数组并用随机值填充它。
语法 :
numpy.random.rand(d0, d1, ..., dn)
参数 :
d0, d1, …, dn : [int, optional]我们需要返回数组的尺寸,如果没有给出参数,则返回单个Python float。
返回 :
定义形状的数组,用随机值填充。
代码1:随机构造1D数组
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 1D Array
array = geek.random.rand(5)
print("1D Array filled with random values : \n", array);
输出 :
1D Array filled with random values :
[ 0.84503968 0.61570994 0.7619945 0.34994803 0.40113761]
代码2:随机构造2D数组
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 2D Array
array = geek.random.rand(3, 4)
print("\n\n2D Array filled with random values : \n", array);
输出 :
2D Array filled with random values :
[[ 0.94739375 0.5557614 0.69812121 0.86902435]
[ 0.94758176 0.22254413 0.21605843 0.44673235]
[ 0.61683839 0.40570269 0.34369248 0.46799524]]
代码3:随机构造3D数组
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 3D Array
array = geek.random.rand(2, 2 ,2)
print("\n\n3D Array filled with random values : \n", array);
输出 :
3D Array filled with random values :
[[[ 0.97942627 0.01068711]
[ 0.35749073 0.22484643]]
[[ 0.99733022 0.8029555 ]
[ 0.44111692 0.90537128]]]