East West University: Computer Science and Engineering
East West University: Computer Science and Engineering
Fall 2020
CSE475
Assignment 1
Submitted to:
Submitted by:
Shishir Zaman
ID:2017-2-60-141
import pandas as pd
import numpy as np
#creating dataset D2
x1 = [3,2,1,3]
x2 = [2,5,3,5]
x3 = [1,3,5,7]
x4 = [4,7,7,7]
y = [3,1,7,5]
data = pd.DataFrame({
"x1" : x1,
"x2" : x2,
"x3" : x3,
"x4" : x4,
"y" : y
})
# iteration
k=0
predict_w0 = []
predict_w1 = []
predict_w2 = []
predict_w3 = []
predict_w4 = []
while(k<iteration):
pw0 = 0
pw1 = 0
pw2 = 0
pw3 = 0
pw4 = 0
for i in range(len(data)):
p_y = w0 + w1*data['x1'][i] + w2*data['x2'][i] + w3*data['x3'][i] + w4*data['x4'][i]
pw0 += (p_y - data['y'][i])
pw1 += ((p_y - data['y'][i])*data['x1'][i])
pw2 += ((p_y - data['y'][i])*data['x2'][i])
pw3 += ((p_y - data['y'][i])*data['x3'][i])
pw4 += ((p_y - data['y'][i])*data['x4'][i])
w0 = w0 - (alpha*pw0)
w1 = w1 - (alpha*pw1)
w2 = w2 - (alpha*pw2)
w3 = w3 - (alpha*pw3)
w4 = w4 - (alpha*pw4)
predict_w0.append(w0)
predict_w1.append(w1)
predict_w2.append(w2)
predict_w3.append(w3)
predict_w4.append(w4)
k +=1
weights = pd.DataFrame({
'W0' : predict_w0,
'W1' : predict_w1,
'W2' : predict_w2,
'W3' : predict_w3,
'W4' : predict_w4
})
Result:
W0 W1 W2 W3 W4
W0 W1 W2 W3 W4
W0 W1 W2 W3 W4
W0 W1 W2 W3 W4
W0 W1 W2 W3 W4