-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression.py
More file actions
53 lines (40 loc) · 1.48 KB
/
regression.py
File metadata and controls
53 lines (40 loc) · 1.48 KB
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
import numpy as np
from featureExtractor import FeatureExtractorUtil
import json
import csv
import csv
from extractSubStructures import *
class Regression:
def __init__(self, dataSet):
self.weights = []
self.dataSet = dataSet
extractSubstructures = ExtractSubStructures()
extractSubstructures.createRegressionData(dataSet)
self.features = extractSubstructures.getFeatures()
self.targets = extractSubstructures.getTargets()
self.normalEquations()
def getWeights(self):
return self.weights
def featureMatrix(self):
self.features
def targetMatrix(self):
self.targets
def evaluate(self, theta, x):
return sum([theta[i] * x[i] for i in range(len(x))])
def lr(self, N = 12, alpha = 0.01):
theta = [0] * len(self.features[0])
for k in range(N):
i = k % len(self.features)
h = self.evaluate(theta, self.features[i])
# print("targets {}".format(targets))
# print("i {} hypothesis {}".format(i, h))
error = h - self.targets[i]
for j in range(len(theta)):
theta[j] -= alpha * error * self.features[i][j]
self.weights = theta
def normalEquations(self):
X = np.matmul(np.transpose(self.features), self.features)
Y = np.matmul(np.transpose(self.features), self.targets)
self.weights = np.linalg.solve(X, Y)
r = Regression("Database.txt")
print(r.getWeights())