0% found this document useful (0 votes)
29 views4 pages

Problem 6 Analysis of Data1 Using Linear Regression

The document describes using linear regression to analyze a dataset called data1. It defines cost and gradient functions for linear regression. It then performs gradient descent on the data to find the optimal omega values, which are printed along with the final cost. A scatter plot is produced comparing the actual y values to the predicted values using the learned omega. Contour and surface plots are also generated to visualize the cost function. Finally, it performs a 5-fold cross validation on the data by splitting it into training and test sets.

Uploaded by

durgesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Problem 6 Analysis of Data1 Using Linear Regression

The document describes using linear regression to analyze a dataset called data1. It defines cost and gradient functions for linear regression. It then performs gradient descent on the data to find the optimal omega values, which are printed along with the final cost. A scatter plot is produced comparing the actual y values to the predicted values using the learned omega. Contour and surface plots are also generated to visualize the cost function. Finally, it performs a 5-fold cross validation on the data by splitting it into training and test sets.

Uploaded by

durgesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem 6 trial http://localhost:8891/nbconvert/html/lab_assign...

Problem 6

Analysis of data1 using linear regression


In [17]: import numpy as np
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

In [18]: data=np.genfromtxt('data1.csv',delimiter=',',dtype=float)

1 of 4 20/08/19, 1:39 am
Problem 6 trial http://localhost:8891/nbconvert/html/lab_assign...

In [19]: def costFunction(X,y,omega):


m,n=X.shape
J=0;
for i in range(m):
J=J+((X[i,0:n].dot(omega)-y[i])**2)*(1/(2))
return J

def gradient(X,y,omega,alpha,iter_num):
m,n=X.shape
for k in range(iter_num):
temp=np.zeros((n,1),dtype=float)
for j in range(n):
for i in range(m):
temp[j]=temp[j]+(X[i,0:n].dot(omega)-y[i])*X[i,j]
temp[j]=omega[j]-(alpha)*temp[j]
omega=temp
J=costFunction(X,y,omega)
plt.scatter(k,J)
#ax.plot_surface(omega[0,:],omega[1,:],J)
return omega
#plt.xlabel(' no of iteration')
#plt.ylabel('cost')
#plt.xlim(0,100)
#plt.ylim(4000,10000)
m,n=data.shape
c=np.ones((m,1))
X=data[:,0:n-1]
X=np.hstack((c,X))
y=data[:,n-1:n]
omega=np.zeros((n,1),dtype=float)
omega=gradient(X,y,omega,0.005,40)
print(omega)
J=costFunction(X,y,omega)
print(J)
#plt.surface()

[[1.49999458]
[3.6999956 ]]
[5.29450388e-07]

2 of 4 20/08/19, 1:39 am
Problem 6 trial http://localhost:8891/nbconvert/html/lab_assign...

In [20]: plt.scatter(X[:,[1]],y[:,[0]])
plt.plot(X[:,[1]],X[:,0:n].dot(omega))
plt.show()

In [12]: 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 [13]: 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$')

Out[13]: Text(0.5, 0, '$cost$')

3 of 4 20/08/19, 1:39 am
Problem 6 trial http://localhost:8891/nbconvert/html/lab_assign...

In [14]: 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])

Out[14]: <matplotlib.collections.PathCollection at 0x7ff2e954d668>

In [28]: 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]
for i in range(k):
b=np.zeros((1,n))
data_testing=a[i]
for j in range(k):
if(j!=0):
b=np.vstack((b,a[j]))
data_traning=b[1:,:]

Out[28]: (160, 2)

4 of 4 20/08/19, 1:39 am

You might also like