1. The reshape() Function

The NumPy reshape() function is used to change the shape of an array without altering its data. It returns a new view or array with the specified dimensions, provided the total number of elements remains constant.

Use Cases
The reshape() function is useful for transforming data to fit different processing requirements such as converting between row-major and column-major formats or preparing data for machine learning models.

This function takes two main arguments: one is the array to reshape and the other is a tuple specifying the new shape. If the new shape is not compatible with the total number of elements, a ValueError is raised.

Syntax

numpy.reshape(arr, newshape, order='C')

Parameters

Parameter Description
arr The input array that has to be reshaped.
newshape This parameter can be an int or tuple of int. The new shape should be compatible with the original shape (i.e., the total number of elements must match).
order This parameter defines read and write order. 'C' for row-major (C-style, default), 'F' for column-major (Fortran-style).
Return Value: The reshape() function returns a new array with the same data but a different shape.

Practical Examples

Example 1: Basic Reshaping (1D to 2D)

In this example we reshape a 1D array of 8 elements into a 2x4 2D array.

import numpy as np # Create a 1D array with 8 elements a = np.arange(8) print('The original array:') print(a) print('\n') # Reshape the array to a 2D array with shape (4, 2) b = a.reshape(4, 2) print('The modified array:') print(b)

Output:

The original array: [0 1 2 3 4 5 6 7] The modified array: [[0 1] [2 3] [4 5] [6 7]]

Example 2: Using -1 for Automatic Calculation

Here this example uses -1 to automatically calculate the appropriate dimension for reshaping a 2x3 array into a 3x2 array. NumPy will infer the missing dimension based on the total elements.

import numpy as np # Original array a = np.array([[1, 2, 3], [4, 5, 6]]) # Reshaping the array to 3x2 using -1 to infer one of the dimensions reshaped_array = np.reshape(a, (3, -1)) print("Original array:") print(a) print("Reshaped array (3x2):") print(reshaped_array)

Output:

Original array: [[1 2 3] [4 5 6]] Reshaped array (3x2): [[1 2] [3 4] [5 6]]
Pro Tip: You can only use -1 for one dimension. NumPy will calculate that dimension's size using the total elements.

Example 3: Changing the Order (Row-major vs Column-major)

As we know that we can assign the order parameter based on our requirement, in the below example we reshape a 2x3 array into a 3x2 array by defining order as 'F' i.e., Fortran-like column-major order.

import numpy as np # Original array a = np.array([[1, 2, 3], [4, 5, 6]]) # Reshaping the array to 3x2 in Fortran-like order reshaped_array = np.reshape(a, (3, 2), order='F') print("Original array:") print(a) print("Reshaped array (3x2) in Fortran-like order:") print(reshaped_array)

Output:

Original array: [[1 2 3] [4 5 6]] Reshaped array (3x2) in Fortran-like order: [[1 5] [4 3] [2 6]]

Notice how the elements are placed into columns first, rather than rows.


2. The ndarray.flat Attribute

The NumPy ndarray.flat attribute returns a 1-D iterator over the array. This iterator allows us to iterate over the array as if it were flattened, but it does not create a new array.

Use Cases
This attribute is especially useful when we need to iterate over elements of a multidimensional array in a flat manner or modify elements directly via their flat index.

Syntax

numpy.ndarray.flat
Parameter / Return Value Description
Parameter The ndarray.flat attribute does not take any parameters.
Return Value Returns a 1-D iterator over the array. This iterator can be used to access and modify elements of the array.

Practical Examples

Example 1: Iterating over a 2D Array

Following is an example of the ndarray.flat attribute, which shows how to iterate over each element of a 2D array using a flat iterator.

import numpy as np # Creating a 2D numpy array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Using flat to iterate over elements for item in array_2d.flat: print(item)

Output:

1 2 3 4 5 6

Example 2: Modifying Elements

This example doubles each element in the array by iterating through the flat iterator and modifying each element directly.

import numpy as np # Creating a 2D numpy array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Modifying elements using flat iterator for index, value in enumerate(array_2d.flat): array_2d.flat[index] = value * 2 print(array_2d)

Output:

[[ 2 4 6] [ 8 10 12]]
Notice: Because flat provides an iterator directly over the array's memory, modifying elements via the iterator modifies the original array in place.

