在Python中,numpy.random.randn()函数创建了一个指定形状的数组,并按照标准高斯/正态分布用随机的指定值填充它们。
np.random.randn
np.random.randn()是一个numpy库方法,它从 "标准正态"分布中返回一个(或多个)样本。它将返回数组的尺寸作为参数,并返回ndarray,如果没有提供参数,则返回float值。
np.random.randn()函数以浮点形式返回所有的值,分布的平均值=0,方差=1。
语法
numpy.random.randn(d0, d1, ..., dn)
参数
如果提供了正参数,randn()函数会生成形状为(d0, d1, ..., dn)的数组,其中充满了从均值为0、方差为1的单变量 "正态"(高斯)分布中抽取的随机浮点数。
如果没有提供参数,则返回一个从分布中随机采样的单个浮点数。此外,返回数组的尺寸必须是非负的。如果你提供一个负的参数,那么它将返回一个错误。如果没有提供参数,则返回一个单一的Python float。
例子
# app.py
import numpy as np
data = np.random.randn()
print(data)
输出
➜ pyt python3 app.py
-0.7919353665049774
➜ pyt python3 app.py
0.9218908714949405
➜ pyt python3 app.py
-0.025179948728764872
➜ pyt python3 app.py
0.29764955041572655
➜ pyt python3 app.py
-0.8279168113225552
➜ pyt python3 app.py
-1.5048354875053158
每次运行app.py文件,你都会得到不同的随机值。
使用np random randn()创建一个一维数组
要在Python中创建一个一维数组,可以使用np.random.randn()方法。numpy random randn()方法只取一个维度,并返回一维数组。
让我们创建一个有6个元素的一维数组:
# app.py
import numpy as np
data = np.random.randn(6)
print(data)
输出
python3 app.py
[ 1.08086154 0.70693 0.38091969 -1.64244255 1.13132413 -0.7443323 ]
我们通过6作为参数来创建6个数组的随机项。
请看另一个例子:
# importing numpy
import numpy as np
# Now creating an 1D array of size 10
arr = np.random.randn(10)
print("Values of 1D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))
# Creating of size 5
arr2 = np.random.randn(5)
print("Values of the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))
输出
python3 app.py
Values of 1D array is:
[ 0.456789 0.11413981 -0.06548174 0.54791075 -0.18972466 -0.11922963
1.37909645 -0.0688107 -0.02731399 0.09351504]
Shape of the array is : (10,)
Values of the array is:
[ 0.99990254 -0.07788214 -0.63521035 -1.01484305 -0.2993925 ]
Shape of the array is : (5,)
解释
在这个例子中,我们使用random.randn()函数打印了两个一维数组。在第一种情况下,我们打印了一个形状为10的数组,第二个形状为5的数组。 数组的值是按照上面讨论的规则随机插入的。
使用np random randn()创建一个二维数组
要在Python中创建一个二维数组,使用**np.random.randn()**方法,并传递两个参数,如尺寸,它就会返回二维数组。
语法
使用 random.randn() 函数创建二维数组的语法如下:
np.random.randn(d1, d2)
参数
它需要两个参数:
- d1参数显示我们需要创建多少行的数组
- d2参数显示我们需要创建多少列数组
请看下面的代码:
# app.py
import numpy as np
data = np.random.randn(2, 2)
print(data)
输出
python3 app.py
[[1.38596221 1.59121102]
[0.11743191 0.89372055]]
使用np random randn()创建一个三维数组
要在Python中创建一个三维数组,使用np.random.randn()方法并传递三个参数作为维度,它将返回三维数组。
例子
请看下面的代码:
# app.py
import numpy as np
data = np.random.randn(3, 3, 3)
print(data)
输出
python3 app.py
[[[-1.31932293 -0.55698306 -0.52587777]
[-1.02907293 -0.87960688 0.48399357]
[-0.64534737 -0.40360183 0.90921266]]
[[ 0.94321599 0.67847027 0.70100542]
[-0.52738798 -0.69975292 0.0960497 ]
[-0.3399558 1.54436365 0.26914068]]
[[ 1.98426783 1.27291484 -0.06685548]
[-0.36821547 1.30168745 1.69065317]
[ 1.26130492 2.05068361 0.82860505]]]
它将生成具有正和负随机值的三维数组。
我们不能向randn()函数传递负值,否则会返回ValueError。
# app.py
import numpy as np
data = np.random.randn(-3)
print(data)
输出
python3 app.py
Traceback (most recent call last):
File "app.py", line 3, in <module>
data = np.random.randn(-3)
File "mtrand.pyx", line 1218, in numpy.random.mtrand.RandomState.randn
File "mtrand.pyx", line 1375, in numpy.random.mtrand.RandomState.standard_normal
File "_common.pyx", line 558, in numpy.random._common.cont
ValueError: negative dimensions are not allowed
请看另一个代码例子:
# importing numpy
import numpy as np
# Now creating an 3D array of size 2x2x3
arr = np.random.randn(2, 2, 3)
print("Values of 3D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))
# Creating 2D array of size 5x5
arr2 = np.random.randn(5, 5)
print("Values of the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))
輸出
python3 app.py
Values of 3D array is:
[[[ 1.17736919 1.26366938 -0.06364264]
[ 0.57792361 -0.68398595 0.32957414]]
[[ 0.49199588 0.56612773 0.98434267]
[ 0.30158417 1.20173148 -0.36974876]]]
Shape of the array is : (2, 2, 3)
Values of the array is:
[[ 2.50244931e+00 1.12361971e+00 -2.54657975e-01 2.12150049e-02
-4.34988456e-01]
[-4.84566143e-01 2.24132038e-01 -1.42568814e+00 -2.47381915e-04
3.37966017e-01]
[-1.56456348e+00 -2.03418573e-01 -7.98728742e-01 8.25852255e-01
-1.57770187e-01]
[-8.90851952e-01 9.51316758e-01 -2.90582269e-01 -9.88468496e-01
-4.66474163e-01]
[ 1.86058960e-01 3.19397531e-01 -1.59117225e+00 2.16834898e-01
-4.51887901e-01]]
Shape of the array is : (5, 5)
解释
在这个例子中,我们使用random.randn()函数打印了一个三维数组。在这个例子中,我们打印了一个形状为2x2x3的数组。此外,我们还使用该函数打印了一个二维数组。 数组的值是按照上面讨论的规则随机插入的。
改变一个随机创建的数组。
在这个例子中,首先,我们将用np random randn()函数创建一个二维数组,然后将该数组乘以2,然后在数组中添加2。
# app.py
import numpy as np
array = np.random.randn(2, 2)
print("2D Array filled with random values : \n", array)
# Multiplying values with 2
print("\nArray * 2 : \n", array * 2)
# Or we cab directly do so by
array = np.random.randn(2, 2) * 2 + 2
print("\nArray * 2 + 2 : \n", array)
输出
python3 app.py
2D Array filled with random values :
[[ 0.61033846 -2.17725096]
[-0.45407816 2.04812173]]
Array * 2 :
[[ 1.22067691 -4.35450192]
[-0.90815633 4.09624346]]
Array * 2 + 2 :
[[0.99010675 2.16741196]
[1.99530574 3.23772725]]
使用np.reshape()对数组进行重塑
在有些情况下,我们必须对数组进行重塑。在Python中,numpy.reshape()函数,改变尺寸并返回新的数组。
请看下面的代码:
# app.py
import numpy as np
array = np.random.randn(3, 4)
print(array)
print("After reshape the array")
print(array.reshape(6, 2))
输出
python3 app.py
[[ 0.86247213 -0.36951031 0.74445018 -1.28952837]
[-1.10220821 0.15989654 -0.49336996 1.05084014]
[ 0.84959592 0.7147576 0.83266357 -0.24533966]]
After reshape the array
[[ 0.86247213 -0.36951031]
[ 0.74445018 -1.28952837]
[-1.10220821 0.15989654]
[-0.49336996 1.05084014]
[ 0.84959592 0.7147576 ]
[ 0.83266357 -0.24533966]]
我们的第一个数组是由np random randn()函数创建的,然后我们用numpy reshape()函数来改变数组的尺寸。请记住,这里数组的值没有变化,但尺寸在变化。
我们也可以使用reshape()函数将上述二维数组转换成三维数组。
请看下面的代码:
# app.py
import numpy as np
array = np.random.randn(3, 4)
print(array)
print("After reshape the array")
print(array.reshape(2, 2, 3))
输出
python3 app.py
[[ 2.28291597 -0.53931961 -0.62792401 -0.81161398]
[-0.7488756 0.43830336 0.27803475 2.64883588]
[ 0.4840037 -0.95278401 0.5861628 -1.08436804]]
After reshape the array
[[[ 2.28291597 -0.53931961 -0.62792401]
[-0.81161398 -0.7488756 0.43830336]]
[[ 0.27803475 2.64883588 0.4840037 ]
[-0.95278401 0.5861628 -1.08436804]]]
总结
在本教程中,我们已经看到了如何使用numpy random.randn()方法来创建一维数组、二维数组和三维数组。
使用np.reshape()方法,我们可以改变其尺寸,今天的主题就到此为止。