Table of Contents



Introduction

Reshaping an array means changing the shape of an array. To recap, the shape attribute of an ndarray object returns a tuple that gives the lengths of the corresponding array dimensions. By reshaping an array, we can add or remove dimensions or change the length in each dimension.

Reshaping an Array

The most direct way of reshaping an array is using the reshape() method.

Example

Reshaping a 1-D array.

1a = np.arange(1, 13)
2print(a)
3b = a.reshape(4,3)
4print(b)
[ 1  2  3  4  5  6  7  8  9 10 11 12]

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Note that the number of elements in the 1-D array a matches the number of elements in the reshaped array b. In both cases, there are 12 elements.

Note

The size of the initial array must match the size of the reshaped array.

Example

Reshaping 2-D arrays.

1c = b.reshape(2,6)
2print(c)
3d = c.reshape(6,2)
4print(d)
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]

[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]

Example

Reshaping a 2-D array into a 3-D array.

1e = d.reshape(2,3,2)
2print(e)
[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]

The NumPy base Attribute

If an ndarray object B is derived from another ndarray object A (i.e. memory location is shared), then object A will be the base of object B. The base attribute of an ndarray object (executed as ndarray.base) will return the base object if memory is from some other object. In short, the base attribute lets us know if an array is a view of another array or not.

Example

Using the base attribute.

Referring to the same arrays a and b defined in the previous section, we want to know if array b is a view or copy of array a by invoking the base attribute.

1print(b.base)
2print(b.base is a)
[ 1  2  3  4  5  6  7  8  9 10 11 12]

True

We see that b.base returns the a array. This tells us that b is a view of a. This suggests the reshape() method returns a view (rather than a copy) of the original array. This fact will again be verified in the next section.

ADVERTISEMENT



reshape() Method Returns a View

We have discussed array view vs array copy in the previous section. Typically, the reshape() method returns a view rather than a copy.

Example

Reshaped arrays are views.

To illustrate this, the reshaped array b is modified and this is immediately reflected in the original array a and also the array c which was reshaped from b.

1b[0,0] = 99
2print(a)
3print(b)
4print(c)
[99  2  3  4  5  6  7  8  9 10 11 12]

[[99  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

[[99  2  3  4  5  6]
 [ 7  8  9 10 11 12]]

The numpy.newaxis Method

The numpy.newaxis method is an alias for the None, which is used for array indexing in NumPy. The most straightforward use of the numpy.newaxis method is to add a new dimension to a NumPy array.

Example

Adding a new dimension to an array using numpy.newaxis.

1x = np.arange(1, 5) # 1-D array
2print(x)
3x_row = x[np.newaxis, :] # row matrix
4print(x_row)
5x_col = x[:, np.newaxis] # column matrix
6print(x_col)
[1 2 3 4]

[[1 2 3 4]]

[[1]
 [2]
 [3]
 [4]]

We can also add a new dimension to an array using the reshape() method.

Example

Adding a new dimension to an array using reshape() method.

1x = np.arange(1, 5) # 1-D array
2print(x)
3x_row = x.reshape((1, 4)) # row matrix
4print(x_row)
5x_col = x.reshape((4, 1))# column matrix
6print(x_col)
[1 2 3 4]

[[1 2 3 4]]

[[1]
 [2]
 [3]
 [4]]

The Unknown Dimension

In specifying the shape in the reshape() method, the user is allowed to leave the size one of the dimensions as -1 and NumPy will calculate this number automatically.

Example

Using the reshape() method with an unknown dimension (2-D).

1x = np.arange(1, 13) # 1-D array
2print(x)
3x1 = x.reshape(4,-1)
4print(x1.shape)
5print(x1)
[ 1  2  3  4  5  6  7  8  9 10 11 12]

(4, 3)

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Example

Using the reshape() method with an unknown dimension (3-D).

1x2 = x.reshape(2,2,-1)
2print(x2.shape)
3print(x2)
(2, 2, 3)

[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

ADVERTISEMENT



Flattening an Array

Flattening an array means converting a multidimensional array into a 1-D array. In this case, we can simply use the reshape() method with an argument of -1 to accomplish this.

Example

Flattening a 2-D array using reshape(-1).

1print(x1) # 2-D array
2print(x1.reshape(-1)) # flattened array
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

[ 1  2  3  4  5  6  7  8  9 10 11 12]

Example

Flattening a 3-D array using reshape(-1).

1print(x2) # 3-D array
2print(x2.reshape(-1)) # flattened array
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]

[ 1  2  3  4  5  6  7  8  9 10 11 12]