Professional Documents
Culture Documents
In [168]: x=np.random.uniform(0,1,10);x.sort()
% store x
x
In [170]: % store -r
In [171]: (x,y)
1
1.2 b)
In [172]: from random import sample
a=sample(range(0,10),8);
% store a
In [173]: % store -r
In [176]: X_train=x[a];
X_train
In [177]: y_train=y[a];y_train=y_train.reshape(y_train.shape[0],1)
y_train
1.3 c)
In [178]: m=np.zeros((X_train.shape[0],10));
for i in range(0,10):
m[:,i]=X_train**i
M=np.zeros((X_test.shape[0],10));
for i in range(0,10):
M[:,i]=X_test**i
def cost(X,theta,y):
2
return(sum((np.matmul(X,theta)-y)**2)[0]/(2*X.shape[0]))
def gradient_descent(X,theta,y,alpha):
for i in range(0,100000):
J=cost(X,theta,y)
theta1=theta-(alpha/X.shape[0])*(np.matmul(X.T,(model(X,theta)-y)))
J_opt=cost(X,theta1,y)
if(J<=J_opt):
break;
else:
theta=theta1
continue;
print("Number of iterations: ",i);
return(theta)
In [181]: theta_opt1=gradient_descent(X_train_1,theta1,y_train,0.05)
theta_opt1
Out[182]: 0.09511622271301672
In [184]: theta_opt2=gradient_descent(X_train_2,theta2,y_train,0.05)
theta_opt2
Out[185]: 0.059948244666874276
In [187]: theta_opt3=gradient_descent(X_train_3,theta3,y_train,0.05)
theta_opt3
3
Number of iterations: 99999
Out[188]: 0.03232230527392706
In [190]: theta_opt4=gradient_descent(X_train_4,theta4,y_train,0.05)
theta_opt4
Out[191]: 0.017688444243537934
In [193]: theta_opt5=gradient_descent(X_train_5,theta5,y_train,0.05)
theta_opt5
4
Out[194]: 0.016944689569137104
In [196]: theta_opt6=gradient_descent(X_train_6,theta6,y_train,0.05)
theta_opt6
Out[197]: 0.019256817795943935
In [199]: theta_opt7=gradient_descent(X_train_7,theta7,y_train,0.05)
theta_opt7
Out[200]: 0.021328822695781458
In [202]: theta_opt8=gradient_descent(X_train_8,theta8,y_train,0.05)
theta_opt8
5
Number of iterations: 99999
Out[203]: 0.022763509050243616
In [205]: theta_opt9=gradient_descent(X_train_9,theta9,y_train,0.05)
theta_opt9
Out[205]: array([[-0.07949424],
[ 7.2603034 ],
[-9.90234645],
[-7.49645706],
[-2.57160618],
[ 0.99907177],
[ 3.05693338],
[ 4.07070013],
[ 4.45792049],
[ 4.4952223 ]])
Out[206]: 0.023714480158400306
1.4 Question no. 2 Visualization of the dataset and the fitted curves
1.5 a)
In [207]: import matplotlib.pyplot as plt
In [208]: plt.plot(x,y,'o')
6
In [209]: g=np.zeros((x.shape[0],10));
for i in range(0,10):
g[:,i]=x**i
In [210]: g1=g[:,[0,1]];
y1=model(g1,theta_opt1)
plt.plot(x,y,'o')
plt.plot(x,y1,marker='o')
7
In [211]: g2=g[:,[0,1,2]];
y2=model(g2,theta_opt2)
plt.plot(x,y,'o')
plt.plot(x,y2,marker='o')
8
In [212]: g3=g[:,[0,1,2,3]];
y3=model(g3,theta_opt3)
plt.plot(x,y,'o')
plt.plot(x,y3,marker='o')
In [213]: g4=g[:,[0,1,2,3,4]];
y4=model(g4,theta_opt4)
plt.plot(x,y,'o')
plt.plot(x,y4,marker='o')
9
In [214]: g5=g[:,[0,1,2,3,4,5]];
y5=model(g5,theta_opt5)
plt.plot(x,y,'o')
plt.plot(x,y5,marker='o')
10
In [215]: g6=g[:,[0,1,2,3,4,5,6]];
y6=model(g6,theta_opt6)
plt.plot(x,y,'o')
plt.plot(x,y6,marker='o')
In [216]: g7=g[:,[0,1,2,3,4,5,6,7]];
y7=model(g7,theta_opt7)
plt.plot(x,y,'o')
plt.plot(x,y7,marker='o')
11
In [217]: g8=g[:,[0,1,2,3,4,5,6,7,8]];
y8=model(g8,theta_opt8)
plt.plot(x,y,'o')
plt.plot(x,y8,marker='o')
12
In [218]: g9=g[:,[0,1,2,3,4,5,6,7,8,9]];
y9=model(g9,theta_opt9)
plt.plot(x,y,'o')
plt.plot(x,y9,marker='o')
1.6 b)
In [219]: trainerror=[train_error1,train_error2,train_error3,train_error4,train_error5,train_er
testerror=[test_error1,test_error2,test_error3,test_error4,test_error5,test_error6,te
In [220]: n=list(range(1,10))
In [221]: plt.plot(n,trainerror,marker='o')
plt.plot(n,testerror,color='r',marker='o')
plt.xlabel('n'); plt.ylabel('error')
plt.legend(['Training Error','Test Error'])
13
In [225]: test_error4
Out[225]: 0.017688444243537934
In [226]: test_error5
Out[226]: 0.016944689569137104
In [227]: abs(train_error4-test_error4)
Out[227]: 0.007501399085289059
In [228]: abs(train_error5-test_error5)
Out[228]: 0.003355065146649662
1.7 So, from the above plot we can see that at n=5 test error is minimum and the
difference between training and test error is also minimum so n=5 is suitable here.
14