We use reshape() to change the shape of data without changing its values. It helps us organize data in a way that fits our analysis or model.
reshape() for changing dimensions in NumPy
numpy_array.reshape(new_shape)
new_shape is a tuple that defines the new dimensions, like (rows, columns).
One dimension can be -1, which means numpy will calculate it automatically.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) new_arr = arr.reshape((2, 3)) print(new_arr)
import numpy as np arr = np.arange(8) new_arr = arr.reshape((2, -1)) print(new_arr)
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) new_arr = arr.reshape((6,)) print(new_arr)
This program shows how to reshape a 1D array into different 2D shapes. It also demonstrates using -1 to let numpy calculate one dimension automatically.
import numpy as np # Create a 1D array with 12 elements arr = np.arange(12) print('Original array:') print(arr) # Reshape to 3 rows and 4 columns reshaped_arr = arr.reshape((3, 4)) print('\nReshaped to 3x4:') print(reshaped_arr) # Reshape to 2 rows and let numpy decide columns reshaped_auto = arr.reshape((2, -1)) print('\nReshaped to 2 rows and automatic columns:') print(reshaped_auto)
The total number of elements must stay the same when reshaping.
If you use a shape that doesn't match the total elements, numpy will give an error.
Reshape does not change the data, only how it is viewed.
reshape() changes the shape of an array without changing its data.
You can specify new dimensions as a tuple, using -1 for automatic calculation.
Always keep the total number of elements the same when reshaping.
Practice
reshape() function do in NumPy?Solution
Step 1: Understand the purpose of reshape()
Thereshape()function changes the shape (dimensions) of an existing array but keeps the data intact.Step 2: Compare with other options
Options A, B, and C describe different functions or actions unrelated to reshaping.Final Answer:
Changes the shape of an array without changing its data -> Option BQuick Check:
reshape() = change shape only [OK]
- Thinking reshape() changes data values
- Confusing reshape() with sorting or deleting
- Assuming reshape() creates new random arrays
arr into 2 rows and 3 columns?Solution
Step 1: Recall correct reshape syntax
The correct syntax isarray.reshape(dim1, dim2, ...)with dimensions as separate arguments or a tuple.Step 2: Evaluate options
arr.reshape(2, 3) uses correct method call with separate arguments. arr.reshape(2[3]) has invalid syntax (missing comma). arr.reshape{2, 3} uses curly braces which is wrong syntax. reshape(arr, 2, 3) uses a function call without object which is invalid.Final Answer:
arr.reshape(2, 3) -> Option CQuick Check:
Use dot and parentheses with dims [OK]
- Forgetting comma between dimensions
- Using curly braces instead of parentheses
- Calling reshape as a standalone function
import numpy as np arr = np.arange(12) new_arr = arr.reshape(3, 4) print(new_arr.shape)
Solution
Step 1: Understand the original array
np.arange(12)creates a 1D array with 12 elements from 0 to 11.Step 2: Reshape to (3, 4)
The reshape changes the array to 3 rows and 4 columns, so the shape becomes (3, 4).Final Answer:
(3, 4) -> Option AQuick Check:
reshape(3,4) = (3,4) shape [OK]
- Confusing rows and columns order
- Thinking reshape changes total elements
- Expecting original 1D shape after reshape
import numpy as np arr = np.arange(10) new_arr = arr.reshape(3, 4)
Solution
Step 1: Check original array size
np.arange(10)creates an array with 10 elements.Step 2: Check reshape dimensions
Trying to reshape into (3, 4) means 12 elements needed, but only 10 exist.Step 3: Identify error type
This mismatch causes aValueErrorabout incompatible shape.Final Answer:
ValueError: cannot reshape array of size 10 into shape (3,4) -> Option AQuick Check:
Elements must match for reshape [OK]
- Ignoring total element count mismatch
- Expecting reshape to auto-fill missing elements
- Confusing ValueError with SyntaxError
arr = np.arange(24), which of the following reshapes it into a 3D array with shape (2, 3, 4) using automatic dimension calculation with -1 in the first position?Solution
Step 1: Understand total elements and target shape
The array has 24 elements. The target shape is 3D with dimensions (2, 3, 4), total 24 elements.Step 2: Use -1 for automatic dimension
Using-1lets NumPy calculate that dimension automatically. For (2, 3, 4), if we replace one dimension with -1, NumPy calculates it as 24 divided by product of other dims.Step 3: Check each option
arr.reshape(2, 3, 4) is explicit (2,3,4) - works but no automatic calculation.
arr.reshape(2, -1, 4): (2, -1, 4) means -1 = 24/(2*4) = 3, shape (2,3,4) correct.
arr.reshape(-1, 3, 4): (-1, 3, 4) means -1 = 24/(3*4) = 2, shape (2,3,4) correct.
arr.reshape(2, 3, -1): (2, 3, -1) means -1 = 24/(2*3) = 4, shape (2,3,4) correct.
All B, C, D produce correct shape, but question asks for reshaping into (2,3,4) using automatic dimension calculation. Only arr.reshape(-1, 3, 4) uses -1 as first dimension, matching question wording.Final Answer:
arr.reshape(-1, 3, 4) -> Option DQuick Check:
-1 auto calculates dimension [OK]
- Using multiple -1 values in reshape
- Not matching total elements with new shape
- Confusing which dimension -1 replaces
Ready to prove it?
Pass the test and this pattern moves unknown
