# A beginner’s guide to Linear Regression in Python with Scikit-Learn

A beginner’s guide to Linear Regression in Python with Scikit-LearnNagesh Singh ChauhanBlockedUnblockFollowFollowingFeb 25sourceThere are two types of supervised machine learning algorithms: Regression and classification.

The former predicts continuous value outputs while the latter predicts discrete outputs.

For instance, predicting the price of a house in dollars is a regression problem whereas predicting whether a tumor is malignant or benign is a classification problem.

In this article we will briefly study what linear regression is and how it can be implemented for both two variables and multiple variables using Scikit-Learn , which is one of the most popular machine learning libraries for Python.

Linear Regression TheoryThe term “linearity” in algebra refers to a linear relationship between two or more variables.

If we draw this relationship in a two dimensional space (between two variables), we get a straight line.

Linear regression performs the task to predict a dependent variable value (y) based on a given independent variable (x).

So, this regression technique finds out a linear relationship between x (input) and y(output).

Hence, the name is Linear Regression.

If we plot the independent variable (x) on the x-axis and dependent variable (y) on the y-axis, linear regression gives us a straight line that best fits the data points, as shown in the figure below.

We know that the equation of a straight line is basically:sourceThe equation of above line is :Y= mx + bWhere b is the intercept and m is the slope of the line.

So basically, the linear regression algorithm gives us the most optimal value for the intercept and the slope (in two dimensions).

The y and x variables remain the same, since they are the data features and cannot be changed.

The values that we can control are the intercept(b) and slope(m).

There can be multiple straight lines depending upon the values of intercept and slope.

Basically what the linear regression algorithm does is it fits multiple lines on the data points and returns the line that results in the least error.

This same concept can be extended to the cases where there are more than two variables.

This is called multiple linear regression.

For instance, consider a scenario where you have to predict the price of house based upon its area, number of bedrooms, average income of the people in the area, the age of the house, and so on.

In this case the dependent variable is dependent upon several independent variables.

A regression model involving multiple variables can be represented as:y = b0 + m1b1 + m2b2 + m3b3 + … … mnbnThis is the equation of a hyper plane.

Remember, a linear regression model in two dimensions is a straight line; in three dimensions it is a plane, and in more than three dimensions, a hyper plane.

In this section we will see how the Python’s Scikit-Learn library for machine learning can be used to implement regression functions.

We will start with simple linear regression involving two variables and and then we will move towards linear regression involving multiple variables.

Simple Linear RegressionLinear RegressionWhile exploring the Aerial Bombing Operations of World War Two dataset and recalling that the D-Day landings were nearly postponed due to poor weather, I downloaded these weather reports from the period to compare with missions in the bombing operations dataset.

The dataset contains information on weather conditions recorded on each day at various weather stations around the world.

Information includes precipitation, snowfall, temperatures, wind speed and whether the day included thunder storms or other poor weather conditions.

So our task is to predict the maximum temperature taking input feature as minimum temperature.

Lets start coding :Import all the required libraries :import pandas as pd import numpy as np import matplotlib.

pyplot as plt import seaborn as seabornInstance from sklearn.

model_selection import train_test_split from sklearn.

linear_model import LinearRegressionfrom sklearn import metrics%matplotlib inlineThe following command imports the CSV dataset using pandas:dataset = pd.

csv')Let’s explore the data little bit by checking the number of rows and columns in our datasets.

dataset.

shapeYou should receive output as (119040, 31), which means the data contains 119040 rows and 31 columns.

To see statistical details of the dataset, we can use describe():dataset.

describe()statistical view of the datasetAnd finally, let’s plot our data points on 2-D graph to eyeball our dataset and see if we can manually find any relationship between the data using below script :dataset.

plot(x='MinTemp', y='MaxTemp', style='o') plt.

title('MinTemp vs MaxTemp') plt.

xlabel('MinTemp') plt.

ylabel('MaxTemp') plt.

show()We have taken MinTemp and MaxTemp for doing our analysis.

Below is 2-D graph between MinTemp and MaxTemp.

Let’s check the average max temperature and once we plot it we can oberserve that the Average Maximum Temperature is Between Nearly 25 and 35.

plt.

figure(figsize=(15,10))plt.

tight_layout()seabornInstance.

distplot(dataset['MaxTemp'])Average maximum temperature which is in between 25 and 35.

Our next step is to divide the data into “attributes” and “labels”.

Attributes are the independent variables while labels are dependent variables whose values are to be predicted.

