Explaining Simple Linear Regression with example. | Download project in Python code.

 Prediction through Simple Linear Regression based Model

In this post, we are performing prediction through the use of simple linear regression. For implementing SLR (Simple Linear Regression), we have collected the dataset consisting of two columns: Salary and Experience.

Here in the example, we are predicting Salary by considering experience. For achieving the prediction, the following steps are undertaken: Starting by importing the libraries and the dataset followed by exploring the dataset. The data consists of a total of 30 rows with no null value and no categorical data. Further the data is split into training set and test set. Train data is required to train the model to perform the prediction for new data.

Step 1: Importing the libraries

Step 2: Importing the dataset

Step 3: Exploring the dataset

Step 4: Splitting the dataset into the Training set and Test set

Step 5: Training the Simple Linear Regression model on the Training set

Step 6: Predicting the Test set results

Step 7: Visualizing the Training set results

Step 8: Visualizing the Test set results

 

Importing the libraries

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

 

Importing the dataset

dataset = pd.read_csv('Salary_Data.csv')

X = dataset.iloc[:, :-1].values  #first column is assigned to variable x

y = dataset.iloc[:, -1].values  #second column is assigned to variable y

 

Exploring the dataset

dataset.head()












dataset



 















Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)

 

Training the Simple Linear Regression model on the Training set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X_train, y_train)

 

Predicting the Test set results

y_pred = regressor.predict(X_test)

y_pred



 

Visualising the Training set results

plt.scatter(X_train, y_train, color = 'red')

plt.plot(X_train, regressor.predict(X_train), color = 'blue')

plt.title('Salary vs Experience (Training set)')

plt.xlabel('Years of Experience')

plt.ylabel('Salary')

plt.show()



 






Visualising the Test set results

plt.scatter(X_test, y_test, color = 'red')

plt.plot(X_train, regressor.predict(X_train), color = 'blue')

plt.title('Salary vs Experience (Test set)')

plt.xlabel('Years of Experience')

plt.ylabel('Salary')

plt.show()



 

 

 

Comments

Popular posts from this blog

Accounting Multiple Choice Questions with answers | Download PDF for MCQs

Accounting multiple choice questions with answers | Download pdf of MCQs