K Fold Cross Validation: Problem6 Part1
K Fold Cross Validation: Problem6 Part1
K Fold Cross Validation: Problem6 Part1
In [2]: data=np.genfromtxt('data1.csv',delimiter=',',dtype=float)
m,n=data.shape
c=np.ones((m,1))
data=np.hstack((c,data))
m,n=data.shape
def gradient(X,y,omega,alpha,iter_num):
for i in range(iter_num):
m,n=X.shape
A=np.array(X.dot(omega)-y)
omega=omega-((X.T).dot(A))*(alpha/m)
#J=costFunction(X,y,omega)
#plt.scatter(i,J)
return omega
1 of 5 20/08/19, 2:01 am
problem6 part1 http://localhost:8891/nbconvert/html/lab_assign...
In [13]: k=5
p=m//5
k1=data[0:p,:]
k2=data[p:2*p,:]
k3=data[2*p:3*p,:]
k4=data[3*p:4*p,:]
k5=data[4*p:5*p,:]
a=[k1,k2,k3,k4,k5]
a1=[]
for alpha in np.linspace(.1,1,10):
MSE1=[]
for i in range(k):
b=np.zeros((1,n))
data_testing=a[i]
X1_testing=data_testing[:,0:n-1]
y1_testing=data_testing[:,n-1:n]
for j in range(k):
if(j!=0):
b=np.vstack((b,a[j]))
data_traning=b[1:,:]
X1_training=data_traning[:,0:n-1]
y1_training=data_traning[:,n-1:n]
omega=np.zeros((n-1,1),dtype=float)
omega=gradient(X1_training,y1_training,omega,alpha,20)
J=costFunction(X1_testing,y1_testing,omega)
MSE1.append(J)
b1=(sum(MSE1)/len(MSE1))
a1.append(b1)
alpha=np.linspace(.1,1,10)
for i in range(len(a1)):
print(f'for alpha={alpha[i]} MSE is {a1[i]}')
2 of 5 20/08/19, 2:01 am
problem6 part1 http://localhost:8891/nbconvert/html/lab_assign...
In [14]: a=[]
for alpha in np.linspace(.1,1,10):
MSE=[]
for i in range(30):
data= np.random.permutation(data)
X_training=data[0:int(0.7*m),0:n-1]
y_training=data[0:int(0.7*m),n-1:n]
X_testing=data[int(0.7*m):m,0:n-1]
y_testing=data[int(0.7*m):m,n-1:n]
omega=np.zeros((n-1,1),dtype=float)
omega=gradient(X_training,y_training,omega,alpha,20)
J=costFunction(X_testing,y_testing,omega)
MSE.append(J)
b=(sum(MSE)/len(MSE))
a.append(b)
alpha=np.linspace(.1,1,10)
for i in range(len(a)):
print(f'for alpha={alpha[i]} MSE is {a[i]}')
In [15]: X=data[:,0:n-1]
y=data[:,n-1:n]
omega=np.zeros((n-1,1),dtype=float)
omega=gradient(X,y,omega,.8,20)
cost=costFunction(X,y,omega)
plt.xlabel(' no of iteration')
plt.ylabel('cost')
plt.xlim(0,20)
print(omega)
print(cost)
[[1.49999458]
[3.6999956 ]]
[[2.64725194e-09]]
3 of 5 20/08/19, 2:01 am
problem6 part1 http://localhost:8891/nbconvert/html/lab_assign...
In [16]: plt.scatter(X[:,[1]],y[:,[0]])
plt.plot(X[:,[1]],X[:,0:n-1].dot(omega))
plt.show()
In [14]: w0_vals=np.linspace(-2,5,100)
w1_vals=np.linspace(1,8,100)
cost_vals=np.zeros((len(w0_vals),len(w1_vals)))
for i in range(len(w0_vals)):
for j in range(len(w1_vals)):
temp=np.array([[w0_vals[i]],[w1_vals[j]]],dtype=float)
cost_vals[i,j]=costFunction(X,y,temp)
cost_vals=cost_vals.T
In [12]: ax = plt.axes(projection='3d')
ax.plot_surface(w0_vals, w1_vals, cost_vals, rstride=1, cstride=1,cmap='viri
dis', edgecolor='none')
ax.set_title('surface');
ax.set_xlabel('$w0$')
ax.set_ylabel('$w1$')
ax.set_zlabel('$cost$')
4 of 5 20/08/19, 2:01 am
problem6 part1 http://localhost:8891/nbconvert/html/lab_assign...
In [13]: ax = plt.axes()
ax.contour(w0_vals, w1_vals, cost_vals)
ax.set_xlabel('$w0$')
ax.set_ylabel('$w1$')
plt.scatter(omega[0],omega[1])
5 of 5 20/08/19, 2:01 am