You want to reshape the array.
Use with AIB = np.reshape(A, (-1, 2))
where -1 infers the size of the new dimension from the size of the input array.
You want to reshape the array.
Use with AIB = np.reshape(A, (-1, 2))
where -1 infers the size of the new dimension from the size of the input array.
You have two options:
If you no longer want the original shape, the easiest is just to assign a new shape to the array
Use with AI
a.shape = (a.size//ncols, ncols)You can switch the
a.size//ncolsby-1to compute the proper shape automatically. Make sure thata.shape[0]*a.shape[1]=a.size, else you'll run into some problem.You can get a new array with the
np.reshapefunction, that works mostly like the version presented aboveUse with AInew = np.reshape(a, (-1, ncols))When it's possible,
newwill be just a view of the initial arraya, meaning that the data are shared. In some cases, though,newarray will be acopy instead. Note thatnp.reshapealso accepts an optional keywordorderthat lets you switch from row-major C order to column-major Fortran order.np.reshapeis the function version of thea.reshapemethod.
If you can't respect the requirement a.shape[0]*a.shape[1]=a.size, you're stuck with having to create a new array. You can use the np.resize function and mixing it with np.reshape, such as
Use with AI>>> a =np.arange(9)
>>> np.resize(a, 10).reshape(5,2)
python - Numpy reshape 1d to 2d array with 1 column - Stack Overflow
How to convert a 2D index into a 1D index?
What is the benefit/purpose of using Numpy 1D Arrays over Numpy 2D Arrays?
Is a (1,n) shaped array not 1D?
The simplest way:
ar.reshape(-1, 1)
You could do -
ar.reshape(ar.shape[0],-1)
That second input to reshape : -1 takes care of the number of elements for the second axis. Thus, for a 2D input case, it does no change. For a 1D input case, it creates a 2D array with all elements being "pushed" to the first axis because of ar.shape[0], which was the total number of elements.
Sample runs
1D Case :
In [87]: ar
Out[87]: array([ 0.80203158, 0.25762844, 0.67039516, 0.31021513, 0.80701097])
In [88]: ar.reshape(ar.shape[0],-1)
Out[88]:
array([[ 0.80203158],
[ 0.25762844],
[ 0.67039516],
[ 0.31021513],
[ 0.80701097]])
2D Case :
In [82]: ar
Out[82]:
array([[ 0.37684126, 0.16973899, 0.82157815, 0.38958523],
[ 0.39728524, 0.03952238, 0.04153052, 0.82009233],
[ 0.38748174, 0.51377738, 0.40365096, 0.74823535]])
In [83]: ar.reshape(ar.shape[0],-1)
Out[83]:
array([[ 0.37684126, 0.16973899, 0.82157815, 0.38958523],
[ 0.39728524, 0.03952238, 0.04153052, 0.82009233],
[ 0.38748174, 0.51377738, 0.40365096, 0.74823535]])