In our dataset we only have two columns.

We want to predict the MaxTemp depending upon the MinTemp recorded.

Therefore our attribute set will consist of the “MinTemp” column which is stored in X variable, and the label will be the “MaxTemp” column which is stored in y variable.

X = dataset['MinTemp'].

values.

reshape(-1,1)y = dataset['MaxTemp'].

values.

reshape(-1,1)Next we split 80% of the data to training set while 20% of the data to test set using below code.

The test_size variable is where we actually specify the proportion of test set.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.

2, random_state=0)After splitting the data into training and testing sets, finally the time to train our algorithm.

For that we need to import LinearRegression class, instantiate it, and call the fit() method along with our training data.

regressor = LinearRegression() regressor.

fit(X_train, y_train) #training the algorithmAs we have discussed that the linear regression model basically finds the best value for the intercept and slope, which results in a line that best fits the data.

To see the value of the intercept and slop calculated by the linear regression algorithm for our dataset, execute the following code.

#To retrieve the intercept:print(regressor.

intercept_)#For retrieving the slope:print(regressor.

coef_)The result should be approximately 10.

66185201 and 0.

92033997 respectively.

This means that for every one unit of change in Min temperature, the change in the Max temperature is about 0.

92%.

Now that we have trained our algorithm, it’s time to make some predictions.

To do so, we will use our test data and see how accurately our algorithm predicts the percentage score.

To make pre-dictions on the test data, execute the following script:y_pred = regressor.

predict(X_test)To compare the actual output values for X_test with the predicted values, execute the following script:df = pd.

DataFrame({'Actual': y_test.

flatten(), 'Predicted': y_pred.

flatten()})dfcomparison of Actual and Predicted valueWe can also visualize comparison result as a bar graph using below script :Note : As the number of records are huge , for representation purpose I’m taking 25 records.

df1 = df.

plot(kind='bar',figsize=(16,10))plt.

