PYTHON FOR AI • LESSON 2

Reshaping

Reshaping allows you to change the structure of a NumPy array without changing the actual data. This is extremely important in AI and Machine Learning because models often expect data in a specific shape.

CORE IDEA

Reshaping changes how data is organized, not the data itself.

For example, an array containing 6 values can be changed from a 1-dimensional array into a 2 × 3 array or a 3 × 2 array. The values stay the same; only their arrangement changes.

01

What Is Reshaping?

Consider this NumPy array:

import numpy as np

numbers = np.array([1, 2, 3, 4, 5, 6])

print(numbers)
[1 2 3 4 5 6]

This is a one-dimensional array containing 6 values.

We can reshape it into 2 rows and 3 columns:

reshaped = numbers.reshape(2, 3)

print(reshaped)
[[1 2 3]
 [4 5 6]]

Notice that no values were added or removed. They were simply organized differently.

02

Why Is Reshaping Important in AI?

Machine Learning and AI models often expect data in a particular shape.

For example, imagine you have 6 numbers representing measurements:

[10, 20, 30, 40, 50, 60]

You may need to organize them as 2 samples with 3 features each:

[[10, 20, 30],
 [40, 50, 60]]

Reshaping lets you change the structure so the data matches what the next part of your AI pipeline expects.

03

The reshape() Method

NumPy provides the reshape() method:

array.reshape(rows, columns)

Example:

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(2, 3)

print(result)
[[1 2 3]
 [4 5 6]]

Here:

2 → number of rows
3 → number of columns
04

The Number of Elements Must Match

This is the most important rule when reshaping.

The total number of elements before and after reshaping must be the same.

Our original array contains 6 elements:

[1, 2, 3, 4, 5, 6]

6 elements

So these shapes are valid:

reshape(2, 3)   → 2 × 3 = 6
reshape(3, 2)   → 3 × 2 = 6
reshape(1, 6)   → 1 × 6 = 6
reshape(6, 1)   → 6 × 1 = 6

But this is invalid:

numbers.reshape(2, 4)

Because:

2 × 4 = 8

but the array only contains 6 elements.

NumPy will raise a ValueError.

05

One Array, Different Shapes

The same 6 values can have different shapes.

numbers = np.array([1, 2, 3, 4, 5, 6])

print(numbers.reshape(2, 3))
print(numbers.reshape(3, 2))
[[1 2 3]
 [4 5 6]]

[[1 2]
 [3 4]
 [5 6]]

The data is identical. Only the structure is different.

06

Reshape Into One Row

We can create a 2D array containing one row:

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(1, 6)

print(result)
[[1 2 3 4 5 6]]

Its shape is:

(1, 6)

That means 1 row and 6 columns.

07

Reshape Into One Column

We can also create a 2D array containing one column:

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(6, 1)

print(result)
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]

Its shape is:

(6, 1)

That means 6 rows and 1 column.

08

Checking the Shape

NumPy arrays have a shape attribute.

numbers = np.array([1, 2, 3, 4, 5, 6])

print(numbers.shape)

reshaped = numbers.reshape(2, 3)

print(reshaped.shape)
(6,)
(2, 3)

The first shape:

(6,)

means a one-dimensional array containing 6 elements.

The second shape:

(2, 3)

means 2 rows and 3 columns.

09

Reshaping a 2D Array

Reshaping is not limited to one-dimensional arrays.

data = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(data.shape)
(2, 3)

This array has 6 total elements, so we can reshape it into 3 rows and 2 columns:

result = data.reshape(3, 2)

print(result)
[[1 2]
 [3 4]
 [5 6]]
10

Using -1 in reshape()

NumPy allows you to use -1 when you don't want to manually calculate one dimension.

numbers = np.array([1, 2, 3, 4, 5, 6])

result = numbers.reshape(2, -1)

print(result)
[[1 2 3]
 [4 5 6]]

NumPy already knows there are 6 elements. We specified 2 rows, so NumPy calculates the columns:

6 ÷ 2 = 3

Therefore the final shape is (2, 3).

Another example:

numbers.reshape(-1, 2)

NumPy calculates the number of rows automatically:

[[1 2]
 [3 4]
 [5 6]]
11

Reshaping Data for AI

Suppose we have 12 measurements:

data = np.array([
    10, 20, 30, 40,
    50, 60, 70, 80,
    90, 100, 110, 120
])

Imagine that every 4 values represent one sample. We can reshape the data into 3 samples with 4 features:

data = data.reshape(3, 4)

print(data)
[[ 10  20  30  40]
 [ 50  60  70  80]
 [ 90 100 110 120]]

Now the structure is:

3 samples
4 features per sample

This is the kind of structural transformation you will frequently perform when preparing data for AI models.

12

Reshaping Image Data

Images are another important example. An image can be represented as numbers.

Imagine a very small grayscale image containing 9 pixels:

pixels = np.array([
    0, 10, 20,
    30, 40, 50,
    60, 70, 80
])

We can reshape those 9 values into a 3 × 3 image:

image = pixels.reshape(3, 3)

print(image)
[[ 0 10 20]
 [30 40 50]
 [60 70 80]]

The values did not change. Their structure changed from a single list into rows and columns.

This is one reason understanding array shapes is important before working with computer vision and deep learning.

13

Reshaping Does Not Change the Data

This is worth remembering.

numbers = np.array([1, 2, 3, 4, 5, 6])

reshaped = numbers.reshape(3, 2)

print(reshaped)
[[1 2]
 [3 4]
 [5 6]]

Compare the original values:

[1, 2, 3, 4, 5, 6]

with the reshaped values:

[[1 2]
 [3 4]
 [5 6]]

The same six values are still there. Only their arrangement has changed.

14

Common Reshaping Mistake

A common mistake is choosing a shape that does not match the number of elements.

numbers = np.array([1, 2, 3, 4, 5, 6])

numbers.reshape(4, 2)

This will fail because:

4 × 2 = 8

Original array = 6 elements

NumPy cannot create 8 positions from only 6 values.

Always check:

rows × columns = total number of elements
15

Reshape Does Not Add or Remove Data

reshape() changes the shape while keeping the same number of elements.

numbers = np.array([1, 2, 3, 4, 5, 6])

numbers.reshape(2, 3)

You still have 6 elements.

If you need to change the number of elements, that is a different operation. Don't confuse changing the shape with changing the actual data.

16

Reshaping Quick Reference

Code Meaning
array.reshape(2, 3) 2 rows and 3 columns
array.reshape(3, 2) 3 rows and 2 columns
array.reshape(1, 6) 1 row and 6 columns
array.reshape(6, 1) 6 rows and 1 column
array.reshape(2, -1) 2 rows, NumPy calculates columns
array.shape Check the current shape
17

Complete Example

Let's put everything together:

import numpy as np

# Create an array
numbers = np.array([10, 20, 30, 40, 50, 60])

# Check original shape
print(numbers.shape)

# Reshape into 2 rows and 3 columns
data = numbers.reshape(2, 3)

# Print the new array
print(data)

# Check the new shape
print(data.shape)
(6,)

[[10 20 30]
 [40 50 60]]

(2, 3)

The data went from:

1D
[10 20 30 40 50 60]

to:

2D
[[10 20 30]
 [40 50 60]]

The six values did not change. Only the structure changed.

KEY TAKEAWAY

Reshaping changes the structure of your data.

Use reshape() when you need to organize an array into a different number of rows, columns, or dimensions. The total number of elements must remain the same. Remember the core rule: rows × columns must equal the total number of elements. In AI, reshaping is especially useful when preparing datasets, features, and image data for models that expect a specific input shape.