Lecture
Array Reshaping and Flattening
NumPy allows you to reshape arrays — adjusting rows and columns while keeping the same data.
You can also flatten multi-dimensional arrays into a single 1D sequence.
Reshaping
Use .reshape(rows, columns) to change the shape of an array.
The total number of elements must remain the same.
Reshape Example
arr = np.array([1, 2, 3, 4, 5, 6]) reshaped = arr.reshape(2, 3) print(reshaped) # [[1 2 3] # [4 5 6]]
Flattening
Use .flatten() to convert any multi-dimensional array into a 1D array.
Flatten Example
matrix = np.array([[1, 2, 3], [4, 5, 6]]) flat = matrix.flatten() print(flat) # [1 2 3 4 5 6]
Summary
- Use
.reshape()to change an array's shape without changing its data - Use
.flatten()to reduce any array to 1D
Previous lessonAggregation Functions (sum, mean, std, etc.)Next lessonGenerating Arrays (arange, linspace, zeros, ones)
Lessons in this chapter · NumPy Essentials for Data Analysis
- 1. What is NumPy and Why Use It?
- 2. Creating 1D and 2D Arrays
- 3. Indexing and Slicing Arrays
- 4. Array Arithmetic and Broadcasting
- 5. Boolean Masking and Filtering
- 6. Array Shapes, Axes, and Broadcasts
- 7. Multiple-choice quiz
- 8. Aggregation Functions (sum, mean, std, etc.)
- 9. Array Reshaping and Flattening
- 10. Generating Arrays (arange, linspace, zeros, ones)
- 11. Data Type Conversion and Copying Arrays
- 12. Working with Multidimensional Arrays
- 13. Fill-in-the-blank quiz
Quiz
0 / 1
Using the reshape method in NumPy changes the actual data in the array.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help