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 required libraries in python for Gradient Design.
- Upload the dataset and check any null value using .isnull() function.
- Declare the default values for linear regression.
- Calculate the loss usinng Mean Square Error.
- Predict the value of y.
- Plot the graph respect to hours and scores using scatter plot function.
/*
Program to implement the linear regression using gradient descent.
Developed by: JANARTHANAN V K
RegisterNumber: 212222230051
*/
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
def linear_regression(X1, y, learning_rate = 0.01, num_iters = 100):
X = np.c_[np.ones(len(X1)),X1]
theta = np.zeros(X.shape[1]).reshape(-1,1)
for _ in range(num_iters):
predictions = (X).dot(theta).reshape(-1,1)
errors = (predictions - y).reshape(-1,1)
theta -= learning_rate * (1/len(X1)) * X.T.dot(errors)
return theta
data = pd.read_csv("..\Ex03\50_Startups.csv")
data.head()
X = data.iloc[1:, :-2].values
print(X)
X1 = X.astype(float)
scaler = StandardScaler()
y = data.iloc[1:,-1].values.reshape(-1,1)
print(y)
X1_scaled = scaler.fit_transform(X1)
Y1_scaled = scaler.fit_transform(y)
print(X1_scaled)
print(Y1_scaled)
theta = linear_regression(X1_scaled,Y1_scaled)
new_data = np.array([165349.2,136897.8,471784.1]).reshape(-1,1)
new_scaled = scaler.fit_transform(new_data)
prediction = np.dot(np.append(1,new_scaled),theta).reshape(-1,1)
pre = scaler.inverse_transform(prediction)
print("Prediction value:",pre)
Thus the program to implement the linear regression using gradient descent is written and verified using python programming.