Example 3: Accessing Specific Elements

Here in this example we access specific elements of a 3D array using the flat iterator, showing how the flat iterator flattens the multidimensional array into a single dimension for easy access.

import numpy as np # Creating a 3D numpy array array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Accessing specific elements using flat print(array_3d.flat[0]) # First element print(array_3d.flat[5]) # Sixth element print(array_3d.flat[-1]) # Last element

Output:

1 6 8

3. The ndarray.flatten() Method

The NumPy ndarray.flatten() method is used to return a new 1-D array that is a copy of the original array which is flattened.

Key Difference
The ndarray.flat attribute is an iterator that provides a view to the original array, while ndarray.flatten() creates a completely new array (a copy) in memory.

Syntax

ndarray.flatten(order='C')

Parameter

Order Description
'C' (Default) Flatten in row-major (C-style) order.
'F' Flatten in column-major (Fortran-style) order.
'A' Flatten in column-major order if the array is Fortran-contiguous in memory, row-major otherwise.
'K' Flatten in the order the elements occur in memory.
Return Value: This function returns a one-dimensional numpy array which contains all the elements of the input array in the specified order.

Practical Examples

Example 1: Default Flattening (Row-major)

Following is the example of ndarray.flatten() method which shows flattening a 2D array into a 1D array in the default row-major order, resulting in [1, 2, 3, 4, 5, 6].

import numpy as np # Creating a 2D numpy array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Flattening the array using the default order ('C') flattened_array = array_2d.flatten() print("Original 2D array:") print(array_2d) print("\nFlattened array (row-major order):") print(flattened_array)

Output:

Original 2D array: [[1 2 3] [4 5 6]] Flattened array (row-major order): [1 2 3 4 5 6]

Example 2: Column-major Flattening

Here in this example we show how a 3D array is flattened into a 1D array in column-major order which yields [1, 5, 3, 7, 2, 6, 4, 8].

import numpy as np # Creating a 3D numpy array array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Flattening the array using the 'F' order flattened_array = array_3d.flatten(order='F') print("Original 3D array:") print(array_3d) print("\nFlattened array (column-major order):") print(flattened_array)

Output:

Original 3D array: [[[1 2] [3 4]] [[5 6] [7 8]]] Flattened array (column-major order): [1 5 3 7 2 6 4 8]

Example 3: Fortran-contiguous Array

Below example shows flattening a Fortran-contiguous array using the 'A' order which results in column-major order flattening, giving [1, 4, 2, 5, 3, 6].

import numpy as np # Creating a 2D numpy array with Fortran-contiguous memory layout array_fortran = np.asfortranarray([[1, 2, 3], [4, 5, 6]]) # Flattening the array using the 'A' order flattened_array = array_fortran.flatten(order='A') print("Original Fortran-contiguous 2D array:") print(array_fortran) print("\nFlattened array ('A' order):") print(flattened_array)

Output:

Original Fortran-contiguous 2D array: [[1 2 3] [4 5 6]] Flattened array ('A' order): [1 4 2 5 3 6]

4. The ravel() Function

The NumPy ravel() function returns a flattened array by providing a 1-D array containing the same elements as the input but with only one dimension.

Memory Efficiency
When compared to the flatten() function, ravel() returns a flattened view of the input array whenever possible. It only returns a copy if the input array is not contiguous. This makes ravel() much more memory efficient for certain operations.

Syntax

numpy.ravel(a, order='C')

Parameters

Parameter Description
a (array_like) The elements in a are read in row-major (C-style) order or column-major (Fortran-style) order depending on the order parameter.
order Specifies the order in which the elements are read:
  • 'C': Row-major (C-style) order.
  • 'F': Column-major (Fortran-style) order.
  • 'A': Row-major order if the array is stored in row-major memory order, and column-major order otherwise.
  • 'K': Elements are read in the order they occur in memory, except for reversing the data when strides are negative.
Return Value: This function returns a 1-D array containing the elements of the input array in a flattened order.

Practical Examples

Example 1: Flattening a 2D Array

Following is the example of NumPy ravel() function which shows the basic use of ravel() to flatten a 2D array into a 1D array in row-major order.

import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Flattening the array flattened_array = np.ravel(array_2d) print("Original array:\n") print(array_2d) print("Flattened array:") print(flattened_array)

