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).
In :import pandas as pd import numpy, matplotlib.pyplot as plt import seaborn as sns %matplotlib inline
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.
In :customers = pd.read_csv('Ecommerce Customers')
Check the head of customers, and check out its info() and describe() methods.
Address Avatar Avg. Session Length Time on App Time on Website Length of Membership Yearly Amount Spent 0 firstname.lastname@example.org 835 Frank Tunnel\nWrightmouth, MI 82180-9605 Violet 34.497268 12.655651 39.577668 4.082621 587.951054 1 email@example.com 4547 Archer Common\nDiazchester, CA 06566-8576 DarkGreen 31.926272 11.109461 37.268959 2.664034 392.204933 2 firstname.lastname@example.org 24645 Valerie Unions Suite 582\nCobbborough, D... Bisque 33.000915 11.330278 37.110597 4.104543 487.547505 3 email@example.com 1414 David Throughway\nPort Jason, OH 22070-1220 SaddleBrown 34.305557 13.717514 36.721283 3.120179 581.852344 4 firstname.lastname@example.org 14023 Rodriguez Passage\nPort Jacobville, PR 3... MediumAquaMarine 33.330673 12.795189 37.536653 4.446308 599.406092
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
<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 :sns.jointplot(customers['Time on Website'], customers['Yearly Amount Spent'])
Out:<seaborn.axisgrid.JointGrid at 0x111172390>
Do the same but with the Time on App column instead.
In :sns.jointplot(customers['Time on App'], customers['Yearly Amount Spent'])
Out:<seaborn.axisgrid.JointGrid at 0x10f46df98>
Use jointplot to create a 2D hex bin plot comparing Time on App and Length of Membership.
In :sns.jointplot(customers['Time on App'], customers['Length of Membership'], kind='hex')
Out:<seaborn.axisgrid.JointGrid at 0x107575ba8>
Out:<seaborn.axisgrid.PairGrid at 0x113bb9320>
Atma: Inference from pairplot
Based off this plot what looks to be the most correlated feature with Yearly Amount Spent?
Length of membership followed by time on app
Create a linear model plot (using seaborn's lmplot) of Yearly Amount Spent vs. Length of Membership.
In :sns.lmplot('Length of Membership', 'Yearly Amount Spent', data=customers)
Out:<seaborn.axisgrid.FacetGrid at 0x1154add68>
Out:Index(['Email', 'Address', 'Avatar', 'Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership', 'Yearly Amount Spent'], dtype='object')
In :x = customers[['Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership']] y = customers['Yearly Amount Spent']
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
In :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)
In :from sklearn.linear_model import LinearRegression
Create an instance of a LinearRegression() model named lm.
In :lm = LinearRegression()
Train/fit lm on the training data.
In :lm.fit(x_train, y_train)
Out:LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
Print out the coefficients of the model
Out:array([ 25.98154972, 38.59015875, 0.19040528, 61.27909654])
In :pd.DataFrame(lm.coef_, index=x_train.columns, columns=['Coefficients'])
Coefficients Avg. Session Length 25.981550 Time on App 38.590159 Time on Website 0.190405 Length of Membership 61.279097
In :y_predicted = lm.predict(x_test)
Create a scatterplot of the real test values versus the predicted values.
In :plt.scatter(y_test, y_predicted) # plt.title='Fitted vs predicted' plt.xlabel ='Fitted - yearly purchases' plt.ylabel ='Predicted - yearly purchases'
Out:<matplotlib.text.Text at 0x135546320>
In :from sklearn.metrics import mean_absolute_error, mean_squared_error import numpy as np
In :print("MAE: " + str(mean_absolute_error(y_test, y_predicted))) print("MSE: " + str(mean_squared_error(y_test, y_predicted))) print("RMSE: " + str(np.sqrt(mean_squared_error(y_test, y_predicted))))
MAE: 7.22814865343 MSE: 79.813051651 RMSE: 8.93381506698
In :sns.distplot((y_test - y_predicted), bins=50)
Out:<matplotlib.axes._subplots.AxesSubplot at 0x1169e7e80>
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.
Coeffecient Avg. Session Length 25.981550 Time on App 38.590159 Time on Website 0.190405 Length of Membership 61.279097
How can you interpret these coefficients?
Do you think the company should focus more on their mobile app or on their website?
App Answer here