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

This data analysis should provide insights into what factors affect the Membership. Specifically, should the company focus on mobile app or website development?


In [2]:
ecomCustomers = pd.read_csv("Ecommerce Customers")

In [3]:
ecomCustomers.head()


Out[3]:
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]:
ecomCustomers.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

In [5]:
ecomCustomers.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

In [6]:
sns.jointplot(ecomCustomers['Time on Website'], ecomCustomers['Yearly Amount Spent'])


Out[6]:
<seaborn.axisgrid.JointGrid at 0x7f81893d39e8>

In [7]:
sns.jointplot(ecomCustomers['Time on App'], ecomCustomers['Yearly Amount Spent'])


Out[7]:
<seaborn.axisgrid.JointGrid at 0x7f8185b422e8>

In [8]:
sns.jointplot(ecomCustomers['Time on App'], ecomCustomers['Length of Membership'], kind='hex')


Out[8]:
<seaborn.axisgrid.JointGrid at 0x7f8185b60ac8>

In [9]:
sns.pairplot(ecomCustomers)


Out[9]:
<seaborn.axisgrid.PairGrid at 0x7f8185b427f0>

Time on App and Length of Membership look to have some positive correlation with Yearly Amount Spent

All of the variables look approximately normally distributed


In [10]:
sns.lmplot(x="Length of Membership", y = "Yearly Amount Spent", data=ecomCustomers)


Out[10]:
<seaborn.axisgrid.FacetGrid at 0x7f81842be6a0>

In [11]:
X = ecomCustomers.select_dtypes(include = ['float64'])

In [12]:
X = X.drop('Yearly Amount Spent', axis = 1)

In [13]:
y = ecomCustomers["Yearly Amount Spent"]

In [14]:
from sklearn.model_selection import train_test_split

In [15]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 101)

In [16]:
def printShapes(df_list):
    for df in df_list:
        print(df.shape)

In [17]:
df_list = [X_train, y_train, X_test, y_test]
printShapes(df_list)


(350, 4)
(350,)
(150, 4)
(150,)

In [18]:
from sklearn.linear_model import LinearRegression

In [19]:
lm = LinearRegression()

In [20]:
lm.fit(X_train, y_train)


Out[20]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

In [21]:
print(lm.coef_)


[ 25.98154972  38.59015875   0.19040528  61.27909654]

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

In [23]:
plt.scatter(predictions, y_test)


Out[23]:
<matplotlib.collections.PathCollection at 0x7f81800f50f0>

In [24]:
from sklearn.metrics import mean_absolute_error, mean_squared_error

In [25]:
mae = mean_absolute_error(y_test, predictions)
print("MAE = ", mae)


MAE =  7.22814865343

In [26]:
mse = mean_squared_error(y_test, predictions)
print("MSE = ", mse)


MSE =  79.813051651

In [27]:
rmse = np.sqrt(mse)
print("RMSE = ", rmse)


RMSE =  8.93381506698

In [28]:
sns.distplot((y_test - predictions), bins = 50 )


Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f818011def0>

In [29]:
coeff_df = pd.DataFrame(lm.coef_, X.columns, columns=["Coefficient"])
coeff_df


Out[29]:
Coefficient
Avg. Session Length 25.981550
Time on App 38.590159
Time on Website 0.190405
Length of Membership 61.279097

In [30]:
appOverWebsite = coeff_df["Coefficient"]["Time on App"] / coeff_df["Coefficient"]["Time on Website"]
print("Time on App affects Yearly Amount Spent {0:.2f}x more than Time on Website".format(appOverWebsite))


Time on App affects Yearly Amount Spent 202.67x more than Time on Website

If other conditions are controlled and/or considered, the company should focus on:

mobile app development.