Output:

Original array: [[1 2 3] [4 5 6]] Flattened array: [1 2 3 4 5 6]

Example 2: Flattening a 3D Array

Here this example illustrates the use of numpy.ravel() function to flatten a 3D array into a 1D array, following the default row-major order.

import numpy as np # Creating a 3D array array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Flattening the array flattened_array_3d = np.ravel(array_3d) print("Original 3D array:\n") print(array_3d) print("Flattened array:") print(flattened_array_3d)

Output:

Original 3D array: [[[1 2] [3 4]] [[5 6] [7 8]]] Flattened array: [1 2 3 4 5 6 7 8]

5. The pad() Function

The NumPy pad() function is used to pad an array with values along its edges. Padding is typically done to increase the size of an array by adding values (such as zeros or a constant) around its edges.

Flexibility
The function provides flexibility in specifying the padding width, the mode of padding, and custom values. Padding can be applied in various ways by setting the mode parameter to options such as constant, edge, symmetric, reflect, and more.

Syntax

numpy.pad(array, pad_width, mode='constant', **kwargs)

Parameters

Parameter Description
array Input array to be padded.
pad_width Number of values padded to the edges of each axis. It can be an integer, tuple, or sequence of tuples.
mode (optional) Specifies the method of padding. Default is 'constant'. Available modes include constant, edge, linear_ramp, reflect, symmetric, etc.
**kwargs (optional) Additional keyword arguments depending on the padding mode, such as constant_values for constant padding.
Return Value: This function returns a padded array with the same or modified shape based on the specified padding.

Practical Examples

Example 1: Basic Padding (Default Constant 0)

Following is a basic example to pad a numpy array using the pad() function.

import numpy as np array = np.array([1, 2, 3]) padded_array = np.pad(array, pad_width=2, mode='constant') print("Padded Array:", padded_array)

Output:

Padded Array: [0 0 1 2 3 0 0]

Example 2: Padding with Different Modes (Edge)

Using numpy.pad(), we can pad an array with different modes such as edge, reflect, or symmetric. In the following example, we pad an array using the edge mode, which replicates the edge values.

import numpy as np array = np.array([1, 2, 3]) print("Original Array:", array) padded_array = np.pad(array, pad_width=2, mode='edge') print("Edge Padded Array:", padded_array)

Output:

Original Array: [1 2 3] Edge Padded Array: [1 1 1 2 3 3 3]

Example 3: Padding Multi-dimensional Arrays

The numpy.pad() function can also pad multi-dimensional arrays along specified axes with different padding widths. Here, a 2D array is padded as follows:

  • (1, 1): specifies padding for the rows, where 1 row is added above and 1 row below the original array, filled with 0s.
  • (2, 2): specifies padding for the columns, where 2 columns are added to the left and 2 columns to the right, filled with 0s.
import numpy as np array = np.array([[1, 2], [3, 4]]) padded_array = np.pad(array, pad_width=((1, 1), (2, 2)), mode='constant', constant_values=0) print("2D Padded Array:\n", padded_array)

Output:

2D Padded Array: [[0 0 0 0 0 0] [0 0 1 2 0 0] [0 0 3 4 0 0] [0 0 0 0 0 0]]

Example 4: Custom Padding with Constant Values

We can customize the padding values in constant mode by using the constant_values parameter. Here, a 1D array is padded with the constant value 9 on both sides, repeated 3 times.

import numpy as np array = np.array([5, 6, 7]) padded_array = np.pad(array, pad_width=3, mode='constant', constant_values=9) print("Constant Padded Array:", padded_array)

Output:

Constant Padded Array: [9 9 9 5 6 7 9 9 9]

Real-World Practice Exercises

Test your knowledge of array manipulation functions with these practical, real-world scenarios. Try to solve them before viewing the answers!

Question 1: Image Processing (Reshape)

You have loaded a grayscale image dataset representing 100 images, where each image is flattened into 784 pixels (Total array shape: (100, 784)). You need to pass these images into a Convolutional Neural Network (CNN) that expects the shape to be (Batch, Height, Width).

Task: Use a NumPy manipulation function to transform this array into (100, 28, 28).

