Lower boundary of the output interval. All values generated will be
greater than or equal to low. The default value is 0.
high : float or array_like of floats
Upper boundary of the output interval. All values generated will be
less than high. The default value is 1.0.
因此low和high都可以接收数组作为参数,为了完整性,请参见下面的代码:
代码:
import numpy as np
x, y = 5, 5
a = np.random.randn(x, y)
b = np.random.randn(x, y)
high = np.maximum(a, b)
low = np.minimum(a, b)
c = np.random.uniform(low, high, (x, y))
print((low <= c).all() and (c <= high).all())
a = np.random.randn(2,5)
array([[ 1.56068748, -2.21431346],
[-0.33707115, 0.93420256]])
b = np.random.randn(2,5)
array([[-0.0522846 , 0.11635731],
[-0.57028069, -1.08307492]])
# Create an interleaved array from both a and b
s = np.vstack((a.ravel(),b.ravel()))
array([[ 1.56068748, -2.21431346, -0.33707115, 0.93420256],
[-0.0522846 , 0.11635731, -0.57028069, -1.08307492]])
# Feed it to `np.random.uniform` which takes low and high as inputs
# and reshape it to match input shape
np.random.uniform(*s).reshape(a.shape)
array([[ 0.14467235, -0.79804187],
[-0.41495614, -0.19177284]])
你可以这样做:
其中d=值介于0和1之间的随机数组,其维数与
您可以使用文档中的numpy.random.uniform:
因此
low
和high
都可以接收数组作为参数,为了完整性,请参见下面的代码:代码:
输出
在上面的例子中,注意使用maximum和minimum来构建
high
和low
。最后一行检查c
的所有值是否都在high
和low
之间。如果您感兴趣,您可以在一行中完成这一切:下面是一个使用
numpy
的想法:相关问题 更多 >
编程相关推荐