[英]Python Numpy Reshape an array to (m,n) shape that has less than m*n elements
我正在尝试将一个简单的数组转换为 (m,n) 形状,但它的元素少于 m*n。
我的代码:
list = [1,2,3,4,5]
ary = np.array(list)
reary = ary.reshpae(2,3)
现回答:
ValueError: cannot reshape array of size 5 into shape (2,3)
预期答案:
reary =
[[1,2,3],
[4,5]]
尝试这个:
ary = np.array([1,2,3,4,5])
r, c = 2, 3
a = np.pad(ary, (0, r * c - len(ary))).reshape(r, c)
>>> a
array([[1, 2, 3],
[4, 5, 0]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.