生成 0 和 1 之间等距离的序列号

Generate sequential numbers between 0 and 1 with equal distance

我正在我的数组中生成随机变量: np.random.rand(5,3,20)

如何创建相同的形状和大小,但顺序在 0 和 1 之间?

只需使用:

np.linspace(0, 1, num=5).reshape((5, 3))

并设置 num 参数以隐式定义您的步长需要多大。

使用 linspace 在指定的时间间隔内创建均匀分布的数字,然后 reshape 形成所需的形状,如下所示:

np.linspace(0, 1, 300).reshape(5, 3, 20)

注:

'The new shape should be compatible with the original shape'

这么说吧, 对于 np.linspace(0, 1, t).reshape(x, y, z) 应该满足的条件是 t = x*y*z

另一个选项:

#start:stop:stepj inside np.r_ is interpreted as np.linspace(start, stop, step, endpoint=1) inside of the brackets
np.r_[0:1:300j].reshape(5,3,20)