Fix the Reshape Bug
Intermediate · Fix the bug · from NumPy Arrays: Create, Index, Slice, and Reshape
From the lesson: NumPy Arrays: Create, Index, Slice, and Reshape
Imagine you have a spreadsheet with a million rows of numbers and you need to multiply every single value by 2. With a regular Python list, you'd write a loop that visits each cell one by one. It works, but it's slow.
Read the lessonThis code tries to create a 3x5 grid of numbers but crashes with an error. Fix the bug so it prints a 3x5 array.
import numpy as np
# BUG: This crashes!
data = np.arange(1, 16)
grid = data.reshape(3, 5)
print(grid)
More NumPy Array exercises
- Create Your First NumPy ArrayIntermediate
- Array Factory FunctionsIntermediate
- Array IndexingIntermediate
- Slice and DiceIntermediate
- Predict the ShapeIntermediate
- Matrix ExtractionIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz