Matrix Transpose Practice Problem
This data science coding problem helps you practice Linear Algebra with NumPy, matrix transpose, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Linear Algebra with NumPy.
- Problem ID: 66
- Problem key: 66-matrix-transpose
- URL: https://datacrack.app/solve/66-matrix-transpose
- Difficulty: easy
- Topic: Linear Algebra with NumPy
- Module: NumPy Foundations
Problem Statement
# 🧩 Matrix Transpose
---
### 🎯 Goal
The **transpose** flips a matrix over its diagonal — rows become columns and columns become rows. It is used in virtually every matrix computation: gradient formulas, covariance matrices, weight updates in backpropagation, and the closed-form solution of linear regression.
---
### 🔍 What Transpose Does
$$
A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}_{2 \times 3}
\quad \Rightarrow \quad
A^T = \begin{pmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{pmatrix}_{3 \times 2}
$$
Shape rule: $(m, n)^T = (n, m)$
---
### 💻 Task
Implement `transpose(matrix)` using `arr.T`.
---
### 📥 Input
- `matrix`: 2D list of shape (m, n)
### 📤 Output
- Transposed matrix as a nested Python list, shape (n, m)
---
### 🧩 Starter Code
```python
import numpy as np
def transpose(matrix):
"""
Compute the transpose of a matrix.
Args:
matrix (list[list]): 2D input matrix of shape (m, n)
Returns:
list[list]: Transposed matrix of shape (n, m)
"""
arr = np.array(matrix)
# 🧠 TODO: Use arr.T to get the transpose, then return .tolist()
pass
```
---
### 💡 Example
```python
transpose([[1, 2, 3], [4, 5, 6]])
# Expected: [[1, 4], [2, 5], [3, 6]]
```
---
### 🔑 Key Concepts
- `arr.T` — the transpose attribute (no copy, just a view with swapped strides)
- `np.transpose(arr)` — equivalent function
- A symmetric matrix satisfies `A == A.T`Starter Code
import numpy as np
def transpose(matrix):
"""
Compute the transpose of a matrix.
Args:
matrix (list[list]): 2D input matrix of shape (m, n)
Returns:
list[list]: Transposed matrix of shape (n, m)
"""
arr = np.array(matrix)
# 🧠 TODO: Use arr.T to get the transpose, then return .tolist()
pass