Reshape an array in NumPy
Solution 1
a = np.arange(18).reshape(9,2)
b = a.reshape(3,3,2).swapaxes(0,2)
# a:
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17]])
# b:
array([[[ 0, 6, 12],
[ 2, 8, 14],
[ 4, 10, 16]],
[[ 1, 7, 13],
[ 3, 9, 15],
[ 5, 11, 17]]])
Solution 2
numpy has a great tool for this task ("numpy.reshape") link to reshape documentation
a = [[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]
[16 17]]
`numpy.reshape(a,(3,3))`
you can also use the "-1" trick
`a = a.reshape(-1,3)`
the "-1" is a wild card that will let the numpy algorithm decide on the number to input when the second dimension is 3
so yes.. this would also work:
a = a.reshape(3,-1)
and this:
a = a.reshape(-1,2)
would do nothing
and this:
a = a.reshape(-1,9)
would change the shape to (2,9)
Related videos on Youtube
user1876864
Updated on July 09, 2022Comments
-
user1876864 almost 2 years
Consider an array of the following form (just an example):
[[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11] [12 13] [14 15] [16 17]]
It's shape is [9,2]. Now I want to transform the array so that each column becomes a shape [3,3], like this:
[[ 0 6 12] [ 2 8 14] [ 4 10 16]] [[ 1 7 13] [ 3 9 15] [ 5 11 17]]
The most obvious (and surely "non-pythonic") solution is to initialise an array of zeroes with the proper dimension and run two for-loops where it will be filled with data. I'm interested in a solution that is language-conform...
-
Jaime over 11 yearsNote that
b
now is not contiguous, which means it cannot be reshaped in place:b.reshape(9, 2)
returns a copy, not a view of the same data, andb.shape = (9, 2)
will raise and error. -
deepelement over 8 yearsVery Very important comment by @Jaime, as the point of Shape is to allow optimistic resizing without a clone. Big deal with massive datasets
-
Jürgen K. about 5 yearsWhy do you need to swap the axes?