T-8*_*800 13 python numpy python-2.x
我有一个长度的矢量,让我们说10:
foo = np.arange(2,12)
Run Code Online (Sandbox Code Playgroud)
为了将它转换为2-D数组,让我们说2列,我使用reshape带有以下参数的命令:
foo.reshape(len(foo)/2, 2)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更优雅的方式/语法来做(可能是某样的foo.reshape(,2))
DSM*_*DSM 25
你几乎拥有它!你可以用-1.
>>> foo.reshape(-1, 2)
array([[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
Run Code Online (Sandbox Code Playgroud)
正如reshape文档所说:
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is inferred
from the length of the array and remaining dimensions.
Run Code Online (Sandbox Code Playgroud)