Use reshape() to change the shape of an array without changing its data. Use ravel() or flatten() to collapse an N-dimensional array into a 1D vector.
NumPy Reshaping and Ravel
Change array dimensions and flatten multi-dimensional arrays.
Key Takeaways: NumPy Reshaping and Ravel
Mastering NumPy Reshaping and Ravel helps you write clean and reliable NumPy code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
NumPy Reshaping and Ravel Code Example
NumPyimport numpy as np
arr = np.arange(1, 7)
reshaped = arr.reshape((2, 3))
print("Reshaped 2x3:\n", reshaped)
print("Flattened back:", reshaped.ravel())
Output
Reshaped 2x3: [[1 2 3] [4 5 6]] Flattened back: [1 2 3 4 5 6]
Free sign-in required · Google login available
Finished this lesson?
Mark it complete to track your progress.
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services