4.1.2.4 Lab - Simple Linear Regression in Python
4.1.2.4 Lab - Simple Linear Regression in Python
Scenario / Background
In statistics, linear regression is a way to model a relationship between dependent variable y and
independent variable x .
In this lab, you will analyze district sales data and perform a simple linear regression to predict annual net
sales based on the number of stores in the district.
Required Resources
1 PC with Internet access
Python libraries: pandas, numpy, scipy, and matplotlib
Datafiles: stores-dist.csv
In this part, you will import the libraries and the data from the file stores-dist.csv.
matplotlib.pyplot as plt
numpy as np
pandas as pd
In [ ]:
# Code Cell 1
In this step, you will import the data from the file stores-dist.csv and verify that the file was imported
correctly.
In [ ]:
# Code Cell 2
The column headings, annual net sales and number of stores in district are renamed to make it
easier during data processing.
In [ ]:
# Code Cell 3
# The district column has no relevance at this time, so it can be dropped.
salesDist = salesDist.rename(columns={'annual net sales':'sales','number of stores in d
istrict':'stores'})
salesDist.head()
In this step, you will investigate the correlation of the data prior to regression analysis. You will also drop any
unrelated columns as necessary.
In [ ]:
# Code Cell 4
# Check correlation of data prior to doing the analysis
# # Hint: check lab 3.1.5.5
From the correlation coefficent, it appears that the column district has low correlation to the annual net
sales and number of stores in the district. So the district column is not necessary as part of the
regression analysis. The district column can be dropped from the dataframe.
In [ ]:
# Code Cell 5
# The district column has no relevance at this time, so it can be dropped.
#sales = salesDist.drop(...)
sales.head()
From the correlation coefficent data, what type of correlation did you observe between annual net sales and
number of stores in the district?
In this step, you will create a plot to visualize the data. You will also assign stores as the independent variable
x and sales as the dependent variable y.
In [ ]:
# Code Cell 6
# dependent variable for y axis
y = sales['sales']
# independent variable for x axis
x = sales.stores
In [ ]:
# Code Cell 7
# Display the plot inline
%matplotlib inline
# Create a scatter plot: Number of stores in the District vs. Annual Net Sales
plt.plot(x,y, 'o', markersize = 15)
In this part, you will use numpy to generate a regression line for the analyzed data. You will also calculate the
centroid for this dataset. The centrod is the mean for the dataset. The generated simple linear regression line
must also pass through the centroid.
Step 1: Calculate the slope and y-intercept of the linear regression line.
In [ ]:
# Code Cell 8
# Use numpy polyfit for linear regression to fit the data
# Generate the slope of the line (m)
# Generate the y-intercept (b)
m, b = np.polyfit(x,y,1)
print ('The slope of line is {:.2f}.'.format(m))
print ('The y-intercept is {:.2f}.'.format(b))
print ('The best fit simple linear regression line is {:.2f}x + {:.2f}.'.format(m,b))
In [ ]:
# Code Cell 9
# y coordinate for centroid
y_mean = y.mean()
# x coordinate for centroid
x_mean = x.mean()
print ('The centroid for this dataset is x = {:.2f} and y = {:.2f}.'.format(x_mean, y_m
ean))
Step 3: Overlay the regression line and the centroid point on the plot.
In [ ]:
# Code Cell 10
# Create the plot inline
%matplotlib inline
# Create legend
plt.legend(loc = 'upper right', fontsize = 20)
Step 4: Prediction
Using the linear regression line, you can predict the annual net sales based on the number of stores in the
district.
In [ ]:
# Code Cell 11
# Function to predict the net sales from the regression line
def predict(query):
if query >= 1:
predict = m * query + b
return predict
else:
print ("You must have at least 1 store in the district to predict the annual ne
t sales.")
In [ ]:
# Code Cell 12
# Enter the number of stores in the function to generate the net sales prediction.
What is the predicted net sales if there are 4 stores in the district?
© 2017 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public.