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!

** 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
```

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]:

In [4]:

```
customers.describe()
```

Out[4]:

**Getting more information on the quality of the data provided**

In [36]:

```
customers.info()
```

**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

**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]:

** 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]:

** 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]:

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

In [41]:

```
sns.pairplot(customers)
```

Out[41]:

In [42]:

```
customers.columns
```

Out[42]:

In [43]:

```
#Which Column correlates most with the "Yearly Amount Spent" == "Length of Membership" with 0.809084
customers.corr()
```

Out[43]:

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]:

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**

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]:

In [54]:

```
type(X_train)
```

Out[54]:

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]:

**Print out the coefficients of the model**

In [58]:

```
# The coefficients
print('Coefficients: \n', lm.coef_)
```

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]: