-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_MinRSS.py
More file actions
36 lines (28 loc) · 927 Bytes
/
Copy path2_MinRSS.py
File metadata and controls
36 lines (28 loc) · 927 Bytes
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
import numpy as np
X= np.array([0,1,2,3,4])
y= np.array([1,3,7,13,21])
# initialize parameters and keep w0 constant
# we want to visualise RSS with respect to one dimension of parameter
# for 2 dimension that is for both w0 and w1, RSS will become like mesh grid
w0=0.
w1=-40.
RSS= {} # keys will be RSS and values will be w1 i.e. slope
epochs= 50
w1steps=2.
for epoch in range (epochs):
SumSqErrors = 0
for i in range(len(X)): # to loop over all data points to calculate sum of square of error
y_predicted = w0 + w1 * X[i]
error= y[i] - y_predicted
SqError= error**2
SumSqErrors += SqError
# add this Error sq sum as key and corresponding parameters (w0,w1) as values
RSS[SumSqErrors] = w1
# chnage w1
w1= w1 + w1steps
import matplotlib.pyplot as plt
plt.scatter(RSS.values(),RSS.keys())
plt.xlabel('w1')
plt.ylabel('RSS value')
plt.show()
print(RSS.keys())