Bird
Raised Fist0
NumPydata~10 mins

Matrix transpose operations in NumPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Matrix transpose operations
Start with matrix A
↓
Access elements by rows and columns
↓
Swap rows with columns
↓
Create new matrix A^T with swapped indices
↓
Output transposed matrix
Transpose flips a matrix over its diagonal, swapping rows and columns to create a new matrix.
Execution Sample
NumPy
import numpy as np
A = np.array([[1, 2, 3],
              [4, 5, 6]])
A_T = A.T
print(A_T)
This code creates a 2x3 matrix A, transposes it to 3x2, and prints the result.
Execution Table
StepActionMatrix ShapeMatrix Content
1Create matrix A(2, 3)[[1, 2, 3], [4, 5, 6]]
2Access element A[0,1]N/A2
3Access element A[1,2]N/A6
4Transpose matrix A to A.T(3, 2)[[1, 4], [2, 5], [3, 6]]
5Print transposed matrix A.T(3, 2)[[1, 4], [2, 5], [3, 6]]
💡 All elements swapped from rows to columns, transpose complete.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
Aundefined[[1, 2, 3], [4, 5, 6]][[1, 2, 3], [4, 5, 6]][[1, 2, 3], [4, 5, 6]]
A_Tundefinedundefined[[1, 4], [2, 5], [3, 6]][[1, 4], [2, 5], [3, 6]]
Key Moments - 3 Insights
Why does the shape change from (2, 3) to (3, 2) after transpose?
Because transpose swaps rows and columns, so 2 rows and 3 columns become 3 rows and 2 columns as shown in execution_table step 4.
Is the original matrix A changed after transpose?
No, the original matrix A stays the same; transpose creates a new matrix A_T as shown in variable_tracker.
How do elements move during transpose?
Each element at position (i, j) in A moves to position (j, i) in A_T, demonstrated by element 2 at A[0,1] moving to A_T[1,0].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shape of A_T after step 4?
A(2, 2)
B(2, 3)
C(3, 2)
D(3, 3)
💡 Hint
Check the 'Matrix Shape' column at step 4 in the execution_table.
At which step is the element 6 accessed in the original matrix A?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for element access in execution_table.
If matrix A was square (3x3), how would the shape of A_T compare to A?
AA_T would have shape (3, 2)
BA_T would have shape (3, 3), same as A
CA_T would have shape (2, 3)
DA_T would have shape (2, 2)
💡 Hint
Transpose swaps rows and columns; for square matrices, shape stays the same.
Concept Snapshot
Matrix Transpose:
- Syntax: A.T or np.transpose(A)
- Swaps rows and columns
- Changes shape from (m, n) to (n, m)
- Original matrix unchanged
- Useful for matrix operations and alignment
Full Transcript
Matrix transpose flips a matrix over its diagonal, swapping rows and columns. Starting with matrix A of shape (2, 3), each element is accessed by its row and column indices. Transpose creates a new matrix A_T where rows become columns and columns become rows, changing shape to (3, 2). The original matrix A remains unchanged. This operation is done using A.T in numpy. The execution table shows each step including element access and the final transposed matrix. Key points include shape change, element position swap, and original matrix preservation.

Practice

(1/5)
1. What does the .T attribute do when applied to a NumPy matrix?
easy
A. It flips the rows and columns of the matrix (transpose).
B. It multiplies all elements by 2.
C. It returns the sum of all elements.
D. It flattens the matrix into a 1D array.

Solution

  1. Step 1: Understand the .T attribute

    The .T attribute in NumPy returns the transpose of a matrix, which means rows become columns and columns become rows.
  2. Step 2: Compare with other options

    Multiplying by 2, summing elements, or flattening are different operations and not related to .T.
  3. Final Answer:

    It flips the rows and columns of the matrix (transpose). -> Option A
  4. Quick Check:

    Transpose = flip rows and columns [OK]
Hint: Remember: .T flips rows and columns [OK]
Common Mistakes:
  • Confusing transpose with flattening
  • Thinking .T multiplies elements
  • Assuming .T sums elements
2. Which of the following is the correct syntax to transpose a NumPy array named arr?
easy
A. arr.transpose
B. arr.T()
C. transpose(arr)
D. arr.T

