forked from msraghavganesh/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear reg.py
35 lines (30 loc) · 1.25 KB
/
Linear reg.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
29
30
31
32
33
34
35
import numpy as np
from numpy.linalg import inv
### 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
"""
# YOUR CODE HERE
try:
n = len(X[0])
I = np.identity(n)
A = inv(np.transpose(X) @ X + lambda_factor * I)
theta = A @ (np.transpose(X) @ Y)
return theta
except:
raise NotImplementedError
### Functions which are already complete, for you to 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)