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
Thereshape() 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
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). |
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.
Output:
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.
Output:
-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.
Output:
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
| 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.
Output:
Example 2: Modifying Elements
This example doubles each element in the array by iterating through the flat iterator and modifying each element directly.
Output:
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.
Output:
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
Thendarray.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
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. |
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].
Output:
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].
Output:
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].
Output:
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 theflatten() 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
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:
|
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.
Output:
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.
Output:
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 themode parameter to options such as constant, edge, symmetric, reflect, and more.
Syntax
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. |
Practical Examples
Example 1: Basic Padding (Default Constant 0)
Following is a basic example to pad a numpy array using the pad() function.
Output:
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.
Output:
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.
Output:
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.
Output:
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.
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.
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.
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.
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.
Manipulation Quick Quiz
Test your knowledge on array manipulation functions. Click the options below to reveal the correct answer.