这个 努比。随机的兰德() 函数创建指定形状的数组并用随机值填充。 语法:
null
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 : " , array); |
输出:
1D Array filled with random values : [ 0.84503968 0.61570994 0.7619945 0.34994803 0.40113761]
代码2:随机构造二维数组
# Python Program illustrating # numpy.random.rand() method import numpy as geek # 2D Array array = geek.random.rand( 3 , 4 ) print ( "2D Array filled with random values : " , 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 ( "3D Array filled with random values : " , array); |
输出:
3D Array filled with random values : [[[ 0.97942627 0.01068711] [ 0.35749073 0.22484643]] [[ 0.99733022 0.8029555 ] [ 0.44111692 0.90537128]]]
参考资料: https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.rand.html#numpy.random.rand
注: 这些代码不会在online-ID上运行。请在您的系统上运行它们以探索工作环境。 . 本文由 莫希特·古普塔(Mohit Gupta_OMG) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END