-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression.py
22 lines (19 loc) · 956 Bytes
/
linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
def closed_form(X, Y, lambda_factor):
"""
Computes the closed form solution of linear regression with L2 regularization
Args:
X - (n, d + 1) NumPy array (n datapoints each with d features plus the bias feature in the first dimension)
Y - (n, ) NumPy array containing the labels (a number from 0-9) for each
data point
lambda_factor - the regularization constant (scalar)
Returns:
theta - (d + 1, ) NumPy array containing the weights of linear regression. Note that theta[0]
represents the y-axis intercept of the model and therefore X[0] = 1
"""
return np.linalg.inv(X.T @ X + lambda_factor*np.identity(X.shape[1])) @ X.T @ Y
def compute_test_error_linear(test_x, Y, theta):
test_y_predict = np.round(np.dot(test_x, theta))
test_y_predict[test_y_predict < 0] = 0
test_y_predict[test_y_predict > 9] = 9
return 1 - np.mean(test_y_predict == Y)