Solution

  1. Step 1: Recall NumPy transpose syntax

    In NumPy, .T is an attribute, not a method, so it does not use parentheses.
  2. Step 2: Evaluate each option

    arr.transpose() uses transpose() method which exists but is a method, so requires parentheses. arr.T() incorrectly uses .T() as a method. transpose(arr) is not valid syntax. arr.T correctly uses .T attribute without parentheses.
  3. Final Answer:

    arr.T -> Option D
  4. Quick Check:

    Transpose attribute = arr.T [OK]
Hint: Use .T without parentheses to transpose [OK]
Common Mistakes:
  • Using .T() as if it were a method
  • Confusing transpose() method with .T attribute
  • Trying to call transpose(arr) without import
3. What is the output of the following code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.T)
medium
A. [[1 3] [2 4]]
B. [[1 2] [3 4]]
C. [[4 3] [2 1]]
D. [[1 4] [2 3]]

Solution

  1. Step 1: Understand the original array

    The array arr is a 2x2 matrix: [[1, 2], [3, 4]].
  2. Step 2: Apply transpose operation

    Transposing swaps rows and columns, so the first row [1, 2] becomes the first column, and the second row [3, 4] becomes the second column. Result: [[1, 3], [2, 4]].
  3. Final Answer:

    [[1 3] [2 4]] -> Option A
  4. Quick Check:

    Transpose swaps rows and columns = [[1 3] [2 4]] [OK]
Hint: Transpose swaps rows and columns positions [OK]
Common Mistakes:
  • Confusing transpose with reversing elements
  • Mixing rows and columns incorrectly
  • Expecting original array output
4. Identify the error in this code snippet that tries to transpose a NumPy array:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
transposed = arr.T()
print(transposed)
medium
A. Array creation syntax is wrong.
B. Using parentheses with .T attribute causes an error.
C. Missing import statement for numpy.
D. print() function is used incorrectly.

Solution

  1. Step 1: Check usage of .T

    The .T attribute is not a function, so it should not have parentheses. Using .T() causes a TypeError.
  2. Step 2: Verify other parts of the code

    Array creation and import are correct. The print statement is also correct.
  3. Final Answer:

    Using parentheses with .T attribute causes an error. -> Option B
  4. Quick Check:

    .T is attribute, not method [OK]
Hint: Do not add () after .T attribute [OK]
Common Mistakes:
  • Calling .T as a function with ()
  • Assuming .T needs import
  • Thinking print syntax is wrong
5. You have a 3x2 NumPy array arr = np.array([[1, 2], [3, 4], [5, 6]]). You want to multiply it by another array so that the result is a 2x2 matrix. Which of the following correctly uses transpose to achieve this multiplication?
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
arr2 = ???
result = np.dot(arr.T, arr2)
print(result)
hard
A. arr2 = np.array([[1, 0, 1], [0, 1, 1]])
B. arr2 = np.array([[1, 0], [0, 1]])
C. arr2 = np.array([[1, 0], [0, 1], [1, 1]])
D. arr2 = np.array([[1, 0], [0, 1], [1, 1], [0, 0]])

Solution

  1. Step 1: Understand shapes of arrays

    arr is 3x2. Its transpose arr.T is 2x3. For matrix multiplication np.dot(arr.T, arr2), the number of rows in arr2 must be 3 to match arr.T's columns.
  2. Step 2: Check options for correct shape

    arr2 = np.array([[1, 0], [0, 1], [1, 1]]) shape is 3x2, so arr.T (2x3) dot arr2 (3x2) results in 2x2 matrix, which is valid. arr2 = np.array([[1, 0], [0, 1]]) shape is 2x2, which does not match for multiplication with 2x3. arr2 = np.array([[1, 0, 1], [0, 1, 1]]) shape is 2x3, which does not match. arr2 = np.array([[1, 0], [0, 1], [1, 1], [0, 0]]) shape is 4x2, invalid.
  3. Step 3: Re-examine arr2 = np.array([[1, 0], [0, 1]]) carefully

    Actually, arr2 = np.array([[1, 0], [0, 1]]) is 2x2, which cannot multiply with 2x3. So arr2 = np.array([[1, 0], [0, 1], [1, 1]]) is correct.
  4. Final Answer:

    arr2 = np.array([[1, 0], [0, 1], [1, 1]]) -> Option C
  5. Quick Check:

    Matrix shapes must align for dot product [OK]
Hint: Check matrix shapes before multiplying [OK]
Common Mistakes:
  • Ignoring shape mismatch in dot product
  • Confusing rows and columns after transpose
  • Choosing arrays with incompatible shapes