Machine Learning Basics: Simple Linear Regression | by Gurucharan M K | Towards Data Science

One would perhaps come across the term “Regression” during their initial days of Data Science programming. In this story, I would like explain the program code for the very basic “Simple Linear Regression” with a common example.
In statistics, Linear Regression is a linear approach to modeling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables). In our example, we will go through the Simple Linear Regression.
Simple Linear Regression is of the form y = wx + b, where y is the dependent variable, x is the independent variable, w and b are the training parameters which are to be optimized during training process to get accurate predictions.
Let us now apply Machine Learning to train a dataset to predict the Salary from Years of Experience.
In this first step, we shall import the pandas library that will be used to store the data in a Pandas DataFrame. The matplotlib is used to plot graphs.
import numpy as npimport matplotlib.pyplot as pltimport pandas as pd
In this step, we shall download the dataset from my github repositary which contains the data as “Salary_Data.csv”. The variable X will store the “Years of Experience” and the variable Y will store the “Salary”. The dataset.head(5)is used to visualize the first 5 rows of the data.
dataset = pd.read_csv('https://raw.githubusercontent.com/mk-gurucharan/Regression/master/Salary_Data.csv')X = dataset.iloc[:, :-1].valuesy = dataset.iloc[:, -1].valuesdataset.head(5)>>YearsExperience Salary1.1             39343.01.3             46205.01.5             37731.02.0             43525.02.2             39891.0
In this step, we have to split the dataset into the Training set, on which the Linear Regression model will be trained and the Test set, on which the trained model will be applied to visualize the results. In this the test_size=0.2 denotes that 20% of the data will be kept as the Test set and the remaining 80% will be used for training as the Training set.
from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
In the fourth step, the class LinearRegression is imported and is assigned to the variable “regressor”. The regressor.fit() function is fitted with X_train and Y_train on which the model will be trained.
from sklearn.linear_model import LinearRegressionregressor = LinearRegression()regressor.fit(X_train, y_train)
In this step, the regressor.predict() function is used to predict the values for the Test set and the values are stored to the variable y_pred.
y_pred = regressor.predict(X_test)
In this step, a Pandas DataFrame is created to compare the Salary values of both the original Test set (y_test) and the predicted results (y_pred).
df = pd.DataFrame({'Real Values':y_test, 'Predicted Values':y_pred})df>> Real Values    Predicted Values109431.0       107621.91710781363.0        81508.21711293940.0        82440.84925555794.0        63788.20640166029.0        74047.15997091738.0        89901.906396
We can see that the predicted salaries are very close to the real salary values and it can be concluded that the model has been well trained.
In this last step, we visualize the results of the Real and the Predicted Salary values along with the Linear Regression Line on a graph that is plotted.
plt.scatter(X_test, y_test, color = 'red')plt.scatter(X_test, y_pred, color = 'green')plt.plot(X_train, regressor.predict(X_train), color = 'black')plt.title('Salary vs Experience (Result)')plt.xlabel('YearsExperience')plt.ylabel('Salary')plt.show()
In this graph, the Real values are plotted in “Red” color and the Predicted values are plotted in “Green” color. The Linear Regression line that is generated is drawn in “Black” color.
Thus in this story, we have successfully been able to build a Simple Linear Regression model that predicts the ‘Salary’ of an employee based on their ‘Years of Experience’ and visualize the results.
I am also attaching the link to my github repository where you can download this Google Colab notebook and the data files for your reference.
github.com
You can also find the explanation of the program for other Regression models below:
Simple Linear Regression
Multiple Linear Regression
Polynomial Regression
Support Vector Regression
Decision Tree Regression
Random Forest Regression
We will come across the more complex models of Regression, Classification and Clustering in the upcoming articles. Till then, Happy Machine Learning!