-
Notifications
You must be signed in to change notification settings - Fork 5
/
linear_regression.py
28 lines (24 loc) · 1.09 KB
/
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
23
24
25
26
27
28
import numpy as np
### Functions for you to fill in ###
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
"""
# X.T => transpose of matrix X
A = (np.dot(X.T,X) + lambda_factor*np.identity(X.shape[1]))/X.shape[0]
b = np.dot(X.T,Y)/X.shape[0]
return np.dot(np.linalg.pinv(A),b)
### Functions for use ###
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)