An Ecommerce data

An ficticious 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!

Imports

Import pandas, numpy, matplotlib,and seaborn.

In [31]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

Get the Data

We'll work with the Ecommerce Customers csv file from the company. It has Customer info, such as Email, Address, and their color Avatar. Then it also has numerical value columns:

  • Avg. Session Length: Average session of in-store style advice sessions.
  • Time on App: Average time spent on App in minutes
  • Time on Website: Average time spent on Website in minutes
  • Length of Membership: How many years the customer has been a member.
In [32]:
customers = pd.read_csv("Ecommerce Customers")

Check the what the ecommerce data looks like.

In [33]:
customers.head()
Out[33]:
Email Address Avatar Avg. Session Length Time on App Time on Website Length of Membership Yearly Amount Spent
0 mstephenson@fernandez.com 835 Frank Tunnel\nWrightmouth, MI 82180-9605 Violet 34.497268 12.655651 39.577668 4.082621 587.951054
1 hduke@hotmail.com 4547 Archer Common\nDiazchester, CA 06566-8576 DarkGreen 31.926272 11.109461 37.268959 2.664034 392.204933
2 pallen@yahoo.com 24645 Valerie Unions Suite 582\nCobbborough, D... Bisque 33.000915 11.330278 37.110597 4.104543 487.547505
3 riverarebecca@gmail.com 1414 David Throughway\nPort Jason, OH 22070-1220 SaddleBrown 34.305557 13.717514 36.721283 3.120179 581.852344
4 mstephens@davidson-herman.com 14023 Rodriguez Passage\nPort Jacobville, PR 3... MediumAquaMarine 33.330673 12.795189 37.536653 4.446308 599.406092
In [4]:
customers.describe()
Out[4]:
Avg. Session Length Time on App Time on Website Length of Membership Yearly Amount Spent
count 500.000000 500.000000 500.000000 500.000000 500.000000
mean 33.053194 12.052488 37.060445 3.533462 499.314038
std 0.992563 0.994216 1.010489 0.999278 79.314782
min 29.532429 8.508152 33.913847 0.269901 256.670582
25% 32.341822 11.388153 36.349257 2.930450 445.038277
50% 33.082008 11.983231 37.069367 3.533975 498.887875
75% 33.711985 12.753850 37.716432 4.126502 549.313828
max 36.139662 15.126994 40.005182 6.922689 765.518462

Getting more information on the quality of the data provided

In [36]:
customers.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 500 entries, 0 to 499
Data columns (total 8 columns):
Email                   500 non-null object
Address                 500 non-null object
Avatar                  500 non-null object
Avg. Session Length     500 non-null float64
Time on App             500 non-null float64
Time on Website         500 non-null float64
Length of Membership    500 non-null float64
Yearly Amount Spent     500 non-null float64
dtypes: float64(5), object(3)
memory usage: 31.3+ KB

From the information above

* There are 500 records of information
* There are no missing values
* There are 8 columns
* The mean, median, mode, percentiles of each Numeric roll
* Proper Data type for each variable column

In summary the data provided is in good shape as far as we can see

Data Exploratory & Visualization Analysis

Let's explore the data by plotting different combinations of only the numeric variable

In [37]:
sns.set_palette("GnBu_d")
sns.set_style('whitegrid')
In [38]:
# More time on site, more money spent.
sns.jointplot(x='Time on Website',y='Yearly Amount Spent',data=customers)
Out[38]:
<seaborn.axisgrid.JointGrid at 0x20e5fd82160>

Do the same but with the Time on App column instead.

In [39]:
sns.jointplot(x='Time on App',y='Yearly Amount Spent',data=customers)
Out[39]:
<seaborn.axisgrid.JointGrid at 0x20e5fd904e0>

Use jointplot to create a 2D hex bin plot comparing Time on App and Length of Membership.

In [40]:
sns.jointplot(x='Time on App',y='Length of Membership',kind='hex',data=customers)
Out[40]:
<seaborn.axisgrid.JointGrid at 0x20e61219a20>

Let's explore these types of relationships across the entire data set.

In [41]:
sns.pairplot(customers)
Out[41]:
<seaborn.axisgrid.PairGrid at 0x20e6121e198>
In [42]:
customers.columns
Out[42]:
Index(['Email', 'Address', 'Avatar', 'Avg. Session Length', 'Time on App',
       'Time on Website', 'Length of Membership', 'Yearly Amount Spent'],
      dtype='object')
In [43]:
#Which Column correlates most with the "Yearly Amount Spent" == "Length of Membership" with 0.809084
customers.corr()
Out[43]:
Avg. Session Length Time on App Time on Website Length of Membership Yearly Amount Spent
Avg. Session Length 1.000000 -0.027826 -0.034987 0.060247 0.355088
Time on App -0.027826 1.000000 0.082388 0.029143 0.499328
Time on Website -0.034987 0.082388 1.000000 -0.047582 -0.002641
Length of Membership 0.060247 0.029143 -0.047582 1.000000 0.809084
Yearly Amount Spent 0.355088 0.499328 -0.002641 0.809084 1.000000

Length of Membership

My expectation is to see the correlation between all the variables against the "Yearly Amount Spent" and to identify which of the variables have a direct positive effect on it.

From the plot above, It can be observed that "Length of Membership" have the most correlation with "Yearly Amount Spent" followed by the "Time on App" and little to no influenced by "Time on Website"

Creating a linear model plot of "Yearly Amount Spent" vs. "Length of Membership".

In [19]:
sns.lmplot(x='Length of Membership',y='Yearly Amount Spent',data=customers)
Out[19]:
<seaborn.axisgrid.FacetGrid at 0x20e5f64b668>

Creating a Model using Linear Regression Machine Learning Algorithm

Having explored the data, we go on to build a model for the dataset

We start by spliting the data into a training and testing dataframes.

Also we identify the predictor variables ['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership'] and the 
Predicted variable 'Yearly Amount Spent'
In [49]:
y = customers['Yearly Amount Spent']
In [50]:
X = customers[['Avg. Session Length', 'Time on App','Time on Website', 'Length of Membership']]

Here, we begin the use of a powerful python Library for machine learning to split, train and evaluate the accuracy of our model.

test_size=0.3 and random_state=101

1. Split the dataset

In [51]:
from sklearn.model_selection import train_test_split
In [52]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
In [53]:
type(y_test)
Out[53]:
pandas.core.series.Series
In [54]:
type(X_train)
Out[54]:
pandas.core.frame.DataFrame

2. Training the Model

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

Using Linear Regression Model

In [55]:
from sklearn.linear_model import LinearRegression
In [56]:
lm = LinearRegression()
In [57]:
lm.fit(X_train,y_train)
Out[57]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

Print out the coefficients of the model

In [58]:
# The coefficients
print('Coefficients: \n', lm.coef_)
Coefficients: 
 [ 25.98154972  38.59015875   0.19040528  61.27909654]

Predicting Test Data

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

In [59]:
predictions = lm.predict( X_test)

Visually evaluating the accuracy predicted values versus the real test values.

In [60]:
plt.scatter(y_test,predictions)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
Out[60]:
<matplotlib.text.Text at 0x20e61b39e48>