In data processing, we often need to adjust the shape of arrays to meet different requirements, such as converting two-dimensional data to one-dimensional or changing the number of rows and columns in a matrix. Numpy provides two very practical methods: reshape and flatten, which can easily achieve array reshaping. This article will explain the usage of these two methods from scratch in the simplest way.

一、基础概念:数组变形的前提

The core premise we need to clarify first is: The total number of elements before and after array reshaping must remain consistent. For example, an array with 12 elements must have a total element count of 12 when reshaped into (3,4), (4,3), or (12,1); otherwise, an error will occur.

For example, suppose we have a 1-dimensional array:

import numpy as np

# Create a 1D array with 12 elements (0 to 11)
arr = np.arange(12)
print("Original array:", arr)
print("Original array shape:", arr.shape)  # Output: (12,), indicating 12 elements in 1D

二、reshape:改变数组形状

The reshape method can convert an array from its current shape to a specified new shape, without changing the order and total number of elements in the original array. Its syntax is arr.reshape(new_shape), where new_shape is a tuple (e.g., (number of rows, number of columns)).

1. Basic Usage: Specify the New Shape

For example, we want to reshape an array of 12 elements into a 3x4 two-dimensional array:

# Reshape the 1D array into a 3x4 matrix
arr_3x4 = arr.reshape(3, 4)
print("Array after reshape(3,4):\n", arr_3x4)
print("New shape:", arr_3x4.shape)  # Output: (3,4)

Output result:

Array after reshape(3,4):
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
New shape: (3, 4)

2. Parameter -1: Automatically Calculate Missing Dimensions

If you are unsure of the size of a dimension, you can use -1 to let Numpy automatically calculate it. For example, we want to reshape the 12-element array into 3 rows with columns calculated automatically:

# Use -1 to automatically calculate the number of columns (total elements 12, 3 rows → 12/3=4 columns)
arr_3x_any = arr.reshape(3, -1)
print("Array after reshape(3,-1):\n", arr_3x_any)
print("Shape:", arr_3x_any.shape)  # Output: (3,4)

3. Note: The Original Array Remains Unchanged

reshape returns a new array and does not modify the original array. This is important; the original array arr still has the shape (12,):

print("Original array shape:", arr.shape)  # Still (12,)

三、flatten:展平为1维数组

When we need to convert a multi-dimensional array (e.g., 2D, 3D) into a 1D array, flatten comes in handy. It “flattens” the array into a single row and returns a new array (without modifying the original array).

1. Basic Usage: Direct Flattening

Let’s flatten the previously created 3x4 2D array:

# Flatten the 3x4 array into 1D
arr_flat = arr_3x4.flatten()
print("Array after flattening:", arr_flat)
print("Shape:", arr_flat.shape)  # Output: (12,)

Output result:

Array after flattening: [ 0  1  2  3  4  5  6  7  8  9 10 11]
Shape: (12,)

2. Difference from ravel (Brief Understanding)

Numpy also has a similar method ravel. The difference between flatten and ravel is:
- flatten returns a new array (copy), and modifications will not affect the original array;
- ravel returns a view of the original array (shallow copy), and modifications may affect the original array.

For beginners, flatten is safer, and it is recommended to use it first.

四、Common Errors and Solutions

The most common error is “mismatched total elements after reshape”. For example, reshaping a 12-element array into (3,5) (3×5=15≠12):

arr.reshape(3,5)  # Error! ValueError: cannot reshape array of size 12 into shape (3,5)

Solution: Check if the product of the reshape parameters equals the total number of elements in the original array (i.e., original_array.size).

五、Comprehensive Example: From Creation to Reshaping

Let’s demonstrate a complete workflow:

# 1. Create a 1D array with 6 elements
arr = np.arange(6)
print("Original array:", arr, "Shape:", arr.shape)  # (6,)

# 2. Reshape into a 2x3 2D array
arr_2x3 = arr.reshape(2, 3)
print("\nAfter reshape(2,3):\n", arr_2x3, "Shape:", arr_2x3.shape)

# 3. Flatten into a 1D array
arr_flat = arr_2x3.flatten()
print("\nAfter flattening:", arr_flat, "Shape:", arr_flat.shape)

Output result:

Original array: [0 1 2 3 4 5] Shape: (6,)

After reshape(2,3):
 [[0 1 2]
 [3 4 5]] Shape: (2, 3)

After flattening: [0 1 2 3 4 5] Shape: (6,)

六、Summary

  • reshape: Changes the array shape without altering element order/total count; -1 can automatically calculate missing dimensions.
  • flatten: Converts the array into a 1D array, returns a new array, and does not affect the original array.
  • Core principle: The total number of elements before and after reshaping must be consistent.

Mastering these two methods allows you to flexibly handle array shapes, laying the foundation for subsequent data processing (e.g., machine learning, data analysis)!

Xiaoye