To write a program to predict the profit of a city using the linear regression model with gradient descent.
- Hardware โ PCs
- Anaconda โ Python 3.7 Installation / Jupyter notebook
- Import all the necessary python libraries.
- Introduce the variables needed to execute the function.
- Using for loop apply the concept using the formulae.
- Execute the program and plot the graph.
/*
Program to implement the linear regression using gradient descent.
Developed by: MANO M
RegisterNumber: 212221040100
*/
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
data=pd.read_csv("/content/ex1.txt",header=None)
plt.scatter(data[0],data[1])
plt.xticks(np.arange(5,30,step=5))
plt.yticks(np.arange(-5,30,step=5))
plt.xlabel("Population of City (10,1000s)")
plt.ylabel("Profit ($10,000)")
plt.title("Profit Prediction")
def computeCost(X,y,theta):
m=len(y) #length of the training data
h=X.dot(theta) #hypothesis
square_err=(h-y)**2
return 1/(2*m) * np.sum(square_err) #returning ]
data_n=data.values
m=data_n[:,0].size
X=np.append(np.ones((m,1)),data_n[:,0].reshape(m,1),axis=1)
y=data_n[:,1].reshape(m,1)
theta=np.zeros((2,1))
computeCost(X,y,theta) #Call the function
def gradientDescent(X,y,theta,alpha,num_iters):
m=len(y)
J_history=[]
for i in range(num_iters):
predictions=X.dot(theta)
error=np.dot(X.transpose(),(predictions -y))
descent=alpha * 1/m * error
theta-=descent
J_history.append(computeCost(X,y,theta))
return theta,J_history
theta,J_history = gradientDescent(X,y,theta,0.01,1500)
print("h(x) ="+str(round(theta[0,0],2))+" + "+str(round(theta[1,0],2))+"x1")
plt.plot(J_history)
plt.xlabel("Iteration")
plt.ylabel("$J(\Theta)$")
plt.title("Cost function using Gradient Descent")
def predict(x,theta):
predictions= np.dot(theta.transpose(),x)
return predictions[0]
predict1=predict(np.array([1,3.5]),theta)*10000
print("For population = 35,000, we predict a profit of $"+str(round(predict1,0)))
predict2=predict(np.array([1,7]),theta)*10000
print("For population = 70,000, we predict a profit of $"+str(round(predict2,0)))
1.Profit prediction graph
2.Compute cost value
3.h(x) value
4.Cost function using Gradient Descent graph
5.Profit prediction graph
6.Profit for the population 35,000
7.Profit for the population 70,000
Thus the program to implement the linear regression using gradient descent is written and verified using python programming.