-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_lib.py
More file actions
42 lines (34 loc) · 1.6 KB
/
test_lib.py
File metadata and controls
42 lines (34 loc) · 1.6 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
from polynomial_regression import PolynomialRegression
import random
import numpy as np # numpy is used only for test data generation
import assignment_dataset as Dataset
def getPredictions(coefficients, independent):
predictionList = []
for sampleIndex in range(len(independent)):
prediction = 0
for index in range(len(coefficients)):
prediction = prediction + coefficients[index] * pow(independent[sampleIndex], index)
predictionList.append(prediction)
return predictionList
def generateDataSet(coefficient=[], addError=False):
independentList = np.arange(0, 50, 1.5)
dependentList = []
for value in independentList:
dependent = 0
for index in range(len(coefficient)):
dependent = dependent + coefficient[index] * pow(value, index)
if addError:
dependent = dependent + random.randint(-100, 100)
dependentList.append(dependent)
return independentList, dependentList
def fitLineAndPlot(independentList, dependentList, plt, order=1):
regression = PolynomialRegression(order)
coefficient = regression.fit(independentList, dependentList)
print('Coefficients for order %d' % order, coefficient)
predictionList = getPredictions(coefficient, independentList)
plt.plot(independentList, predictionList, label='Order-%d' % order, linewidth=3)
plt.legend()
def drawScatterPlot(independentList, dependentList, plt):
plt.scatter(independentList, dependentList, s=100)
def getAssignmentDataset():
return Dataset.assignmentIndependentList, Dataset.assignmentDependentList