受其他数组限制的 Numpy 随机数组
Numpy random array limited by other arrays
我有两个相同大小的 numpy ndarray。
a = np.random.randn(x,y)
b = np.random.randn(x,y)
我想创建一个新数组,其中每个元素都是 a
和 b
中具有相同索引的元素值之间的随机值。所以每个元素 c[i][j]
应该在 a[i][j]
和 b[i][j]
之间。
除了遍历 c
的所有元素并分配随机值之外,还有什么 quicker/simpler/more 有效的方法吗?
您可以使用 numpy.random.uniform,来自文档:
low : float or array_like of floats, optional
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())
输出
True
在上面的示例中,请注意使用 maximum and minimum 来构建 high
和 low
。最后一行检查确实 c
的所有值都在 high
和 low
之间。如果您对此感兴趣,您可以一行完成所有操作:
c = np.random.uniform(np.minimum(a, b), np.maximum(a, b), (x, y))
这是一个使用 numpy
的想法:
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]])
你可以这样做:
c=a+(b-a)*d
d = 随机数组,其值在 0 到 1 之间,与 a
具有相同的维度
我有两个相同大小的 numpy ndarray。
a = np.random.randn(x,y)
b = np.random.randn(x,y)
我想创建一个新数组,其中每个元素都是 a
和 b
中具有相同索引的元素值之间的随机值。所以每个元素 c[i][j]
应该在 a[i][j]
和 b[i][j]
之间。
除了遍历 c
的所有元素并分配随机值之外,还有什么 quicker/simpler/more 有效的方法吗?
您可以使用 numpy.random.uniform,来自文档:
low : float or array_like of floats, optional
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())
输出
True
在上面的示例中,请注意使用 maximum and minimum 来构建 high
和 low
。最后一行检查确实 c
的所有值都在 high
和 low
之间。如果您对此感兴趣,您可以一行完成所有操作:
c = np.random.uniform(np.minimum(a, b), np.maximum(a, b), (x, y))
这是一个使用 numpy
的想法:
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]])
你可以这样做:
c=a+(b-a)*d
d = 随机数组,其值在 0 到 1 之间,与 a
具有相同的维度