简体   繁体   中英

Numpy / Why can't I use reshape with random.randint?

I have this line od code :

tirage2=np.random.randint(0,2,size=(10,5))

在此处输入图像描述

So I was thinking using reshape is possible like in this line :

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'

When you call np.random.randint(a,b) , it returns an int object between a and b . If you want a matrix with values in that range, you should use np.random.randint(a, b, size=(rows,cols)) . Then you'll be able to call reshape on the resulting np.ndarray .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM