numpy.random.rand() 用法及代码示例
关于:
numpy.random.rand(d0,d1,…,dn): 创建指定形状的数组,并用随机值填充它。
参数:
d0, d1, …, dn : [int, optional]Dimension of the returned array we require,
If no argument is given a single Python float is returned.
返回:
Array of defined shape, filled with random values.
代码1:随机构造一维数组
# 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]]]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。