grid(which='major', linestyle='-', linewidth='0.

5', color='green')plt.

grid(which='minor', linestyle=':', linewidth='0.

5', color='black')plt.

show()Bar graph showing comparison of Actual and Predicted values.

Though our model is not very precise, the predicted percentages are close to the actual ones.

Lets plot our straight line with the test data :plt.

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

plot(X_test, y_pred, color='red', linewidth=2)plt.

show()prediction vs test dataThe straight line in the above graph shows our algorithm is correct.

The final step is to evaluate the performance of algorithm.

This step is particularly important to compare how well different algorithms perform on a particular dataset.

For regression algorithms, three evaluation metrics are commonly used:Mean Absolute Error (MAE) is the mean of the absolute value of the errors.

It is calculated as:Mean Absolute Error2.

Mean Squared Error (MSE) is the mean of the squared errors and is calculated as:Mean Squared Error3.

Root Mean Squared Error (RMSE) is the square root of the mean of the squared errors:Root Mean Squared ErrorLuckily, we don’t have to perform these calculations manually.

The Scikit-Learn library comes with pre-built functions that can be used to find out these values for us.

Let’s find the values for these metrics using our test data.

print('Mean Absolute Error:', metrics.

mean_absolute_error(y_test, y_pred)) print('Mean Squared Error:', metrics.

mean_squared_error(y_test, y_pred)) print('Root Mean Squared Error:', np.

sqrt(metrics.

mean_squared_error(y_test, y_pred)))You should receive output like this (but probably slightly different):('Mean Absolute Error:', 3.

19932917837853)('Mean Squared Error:', 17.

631568097568447)('Root Mean Squared Error:', 4.

198996082109204)You can see that the value of root mean squared error is 4.

19, which is more than 10% of the mean value of the percentages of all the temperature i.

e.

22.

41.

This means that our algorithm was not very accurate but can still make reasonably good predictions.

Multiple Linear RegressionsourceWe just performed linear regression in the above section involving two variables.

Almost all real world problems that you are going to encounter will have more than two variables.

Linear regression involving multiple variables is called “multiple linear regression” or multivariate linear regression.

The steps to perform multiple linear regression are almost similar to that of simple linear regression.

The difference lies in the evaluation.

You can use it to find out which factor has the highest impact on the predicted output and how different variables relate to each other.

The dataset related to red variants of the Portuguese “Vinho Verde” wine.

Due to privacy and logistic issues, only physicochemical (inputs) and sensory (the output) variables are available (e.

g.

there is no data about grape types, wine brand, wine selling price, etc.

).

We will take into account various input features like fixed acidity, volatile acidity ,citric acid, residual sugar, chlorides, free sulfur dioxide, total sulfur dioxide, density, pH, sulphates, alcohol.

Based on these features we will predict the quality of the wine.

Now lets start our coding :import all the required libraries :import pandas as pd import numpy as np import matplotlib.

pyplot as plt import seaborn as seabornInstance from sklearn.

model_selection import train_test_split from sklearn.

linear_model import LinearRegressionfrom sklearn import metrics%matplotlib inlineThe following command imports the dataset from the file you downloaded via the link above:dataset = pd.

csv')Let’s explore the data little bit by checking the number of rows and columns in our datasets.

dataset.

shapeIt will give (1599, 12) as output which means our dataset has 1599 rows and 12 columns.

To see statistical details of the dataset, we can use describe():dataset.

describe()Lets clean our data little bit, So first check which are the columns have NaN values in it :dataset.

isnull().

any()Once the above code is executed, all the columns should give False, Incase for any column you find True result, then remove all the null values from that column using below code.

dataset = dataset.

fillna(method='ffill')Our next step is to divide the data into “attributes” and “labels”.

X variable contains all the attributes/features and y variable contains labels.

X = dataset[['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar', 'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'sulphates','alcohol']].

valuesy = dataset['quality'].

valuesLets check the average value of the “quality” column.

plt.

figure(figsize=(15,10))plt.

tight_layout()seabornInstance.

distplot(dataset['quality'])Average value of the quality of the wine.

As we can observe that most of the time the value is either 5 or 6.

Next we split 80% of the data to training set while 20% of the data to test set using below code.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.

2, random_state=0)Now lets train our model.

regressor = LinearRegression() regressor.

fit(X_train, y_train)As said earlier, in case of multivariable linear regression, the regression model has to find the most optimal coefficients for all the attributes.

To see what coefficients our regression model has chosen, execute the following script:coeff_df = pd.

DataFrame(regressor.

coef_, X.

columns, columns=['Coefficient']) coeff_dfit should give output something like :This means that for a unit increase in “density”, there is a decrease of 31.

51 units in the quality of the wine.

Similarly, a unit decrease in “Chlorides“ results in an increase of 1.

87 units in the quality of the wine .

We can see that rest of the features have a very little effect on the quality of the wine.

Now lets do prediction on test data.

y_pred = regressor.

predict(X_test)Now lets check the difference between the actual value and predicted value.

df = pd.

DataFrame({'Actual': y_test, 'Predicted': y_pred})df1 = df.

head(25)Comparison between Actual and Predicted valueNow lets plot the comparison of Actual and Predicted valuesdf1.

plot(kind='bar',figsize=(10,8))plt.

grid(which='major', linestyle='-', linewidth='0.

5', color='green')plt.

grid(which='minor', linestyle=':', linewidth='0.

5', color='black')plt.

show()Bar graph showing the difference between Actual and predicted valueAs we can observe here that our model has returned pretty good prediction results.

The final step is to evaluate the performance of algorithm.

We’ll do this by finding the values for MAE, MSE and RMSE.

Execute the following script:print('Mean Absolute Error:', metrics.

mean_absolute_error(y_test, y_pred)) print('Mean Squared Error:', metrics.

mean_squared_error(y_test, y_pred)) print('Root Mean Squared Error:', np.

sqrt(metrics.

mean_squared_error(y_test, y_pred)))The output looks like :('Mean Absolute Error:', 0.

46963309286611077)('Mean Squared Error:', 0.

38447119782012446)('Root Mean Squared Error:', 0.

6200574149384268)You can see that the value of root mean squared error is 0.

62, which is slightly greater than 10% of the mean value of the gas consumption in all states which is 5.

63.

This means that our algorithm was not very accurate but can still make reasonably good predictions.

There are many factors that may have contributed to this inaccuracy, for example :Need more data: We need to have huge amount of to get the best possible prediction.

Bad assumptions: We made the assumption that this data has a linear relationship, but that might not be the case.

Poor features: The features we used may not have had a high enough correlation to the values we were trying to predict.

ConclusionIn this article we studied on of the most fundamental machine learning algorithms i.

e.

linear regression.

We implemented both simple linear regression and multiple linear regression with the help of the Scikit-Learn machine learning library.

I hope you guys have enjoyed the reading.

Let me know your doubts/suggestions in the comment section.