How to reshape arrays in NumPy with np.reshape

Einblick Content Team - April 5th, 2023

In this article, we will cover the basics of how to reshape NumPy arrays. This can be useful when reformatting the shape of a dataset in order to make it compatible with various machine learning algorithms. NumPy reshape can also be used for dimensionality reduction by reducing the number of features or variables from a dataset while preserving its structure and relationships between variables.

In the following canvas (and code enumerated below), we’ll cover the basic syntax for the function np.reshape(), its equivalent function that can be called directly on an array, arr, via arr.reshape(), as well as the uses of the placeholder -1.

Basic Syntax: np.reshape(arr, newshape)

The two basic arguments for np.reshape() are arr and newshape, which call for the array to be reshaped, and the dimensions of the reshaped array, respectively.

We start by creating a 1-dimensional array of integers using NumPy's arange() function:

import numpy as np

# Create a NumPy array of integers from 1 to 12
arr = np.arange(1, 13)
print(arr)

# Get the shape of arr
print(arr.shape)

Output:

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

Then we'll reshape the 1-dimensional array into a 2-dimensional array:

# Reshape arr into a 2-dimensional array
print(np.reshape(arr, (2,6)))

# Reshape arr into a 2-dimensional array
print(np.reshape(arr, (6,2)))

# Print arr -- see that it is unchanged
print(arr)

Output:

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

As you can see from the output, we can determine if we want two 6-element arrays or six 2-element arrays by swapping the values in the newshape argument. Additionally, the original arr remains unchanged. We were just able to work with a new view of the data.

Example: order argument, row- vs. column-major order

There are 3 options for the order argument when reshaping an array: 'C', 'F', or 'A'. You can think of the options in the following way:

  • The default is 'C', which stands for the programming language C. Utilizing order = 'C' results in an array read/written in row-major order.
  • The 'F' option stands for FORTRAN. Utilizing order = 'F' results in an array read/written in column-major order.
  • The 'A' option will read/write in row-major order or column-major order based on how the array is stored in memory.
# Reshape arr into a 2-dimensional array
print(np.reshape(arr, (2,6), order = "F"))

# Reshape arr into a 2-dimensional array
print(np.reshape(arr, (6,2), order = "F"))

Output:

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

In this example where order = 'F', you can see that the elements are in a different order--column-major order. This differs from the earlier example where the elements were in row-major order since the default argument is order = 'C'.

Basic Syntax: arr.reshape(newshape, order = 'C')

You can also call the reshape function directly on an array. The syntax is the same, except that you do not need to use the argument arr to indicate the array to reshape.

# Get the shape of arr
print(arr.shape)

# Reshape arr into a 2-dimensional array
print(arr.reshape((2,6), order = "C"))

# Reshape arr into a 2-dimensional array
print(arr.reshape((6,2), order = "C"))

# Print arr -- see that it is unchanged
print(arr)

Output:

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

What does a dimension of -1 do in np.reshape?

Put simply, -1 serves as a placeholder for the nth dimension. The value is determined by the number of elements and the remaining n-1 dimensions.

For example, if you are creating a 2-dimensional array with 12 elements, as below, by providing the shape (2, -1), the function will infer the shape (2, 6) since there are 12 elements. If you are creating a 3-dimensional array with 12 elements, and provide the shape (2, -1, 2), the function will infer the shape (2, 3, 2) to ensure there are 12 elements in the resulting array.

The placeholder value allows you to avoid hardcoding in the nth dimension, allowing for more flexibility in your data.

NOTE: Since -1 is a placeholder, dependent on the other dimensions provided, you can only use -1 one time when you define the shape of the array.

Example: 1-D array with np.reshape(arr, (-1))

# Reshape arr into a 1-dimensional array
print("1-dimensional array:")
print(np.reshape(arr, (-1)))

Output:

1-dimensional array:
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Example: 2-D array with np.reshape(arr, (2, -1))

# Reshape arr into a 2-dimensional array
print("2-dimensional array:")
print(np.reshape(arr, (2,-1)))

Output:

2-dimensional array:
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]

Example: 3-D array with np.reshape(arr, (2, -1, 2))

# Reshape arr into a 3-dimensional array
print("3-dimensional array:")
print(np.reshape(arr, (2, -1, 2)))

Output:

3-dimensional array:
[[[ 1  2]
  [ 3  4]
  [ 5  6]]
 [[ 7  8]
  [ 9 10]
  [11 12]]]

About

Einblick is an agile data science platform that provides data scientists with a collaborative workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick customers include Cisco, DARPA, Fuji, NetApp and USDA. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators