[英]Numpy / Why can't I use reshape with random.randint?
I have this line od code :我有这一行 od 代码:
tirage2=np.random.randint(0,2,size=(10,5))
So I was thinking using reshape is possible like in this line :所以我在想使用 reshape 是可能的,就像这行一样:
tirage4=np.arange(50).reshape(10,5)
But this throws an error :但这会引发错误:
tirage3=np.random.randint(0,2).reshape(5,10)
AttributeError: 'int' object has no attribute 'reshape' AttributeError:“int”对象没有属性“reshape”
When you call np.random.randint(a,b)
, it returns an int
object between a
and b
.当您调用
np.random.randint(a,b)
时,它会返回a
和b
之间的int
对象。 If you want a matrix with values in that range, you should use np.random.randint(a, b, size=(rows,cols))
.如果您想要一个值在该范围内的矩阵,您应该使用
np.random.randint(a, b, size=(rows,cols))
。 Then you'll be able to call reshape
on the resulting np.ndarray
.然后,您将能够在生成的
np.ndarray
上调用reshape
。
The reason you can call reshape
on the return type of np.arange
is because np.arange
returns an np.ndarray
, which can be reshaped.您可以在
np.arange
的返回类型上调用reshape
的原因是因为np.arange
返回一个np.ndarray
,它可以被重新整形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.