Solution: We use the reshape() function. Since 28 × 28 = 784, the total number of elements matches perfectly.

import numpy as np images_flat = np.random.rand(100, 784) # Example dataset images_2d = images_flat.reshape(100, 28, 28) # Or dynamically: images_flat.reshape(-1, 28, 28)
Question 2: Temperature Sensor Cleanup (Flat)

You have a 3D array of temperature readings collected from different sensors over multiple days: temperatures = np.array([[[35, 36], [38, 42]], [[31, 30], [29, 28]]]). Any reading above 40 degrees is considered a sensor glitch and needs to be capped at exactly 40 degrees.

Task: Iterate through every single reading without creating a copy, and modify values > 40 in place.

Solution: We use the ndarray.flat attribute because it provides an iterator and allows in-place modification of multi-dimensional arrays.

import numpy as np temperatures = np.array([[[35, 36], [38, 42]], [[31, 30], [29, 28]]]) for i, temp in enumerate(temperatures.flat): if temp > 40: temperatures.flat[i] = 40 print(temperatures)
Question 3: Exporting Data for a CSV (Flatten)

You have a 2D matrix representing monthly sales for 3 stores: sales = np.array([[10, 20, 30], [40, 50, 60]]). You need to export this data to a legacy system that only accepts a single continuous row of numbers (a 1D list). Because the legacy system might accidentally corrupt the data, you must provide a safe, independent copy.

Task: Convert the 2D sales matrix into a 1D array while ensuring it is physically copied in memory.

Solution: We use the flatten() method because it guarantees returning a deep copy of the array.

import numpy as np sales = np.array([[10, 20, 30], [40, 50, 60]]) safe_1d_export = sales.flatten() # Modifying the export will NOT affect original sales safe_1d_export[0] = 999 print("Original:", sales[0,0]) # Output: 10
Question 4: Memory-Efficient Flattening (Ravel)

You are working with a massive 3D scientific simulation array containing 1 billion elements (shape=(1000, 1000, 1000)). You need to feed this data sequentially into a 1D processing algorithm. Your server is running very low on RAM, so creating a copy of the array will crash the system.

Task: What function should you use to view this 3D array as 1D without allocating new memory?

Solution: We use the ravel() function. Unlike flatten(), ravel() returns a memory-efficient view whenever the underlying data is contiguous, saving gigabytes of RAM.

import numpy as np # Assuming 'massive_array' is already loaded # 1D_view = np.ravel(massive_array)
Question 5: Convolutional Image Padding (Pad)

In computer vision, when applying filters to a 3x3 pixel image grid (image = np.ones((3, 3))), the filter often shrinks the image. To prevent shrinking, we add a border of 0s around the entire image (this is called "Zero Padding").

Task: Add a border of precisely 1 row/column of zeros around all sides of the 3x3 image grid.

Solution: We use the pad() function with mode='constant' (which defaults to 0). Since we want 1 element padded on all axes (top, bottom, left, right), we can simply pass pad_width=1.

import numpy as np image = np.ones((3, 3)) padded_image = np.pad(image, pad_width=1, mode='constant', constant_values=0) print(padded_image) # Output will be a 5x5 grid with 1s in the center and 0s on the border

Manipulation Quick Quiz

Test your knowledge on array manipulation functions. Click the options below to reveal the correct answer.

1. Which function returns a memory-efficient "view" of the array whenever possible rather than a copy?
A) np.flatten()
B) np.reshape(-1)
C) np.ravel()
D) np.pad()
2. What happens if you try to reshape() a (3, 3) array into a (5, 2) array?
A) It pads the missing element with a 0
B) It raises a ValueError
C) It truncates the array to fit
D) It creates a (5, 2) array filled with NaN
3. How can you modify elements in a multi-dimensional array directly using an iterator?
A) Using ndarray.flat
B) Using ndarray.flatten()
C) Using np.ravel()
D) Using np.pad()
4. If you want to automatically calculate one dimension during a reshape, what value do you use?
A) None
B) 0
C) -1
D) *
5. In np.pad(array, pad_width=1, mode='constant', constant_values=5), what does this do to a 1D array [1, 2]?
A) [0, 1, 2, 0]
B) [5, 5, 1, 2, 5, 5]
C) [5, 1, 2, 5]
D) [1, 2, 5, 5]