Customer Insights

Investigating customer data to determine how to best encourage customers to spend more for the duration of their customer lifecycle.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
sns.set()

%matplotlib inline
# Load data and confirm it was loaded
customers = pd.read_csv('Fake Ecommerce Customers.csv')
customers.head()
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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
# Take a look at our data's characteristics
customers.describe()
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
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
# Assess the relationship between the time people spend on the website and purchasing
sns.jointplot(x='Time on Website', y='Yearly Amount Spent', data=customers)
<seaborn.axisgrid.JointGrid at 0x1a1ffa9690>

png

# Assess the relationship between the time people spend on the app and purchasing
sns.jointplot(x='Time on App', y='Yearly Amount Spent', data=customers)
<seaborn.axisgrid.JointGrid at 0x1a2091dbd0>

png

# Look for any clear correlations
sns.pairplot(customers)
<seaborn.axisgrid.PairGrid at 0x1a2108c7d0>

png

# Set up independent features as X and the dependent feature as Y 
X = customers[['Avg. Session Length', 'Time on App',
       'Time on Website', 'Length of Membership']]
y = customers['Yearly Amount Spent']
# Set up my testing and training datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Create and train the mobel
lm = LinearRegression()
lm.fit(X_train,y_train)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
#Create some predictions based on the X test dataset
predictions = lm.predict(X_test)
# Compare our predicitons of Y based on the X test dataset to the actual Y test dataset
ax = sns.scatterplot(y=y_test,x=predictions)
ax.set(xlabel='Y Test', ylabel='Predicted Y')
plt.show()

png

# Assess the module performance
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
MAE: 8.097631371748907
MSE: 104.90160739775463
RMSE: 10.24214857330993
# Evaluate the independent features with the most impact on the purchasing amount of a customer
pd.DataFrame(lm.coef_,X.columns,columns=['Coefficient'])
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Coefficient
Avg. Session Length 25.745354
Time on App 38.252715
Time on Website 0.894086
Length of Membership 61.575456