Python中的 numpy.random.randn()
numpy.random.randn()函数创建一个指定形状的数组,并按照标准正态分布用随机值填充它。
如果提供了正参数,则 randn 生成一个形状为 (d0, d1, ..., dn) 的数组,其中填充了从均值 0 和方差 1 的单变量“正态”(高斯)分布(如果任何 d_i 是浮点数,它们首先通过截断转换为整数)。如果未提供参数,则返回从分布中随机采样的单个浮点数。
句法 :
numpy.random.randn(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 floating-point samples from
the standard normal distribution.
代码1:随机构造一维数组
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 1D Array
array = geek.random.randn(5)
print("1D Array filled with random values : \n", array);
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 2D Array
array = geek.random.randn(3, 4)
print("2D Array filled with random values : \n", array);
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 3D Array
array = geek.random.randn(2, 2 ,2)
print("3D Array filled with random values : \n", array);
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 3D Array
array = geek.random.randn(2, 2 ,2)
print("3D Array filled with random values : \n", array);
# Multiplying values with 3
print("\nArray * 3 : \n", array *3)
# Or we cab directly do so by
array = geek.random.randn(2, 2 ,2) * 3 + 2
print("\nArray * 3 + 2 : \n", array);
输出 :
1D Array filled with random values :
[-0.51733692 0.48813676 -0.88147002 1.12901958 0.68026197]
代码2:随机构造二维数组
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 2D Array
array = geek.random.randn(3, 4)
print("2D Array filled with random values : \n", array);
输出 :
2D Array filled with random values :
[[ 1.33262386 -0.88922967 -0.07056098 0.27340112]
[ 1.00664965 -0.68443807 0.43801295 -0.35874714]
[-0.19289416 -0.42746963 -1.80435223 0.02751727]]
代码3:随机构造3D数组
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 3D Array
array = geek.random.randn(2, 2 ,2)
print("3D Array filled with random values : \n", array);
输出 :
3D Array filled with random values :
[[[-0.00416587 -0.66211158]
[-0.97254293 -0.68981333]]
[[-0.18304476 -0.8371425 ]
[ 2.18985366 -0.9740637 ]]]
代码 4:使用随机创建的数组进行操作
Python3
# Python Program illustrating
# numpy.random.randn() method
import numpy as geek
# 3D Array
array = geek.random.randn(2, 2 ,2)
print("3D Array filled with random values : \n", array);
# Multiplying values with 3
print("\nArray * 3 : \n", array *3)
# Or we cab directly do so by
array = geek.random.randn(2, 2 ,2) * 3 + 2
print("\nArray * 3 + 2 : \n", array);
输出 :
3D Array filled with random values :
[[[ 1.9609643 -1.89882763]
[ 0.52252173 0.08159455]]
[[-0.6060213 -0.86759247]
[ 0.53870235 -0.77388125]]]
Array * 3 :
[[[ 5.88289289 -5.69648288]
[ 1.56756519 0.24478366]]
[[-1.81806391 -2.6027774 ]
[ 1.61610704 -2.32164376]]]
Array * 3 + 2 :
[[[-2.73766306 6.80761741]
[-1.57909191 -1.64195796]]
[[ 0.51019498 1.30017345]
[ 3.8107863 -4.07438963]]]