It changes the shape or dimensions of an existing array without changing its data.
Click to reveal answer
beginner
How do you reshape a 1D array of 6 elements into a 2D array with 2 rows and 3 columns?
Use array.reshape(2, 3) where array is your original 1D array.
Click to reveal answer
intermediate
What happens if you try to reshape an array into a shape that does not match the total number of elements?
NumPy will raise an error because the total number of elements must remain the same.
Click to reveal answer
intermediate
What does using -1 in the reshape dimensions mean?
It tells NumPy to automatically calculate that dimension based on the array size and other given dimensions.
Click to reveal answer
beginner
Can reshape() change the total number of elements in the array?
No, reshape() only changes the shape, not the total number of elements.
Click to reveal answer
What will np.array([1,2,3,4,5,6]).reshape(3,2) produce?
AA 1D array with 6 elements
BA 3x2 array with elements [[1,2],[3,4],[5,6]]
CAn error because the shape is invalid
DA 2x3 array with elements [[1,2,3],[4,5,6]]
✗ Incorrect
The reshape changes the 1D array into 3 rows and 2 columns.
What does reshape(-1, 1) do to a 1D array?
ARaises an error
BConverts it to a 2D row vector
CFlattens the array
DConverts it to a 2D column vector
✗ Incorrect
Using -1 lets NumPy calculate the number of rows, making it a column vector.
If an array has 12 elements, which reshape is invalid?
Areshape(5,3)
Breshape(3,4)
Creshape(2,6)
Dreshape(4,3)
✗ Incorrect
5x3=15 which does not match 12 elements, so it is invalid.
What must remain the same before and after using reshape()?
AThe data type
BThe shape of the array
CThe total number of elements
DThe memory address
✗ Incorrect
Reshape only changes dimensions, not the total number of elements.
Which of these is a valid way to reshape an array arr with 8 elements?
Aarr.reshape(2, 4)
Barr.reshape(3, 3)
Carr.reshape(4, 3)
Darr.reshape(5, 2)
✗ Incorrect
2x4=8 matches the total elements, so it is valid.
Explain how the reshape() function works in NumPy and why the total number of elements must stay the same.
Think about how you can rearrange items in boxes without adding or removing items.
You got /4 concepts.
Describe how using -1 in reshape helps when changing array dimensions.
It’s like telling NumPy: 'You figure out this size for me.'
You got /4 concepts.
Practice
(1/5)
1. What does the reshape() function do in NumPy?
easy
A. Creates a new array with random values
B. Changes the shape of an array without changing its data
C. Sorts the elements of an array
D. Deletes elements from an array
Solution
Step 1: Understand the purpose of reshape()
The reshape() 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 B
Quick Check:
reshape() = change shape only [OK]
Hint: reshape() changes shape, data stays same [OK]
Common Mistakes:
Thinking reshape() changes data values
Confusing reshape() with sorting or deleting
Assuming reshape() creates new random arrays
2. Which of the following is the correct syntax to reshape a NumPy array arr into 2 rows and 3 columns?
easy
A. arr.reshape(2[3])
B. arr.reshape{2, 3}
C. arr.reshape(2, 3)
D. reshape(arr, 2, 3)
Solution
Step 1: Recall correct reshape syntax
The correct syntax is array.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 C
Quick Check:
Use dot and parentheses with dims [OK]
Hint: Use dot and parentheses with dims, no brackets [OK]
Common Mistakes:
Forgetting comma between dimensions
Using curly braces instead of parentheses
Calling reshape as a standalone function
3. What is the output shape of the following code?
A. ValueError: cannot reshape array of size 10 into shape (3,4)
B. TypeError: reshape() takes exactly 1 argument
C. SyntaxError: invalid syntax
D. No error, reshaping works fine
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 a ValueError about incompatible shape.
Final Answer:
ValueError: cannot reshape array of size 10 into shape (3,4) -> Option A
Quick Check:
Elements must match for reshape [OK]
Hint: Total elements must match for reshape [OK]
Common Mistakes:
Ignoring total element count mismatch
Expecting reshape to auto-fill missing elements
Confusing ValueError with SyntaxError
5. Given a 1D NumPy array 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?
hard
A. arr.reshape(2, 3, 4)
B. arr.reshape(2, -1, 4)
C. arr.reshape(2, 3, -1)
D. arr.reshape(-1, 3, 4)
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 -1 lets 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 D
Quick Check:
-1 auto calculates dimension [OK]
Hint: Use -1 for one dimension to auto calculate [OK]
Common Mistakes:
Using multiple -1 values in reshape
Not matching total elements with new shape
Confusing which dimension -1 replaces
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.