-
Notifications
You must be signed in to change notification settings - Fork 0
/
lr.py
106 lines (85 loc) · 3.41 KB
/
lr.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import division, print_function
from typing import List
import numpy
import scipy
class LinearRegression:
def __init__(self, nb_features: int):
self.nb_features = nb_features
self.weights = []
def train(self, features: List[List[float]], values: List[float]):
modifiedFeatures=[]
for i in range(len(features)):
modifiedFeature=[]
currentFeature=features[i]
modifiedFeature.append(1)
for j in range(len(currentFeature)):
modifiedFeature.append(currentFeature[j])
modifiedFeatures.append(modifiedFeature)
xMatrix=numpy.array(modifiedFeatures)
xMatrixTranspose=xMatrix.transpose()
product=numpy.matmul(xMatrixTranspose, xMatrix)
inverse=numpy.linalg.inv(product)
product2=numpy.matmul(inverse, xMatrixTranspose)
yVector=numpy.array(values)
product3=numpy.matmul(product2, yVector)
self.weights=product3.tolist()
def predict(self, features: List[List[float]]) -> List[float]:
predictedValues=[]
for i in range(len(features)):
predictedValue=self.weights[0]
currentFeature=features[i]
for j in range(len(currentFeature)):
predictedValue=predictedValue+(currentFeature[j]*self.weights[j+1])
predictedValues.append(predictedValue)
return predictedValues
def get_weights(self) -> List[float]:
"""
for a model y = 1 + 3 * x_0 - 2 * x_1,
the return value should be [1, 3, -2].
"""
return self.weights
class LinearRegressionWithL2Loss:
''' L2 loss for weight regularization'''
def __init__(self, nb_features: int, alpha: float):
self.alpha = alpha
self.nb_features = nb_features
self.weights = []
def train(self, features: List[List[float]], values: List[float]):
modifiedFeatures=[]
for i in range(len(features)):
modifiedFeature=[]
currentFeature=features[i]
modifiedFeature.append(1)
for j in range(len(currentFeature)):
modifiedFeature.append(currentFeature[j])
modifiedFeatures.append(modifiedFeature)
xMatrix=numpy.array(modifiedFeatures)
xMatrixTranspose=xMatrix.transpose()
product=numpy.matmul(xMatrixTranspose, xMatrix)
dim=product.shape[0]
identity=numpy.identity(dim)
product4=self.alpha*identity
summed=numpy.add(product, product4)
inverse=numpy.linalg.inv(summed)
product2=numpy.matmul(inverse, xMatrixTranspose)
yVector=numpy.array(values)
product3=numpy.matmul(product2, yVector)
self.weights=product3.tolist()
def predict(self, features: List[List[float]]) -> List[float]:
predictedValues=[]
for i in range(len(features)):
predictedValue=self.weights[0]
currentFeature=features[i]
for j in range(len(currentFeature)):
predictedValue=predictedValue+(currentFeature[j]*self.weights[j+1])
predictedValues.append(predictedValue)
return predictedValues
def get_weights(self) -> List[float]:
"""
for a model y = 1 + 3 * x_0 - 2 * x_1,
the return value should be [1, 3, -2].
"""
return self.weights
if __name__ == '__main__':
print(numpy.__version__)
print(scipy.__version__)