Search⌘ K
AI Features

NumPy Basics - Reshaping and Concatenation

Explore how to reshape arrays, combine multiple arrays, and split arrays using NumPy in Python. Understand reshaping concepts and methods like concatenate, vstack, and hstack to manipulate array structures effectively for data science projects.

5. Reshaping of Arrays

Reshaping is about changing the way items are arranged within the array so that the shape of the array changes, but the total number of elements in both arrays remains the same, e.g., you can use it to convert a 1D array into 2D array.

Reshaping is a very useful operation and it can easily be done using the reshape() method. Since a picture speaks a thousand words, let’s see the effects of reshaping visually:

Reshaping example (Image credits: www.3resource.com)
Reshaping example (Image credits: www.3resource.com)

How can we do this in code? Say we want to create a 3x3 grid with numbers from 1 to 9. We first create a 1D array and then convert it to the desired shape as shown below.

...