Problem Formulation: Given a one-dimensional NumPy array. How to create a new two-dimensional array by reshaping the original array so that the new array has
x rows and
Here’s an example of what you’re trying to do:
# Given: [0 1 2 3 4 5] x = 2 # rows y = 3 # columns # Desired: [[0 1 2] [3 4 5]]
reshape() function takes an array to be reshaped as a first argument and the new shape tuple as a second argument. It returns a new view on the existing data—if possible—rather than create a full copy of the original array. The returned array behaves like a new object: any change on one view won’t affect any other view.
You can reshape a 1D array into a 2D array with the following four steps:
- Import the NumPy library with
import numpy as np,
- Use the function
- Pass the original 1D array as a first argument,
- Pass the new shape tuple
ycolumns as a second argument.
In summary, the function call
np.reshape(original_array, (x, y)) will create a 2D array with
x rows and
import numpy as np # Problem: Reshape this 1D into a 2D array array_1d = np.array([0, 1, 2, 3, 4, 5]) # Solution: np.reshape(array, shape) array_2d = np.reshape(array_1d, (2, 3)) # Check the new array print(array_2d)
The output is the 2D array in its desired form:
# Reshaped 2D Array:
[[0 1 2] [3 4 5]]
Let’s get some practice to train your understanding of the reshaping 1D to 2D functionality!
NumPy Puzzle Reshaping
Numpy is a popular Python library for data science focusing on linear algebra. This puzzle performs a miniature stock analysis of the Apple stock.
import numpy as np # apple stock prices (May 2018) prices = [ 189, 186, 186, 188, 187, 188, 188, 186, 188, 188, 187, 186 ] prices = np.array(prices) data_3day = prices.reshape(4,3) print(int(np.average(data_3day))) print(int(np.average(data_3day[-1])))
Exercise: What is the output of this puzzle?
You can also solve the puzzle interactively on our Finxter puzzle-based training app here:
First, we create a NumPy array from the raw price data.
Second, we create a new array
data_3day for more convenient analysis. This array bundles the price data from three days into each row. We examine some rows in more detail later.
Third, we average the 3-day price data of the first and last row using the NumPy
np.average() function. Doing this results in data points that are more robust against outliers. Comparing the first and the last 3-day price period reveals that the Apple stock price remains stable in our mini data set.
NumPy Reshape Video
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.