Linear Regression Using Pandas & Numpy — For Beginners in Data Science
Tags: Numpy, Data Science, Pandas, Linear Regression, Data Visualization

Problem Statement An eCommerce company based in New York City that sells clothing online also have in-store style and clothing advice sessions. Customers come into the store, have sessions/meetings with a personal stylist, then they can go home and order either on a mobile app or website for the clothes they want.

The company is trying to decide whether to focus their efforts on their mobile app experience or their website. Let’s figure it out.

#Import the required packages

import pandas as pd

import numpy as np

import matplotlib.pyplot as py

import seaborn as sns

%matplotlib inline

# Read the file

customers = pd.read_csv(“Ecommerce Customers”)

# Let’s checkout the data -

customers.head()

customers.describe()

Exploratory Data Analysis

# First step with data is to analyze the data, explore what relationships exist and how those are correlated.

# Created a jointplot (using seaborn) to compare the Time on Website and Yearly Amount Spent columns. This is to check if the correlation makes sense?

sns.jointplot(x=’Time on Website’,y=’Yearly Amount Spent’, data=customers)

# The same for App data

sns.jointplot(x=’Time on App’,y=’Yearly Amount Spent’, data=customers)

# Created a jointplot of type hex for Time spent on App vs the Length of Membership

sns.jointplot(x=’Time on App’,y=’Length of Membership’, data=customers,kind=’hex’)

# We can create a pairplot to explore the types of relationships across the entire data set. Notice the positive linear correlation between Yearly amount spent and length of membership.

sns.pairplot(data=customers)

# So we dig deep into this relationship by creating a linear plot (using seaborn’s lmplot) of Yearly Amount Spent vs. Length of Membership

sns.lmplot(x=’Length of Membership’,y=’Yearly Amount Spent’,data=customers)

Training and Testing Data

Now that we’ve explored the data a bit, let’s go ahead and split the data into training and testing sets. Set a variable X equal to the numerical features of the customers and a variable Y equal to the “Yearly Amount Spent” column.

X =customers[[‘Avg. Session Length’,’Time on App’,’Time on Website’,’Length of Membership’]]

X.head()

Y=customers[‘Yearly Amount Spent’]

Y.head()

Use model_selection.train_test_split from sklearn to split the data into training and testing sets. Set test_size=0.3 and random_state=101

from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.3,random_state=101)

Training the Model

Now its time to train our model on our training data!

from sklearn.linear_model import LinearRegression

lm = LinearRegression() # Creating an Instance of LinearRegression model

lm.fit(X_train,Y_train) # Train/fit on the trainingdata, this will give-

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

# The coefficients/slopes of model -

print(lm.coef_)

[25.98154972 38.59015875 0.19040528 61.27909654]

Predicting Test Data

Now that we have fit our model, let’s evaluate its performance by predicting off the test values!

prediction = lm.predict(X_test)

#Let’s create a scatterplot of the real test values versus the predicted values to check the performance of our model

py.scatter(Y_test,prediction)

Evaluating the Model

Let’s evaluate our model performance by calculating the residual sum of squares and the explained variance score (R²)

from sklearn import metrics

print(‘MAE= ‘, metrics.mean_absolute_error(Y_test,prediction) )

print(‘MSE= ‘, metrics.mean_squared_error(Y_test,prediction))

print(‘RMSE:’, np.sqrt(metrics.mean_squared_error(Y_test, prediction)))

MAE= 7.228148653430853

MSE= 79.81305165097487

RMSE: 8.933815066978656

Residuals

Let’s quickly explore the residuals to make sure everything was okay with our data

# Plotting a histogram of the residuals and make sure it looks normally distributed using plt.hist().

py.hist(prediction-Y_test,bins=50)

Conclusion

We still want to figure out the answer to the original question, do we focus our efforts on the mobile App or website development? Or maybe that doesn’t even really matter, and Membership Time is what is really important. Let’s interpret the coefficients to get an idea

co=pd.DataFrame(lm.coef_,X.columns)

co.columns = [‘Coefficient’]

co

Interpreting the coefficients:

Holding all other features fixed, a 1 unit increase in Avg. Session Length is associated with an increase of 25.98 total dollars spent .

is associated with an . Holding all other features fixed, a 1 unit increase in Time on App is associated with an increase of 38.59 total dollars spent .

is associated with an . Holding all other features fixed, a 1 unit increase in Time on Website is associated with an increase of 0.19 total dollars spent .

is associated with an . Holding all other features fixed, a 1 unit increase in Length of Membership is associated with an increase of 61.27 total dollars spent.

Final Conclusion —

There are two ways to think about this: 1. Develop the Website to catch up to the performance of the mobile app, or 2. develop the App more since that is what is working better.

Being a data person, we can present both the options to the company with the numbers and help them make a decision.