Simple Linear Regression (Using Scikit Learn)
Implement simple linear regression in python. Use scikit-learn for various functionalities. Read training dataset from a file, build a linear regression model and predict the output for a few unseen data samples. Use a minimum of three datasets for experimentation. Salary_Data.csv is provided as an example. Compare results with results of Assignment 3.
In [2]:
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (6.0, 4.0)
from sklearn.metrics import mean_squared_error # for calculating mean_squared error
In [3]:
data = pd.read_csv('Salary_Data.csv')
X = data.iloc[:, 0]
Y = data.iloc[:, 1]
plt.scatter(X, Y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Training Data')
plt.show()
X = np.array(X).reshape((-1, 1))
In [4]:
model = LinearRegression()
model.fit(X, Y)
print('theta0 = ', model.intercept_, 'theta1 = ', model.coef_)
# Making predictions for Training Data
Y_pred = model.predict(X)
theta0 = 25792.200198668717 theta1 = [9449.96232146]
In [5]:
print('RMSE for Regression=>',np.sqrt(mean_squared_error(Y,Y_pred)))
plt.scatter(X, Y)
plt.plot(X, Y_pred, color = 'red')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Fitting the Linear Hypothesis')
plt.show()
RMSE for Regression=> 5592.043608760661
In [8]:
test_X = 1.2
Y_pred = model.predict(np.array(test_X).reshape((-1, 1)))
In [9]:
Y_pred2 = model.intercept_ + model.coef_ * test_X
print("Salary Prediction for Experience of ", str(test_X), " years =", Y_pred, Y_pred2)
Salary Prediction for Experience of 1.2 years = [37132.15498441] [37132.15498441]
In [ ]:
Tags:
Machine Learning
