英文:
Reshape a variable numpy array
问题
假设我有一个形状已知的NumPy数组u
,以及u
中总条目数的除数d
。如何快速地将u
重新塑造为形状为(something,d)
的数组?
当u
只是一个双精度数时,也应包括在内 -> (1,1)
。
当u
为空时,应变成一个形状为(0,d)
的数组。
英文:
Suppose i have a numpy array u
with a given shape, a a divisor d
of the total number of entries in u
. How can i fastly reshape u
to be shaped (something,d)
?
The case where u
is just a double should be included as well -> (1,1)
The case where u
is empty should become a (0,d)
shaped array
答案1
得分: 1
你想要使用reshape
u.reshape(-1, d)
Python 中没有double
,你是不是想说float
?
简而言之:
import numpy as np
def div_reshape(arr, div):
if arr.size == 0:
return np.empty(shape=(0, div))
elif arr.size == 1:
return arr.reshape(1, 1)
else:
return arr.reshape(-1, d)
英文:
You want to use reshape
u.reshape(-1, d)
There is no double
in Python you do you mean float
?
In short :
import numpy as np
def div_reshape(arr, div):
if arr.size == 0:
return np.empty(shape=(0, div))
elif arr.size == 1:
return arr.reshape(1, 1)
else:
return arr.reshape(-1, d)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论