CodeBlanks Logo Icon CodeBlanks

Transpose

In a matrix transpose, rows and columns are swapped.

Transpose

The transpose turns a matrix's rows into columns: (m, n) → (n, m). In NumPy it is written .T.

A = np.array([[1, 2, 3],
              [4, 5, 6]])
print(A.T)        # [[1 4] [2 5] [3 6]]
print(A.T.shape)  # (3, 2)

When shapes don't fit in a matrix multiplication, the fix is often to take the transpose of one side.

XᵀX and the normal equation

The closed-form solution of linear regression is written with the transpose: w = (XᵀX)⁻¹ Xᵀy.

X = np.array([[1, 1], [1, 2], [1, 3]])   # first column: 1 for the intercept
y = np.array([3, 5, 7])
w = np.linalg.solve(X.T @ X, X.T @ y)
print(w)   # [1. 2.]  ->  y = 1 + 2x

np.linalg.solve is more stable and faster than taking the inverse explicitly. scikit-learn's LinearRegression also solves it in a similar way.

Practice Normal equation

X with a first column of 1s and y are given. Find w with np.linalg.solve(X.T @ X, X.T @ y) and print it.

Starter code

model.py
import numpy as np

X = np.array([[1, 0], [1, 1], [1, 2], [1, 3]])
y = np.array([4, 7, 10, 13])

# buraya yaz

Sample solution

model.py
import numpy as np

X = np.array([[1, 0], [1, 1], [1, 2], [1, 3]])
y = np.array([4, 7, 10, 13])

w = np.linalg.solve(X.T @ X, X.T @ y)
print(w)

Expected output

[4. 3.]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 For X of shape (5, 3), what is the shape of <code>X.T @ X</code>?

  • An error
  • (3, 3) Correct answer
  • (5, 5)
  • (5, 3)

Explanation: (3, 5) @ (5, 3) = (3, 3); it is a matrix describing the relationships between features.

2 Take the transpose of the matrix.

Question
At = A.____
Answer
At = A.T

Explanation: .T is the same as numpy.transpose(A).

3 Match the expressions.

Term Meaning
A.T Transpose
X.T @ X Feature × feature matrix
np.linalg.solve Linear system solution
(A.T).T A itself

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation