Skip to content

Commit 05d28b2

Browse files
committed
Refactor linear regression weight calculation method
- Updated the weight calculation in the LinearRegression class to use the analytical solution with the normal equation instead of the Moore-Penrose pseudoinverse. - Modified the exercise documentation to clarify the implications of using the pseudoinverse and the conditions under which it may fail, enhancing the educational context for users.
1 parent 977ebab commit 05d28b2

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/codes/05-machine_learning/exercise_05.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def __init__(self):
77
self.weights = None
88

99
def fit(self, X, y):
10-
self.weights = np.linalg.pinv(X) @ y
10+
# self.weights = np.linalg.pinv(X) @ y
11+
self.weights = np.linalg.inv(X.T @ X) @ X.T @ y
1112

1213
def predict(self, X):
1314
return X @ self.weights

src/psets/04.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@ In the lecture, we performed linear regression to predict the fluorescence inten
88

99
**(a) Linear Regression with Morgan Fingerprints**
1010

11-
Use your object-oriented implementation of linear regression to perform linear regression on the dataset using Morgan fingerprints as features. To reduce the number of features, you can use `pandas` to drop all columns that have a standard deviation of 0, as they are not informative. Compute the mean absolute error (MAE) and plot the predicted vs. actual fluorescence intensity.
11+
Use your object-oriented implementation of linear regression to perform linear regression on the dataset using Morgan fingerprints as features. What do you observe?
12+
13+
**(b) Moore-Penrose Pseudoinverse**
14+
15+
You will see, that `numpy` will probably throw an error or the form `numpy.linalg.LinAlgError: Singular matrix`, indicating that the matrix $\bm{X}^T \bm{X}$ is not invertible. This means that the columns of $\bm{X}$ are linearly dependent, i.e., you can express one column as a linear combination of the other columns.
16+
17+
To solve this problem, we can use the Moore-Penrose pseudoinverse $\mathbf{X}^+$, which you have got to know in the lecture on SVD. Show that the analytical solution for the weights for linear regression
18+
19+
$$
20+
\vec{w} = (\bm{X}^T \bm{X})^{-1} \bm{X}^T \bm{y}
21+
$$
22+
23+
is equivalent to:
24+
25+
$$
26+
\vec{w} = \bm{X}^+ \bm{y}
27+
$$
28+
29+
Then use `np.linalg.pinv` to compute the pseudoinverse and use it to compute the weights for linear regression. Compute the MAE and plot the predicted vs. actual fluorescence intensity.
1230

1331
How severe do you estimate the degree of overfitting to be? Consider the number of weights in relation to the number of data points.
1432

1533
**(b) Ridge Regression**
1634

17-
Now perform ridge regression using the same dataset. Remember that ridge regression is linear regression with a regularization term added to the loss function:
35+
To reduce the risk of overfitting, we can use ridge regression. Remember that ridge regression is linear regression with a regularization term added to the loss function:
1836

1937
$$
2038
\mathcal{L} = \frac{1}{2} \sum_{i=1}^{N} (y_i - \hat{f}(\vec{x}_i))^2 + \frac{\lambda}{2} \|\vec{w}\|^2

0 commit comments

Comments
 (0)