This program creates a 2-row, 3-column array and then swaps rows and columns using transpose(). The output shows the original and transposed arrays.
NumPy
import numpy as np
# Create a 2D array
array_2d = np.array([[10, 20, 30], [40, 50, 60]])
print("Original array:")
print(array_2d)
# Transpose the array
transposed_array = np.transpose(array_2d)
print("\nTransposed array:")
print(transposed_array)
OutputSuccess
Important Notes
For 2D arrays, transpose() swaps rows and columns.
For arrays with more dimensions, use the axes parameter to control how dimensions are reordered.
Transposing does not change the data, only how it is viewed or accessed.
Summary
transpose() flips or swaps the axes of an array.
It is useful to change the shape or orientation of data.
Use the axes parameter to customize the swap for multi-dimensional arrays.
Practice
(1/5)
1. What does the numpy.transpose() function do to an array?
easy
A. It removes duplicate elements from the array.
B. It adds new elements to the array.
C. It sorts the array elements in ascending order.
D. It swaps or flips the axes of the array.
Solution
Step 1: Understand the purpose of transpose()
The transpose() function changes the order of axes in an array, effectively flipping or swapping them.
Step 2: Compare with other options
Adding elements, sorting, or removing duplicates are unrelated to axis swapping.
Final Answer:
It swaps or flips the axes of the array. -> Option D
Quick Check:
transpose() = swaps axes [OK]
Hint: Remember: transpose flips axes, not values or elements [OK]
Common Mistakes:
Thinking transpose sorts or changes values
Confusing transpose with adding or removing elements
Assuming transpose only works on 2D arrays
2. Which of the following is the correct syntax to transpose a 2D NumPy array named arr?
easy
A. transpose(arr)
B. arr.transpose
C. arr.transpose()
D. arr.transpose[]
Solution
Step 1: Recall method call syntax in Python
To call a method, parentheses are required: arr.transpose().
Step 2: Check other options
transpose(arr) is incorrect because transpose is a method, not a standalone function here; arr.transpose misses parentheses; arr.transpose[] is invalid syntax.
Final Answer:
arr.transpose() -> Option C
Quick Check:
Method call needs parentheses [OK]
Hint: Use parentheses to call transpose method: arr.transpose() [OK]
Common Mistakes:
Omitting parentheses when calling transpose
Using transpose as a standalone function without import
Transpose swaps axes, so shape becomes (3, 2). Rows become columns and vice versa. The first column becomes the first row: [1,4], second: [2,5], third: [3,6].
Final Answer:
[[1 4]
[2 5]
[3 6]] -> Option A
Quick Check:
Transpose swaps rows and columns [OK]
Hint: Transpose flips rows and columns in 2D arrays [OK]
Common Mistakes:
Confusing transpose with reversing elements
Expecting original shape after transpose
Mixing up rows and columns in output
4. The following code throws an error. What is the mistake?
import numpy as np
arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]])
print(arr.transpose(1, 0))
medium
A. transpose() requires all axes to be specified for 3D arrays.
B. The array is not a NumPy array.
C. transpose() cannot be used on 3D arrays.
D. The axes argument must be a list, not a tuple.
Solution
Step 1: Check array dimensions
The array has 3 dimensions (shape (2, 2, 2)).
Step 2: Understand transpose axes argument
For 3D arrays, transpose requires all three axes to be specified. Passing only two axes (1, 0) causes an error.
Final Answer:
transpose() requires all axes to be specified for 3D arrays. -> Option A
Quick Check:
All axes must be given for 3D transpose [OK]
Hint: Specify all axes when transposing multi-dimensional arrays [OK]
Common Mistakes:
Passing fewer axes than array dimensions
Thinking transpose can't handle 3D arrays
Confusing tuple and list for axes argument
5. Given a 3D NumPy array arr with shape (2, 3, 4), which transpose() call will swap the first and last axes, resulting in shape (4, 3, 2)?
hard
A. arr.transpose(2, 0, 1)
B. arr.transpose(2, 1, 0)
C. arr.transpose(0, 2, 1)
D. arr.transpose(1, 0, 2)
Solution
Step 1: Identify original axes order
Original shape (2, 3, 4) corresponds to axes (0, 1, 2).
Step 2: Determine desired axes order
Swapping first and last axes means new order is (2, 1, 0), resulting in shape (4, 3, 2).
Final Answer:
arr.transpose(2, 1, 0) -> Option B
Quick Check:
Swap axes 0 and 2: (2,1,0) [OK]
Hint: List axes in desired order to swap them correctly [OK]
Common Mistakes:
Mixing up axes order in transpose
Not specifying all axes for 3D arrays
Assuming transpose swaps only two axes automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.