Reputation: 1603
I have a vector of length, lets say 10:
foo = np.arange(2,12)
In order to convert it to a 2-D array, with lets say 2 columns, I use the command reshape
with following arguments:
foo.reshape(len(foo)/2, 2)
I was wondering if there is a more elegant way/syntax to do that (may be sth like foo.reshape(,2)
)
Upvotes: 21
Views: 14902
Reputation: 353249
You almost had it! You can use -1
.
>>> foo.reshape(-1, 2)
array([[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11]])
As the reshape
docs say:
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.
Upvotes: 38