Congratulations! You just got some contract work with an Ecommerce company based in New York City that sells clothing online but they also have in-store style and clothing advice sessions. Customers come in to 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. They've hired you on contract to help them figure it out! Let's get started!
Just follow the steps below to analyze the customer data (it's fake, don't worry I didn't give you real credit card numbers or emails).
Import pandas, numpy, matplotlib,and seaborn. Then set %matplotlib inline (You'll import sklearn as you need it.)
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
We'll work with the Ecommerce Customers csv file from the company. It has Customer info, suchas Email, Address, and their color Avatar. Then it also has numerical value columns:
Read in the Ecommerce Customers csv file as a DataFrame called customers.
customers = pd.read_csv('Ecommerce Customers')
Check the head of customers, and check out its info() and describe() methods.
customers.head()
customers.describe()
customers.info()
Let's explore the data!
For the rest of the exercise we'll only be using the numerical data of the csv file.
Use seaborn to create a jointplot to compare the Time on Website and Yearly Amount Spent columns. Does the correlation make sense?
sns.set_palette("GnBu_d")
sns.set_style('whitegrid')
sns.jointplot(x = 'Time on Website', y = 'Yearly Amount Spent', data = customers)
Do the same but with the Time on App column instead.
sns.jointplot(x = 'Time on App', y = 'Yearly Amount Spent', data = customers)
Use jointplot to create a 2D hex bin plot comparing Time on App and Length of Membership.
sns.jointplot(x = 'Time on App', y = 'Length of Membership', data = customers, kind = 'hex')
Let's explore these types of relationships across the entire data set. Use pairplot to recreate the plot below.(Don't worry about the the colors)
sns.pairplot(customers)
Based off this plot what looks to be the most correlated feature with Yearly Amount Spent?
sns.heatmap(customers.corr(), cmap="Blues", annot=True)
Create a linear model 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)
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)
Now its time to train our model on our training data!
Import LinearRegression from sklearn.linear_model
from sklearn.linear_model import LinearRegression
Create an instance of a LinearRegression() model named lm.
lm = LinearRegression()
Train/fit lm on the training data.
lm.fit(X_train,y_train)
Print out the coefficients of the model
lm.coef_
Now that we have fit our model, let's evaluate its performance by predicting off the test values!
Use lm.predict() to predict off the X_test set of the data.
predictions = lm.predict(X_test)
Create a scatterplot of the real test values versus the predicted values.
plt.scatter(x = y_test, y = predictions)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
Let's evaluate our model performance by calculating the residual sum of squares and the explained variance score (R^2).
Calculate the Mean Absolute Error, Mean Squared Error, and the Root Mean Squared Error. Refer to the lecture or to Wikipedia for the formulas
from sklearn import metrics
from math import sqrt
print('MAE:',
metrics.mean_absolute_error(y_test, predictions), ' ',
(1./len(y_test))*(sum(abs(y_test-predictions))))
print('MSE:',
metrics.mean_squared_error(y_test, predictions), ' ',
(1./len(y_test))*(sum((y_test-predictions)**2)))
print('RMSE:',
np.sqrt(metrics.mean_squared_error(y_test, predictions)), ' ',
sqrt((1./len(y_test))*(sum((y_test-predictions)**2))))
You should have gotten a very good model with a good fit. Let's quickly explore the residuals to make sure everything was okay with our data.
Plot a histogram of the residuals and make sure it looks normally distributed. Use either seaborn distplot, or just plt.hist().
sns.distplot((y_test-predictions), bins = 50)
We still want to figure out the answer to the original question, do we focus our efforst on mobile app or website development? Or maybe that doesn't even really matter, and Membership Time is what is really important. Let's see if we can interpret the coefficients at all to get an idea.
Recreate the dataframe below.
df = pd.DataFrame( data = lm.coef_, columns = ['Coefficient'] ,index = X_train.columns)
df.head()
How can you interpret these coefficients?
Holding all other variables fixed, one unit of increase in:
Do you think the company should focus more on their mobile app or on their website?
The Time on Website seems to have little influence on the Yearly Amount Spent, where as the Time on App show a stronger correlation. In that regard the revenue obtained through the App seems to be more important. However, instead of completely scraping the website, I guess the company can improve it to increase its revenue on that channel as well. Then, re-perform the analysis. Having said that, the most important variable that influence the spending is the Length of Membership.
Congrats on your contract work! The company loved the insights! Let's move on.