import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

"""
Explanation: Linear Regression - Project Exercise
In this project, we are going to analyze customer data from an online store and decide whether or not this company should invest more on their website or mobile app, based on the customer behavior.
Imports
End of explanation
"""


customers =  pd.read_csv('Ecommerce Customers')

"""
Explanation: Get the Data
End of explanation
"""


customers.head()

customers.describe()

customers.info()

"""
Explanation: Checking the head of customers, and checking out its info() and describe() methods
End of explanation
"""


sns.jointplot(x='Time on Website', y='Yearly Amount Spent', data=customers, color='green')

"""
Explanation: Exploratory Data Analysis
Creating a jointpolot with seaborn to compare user time on website and yearly amount spent. Is there any correlation?
End of explanation
"""


sns.jointplot(x='Time on App', y='Yearly Amount Spent', data=customers, color='green')

"""
Explanation: The same for time on app
End of explanation
"""


sns.jointplot(x='Time on App', y='Length of Membership', data=customers, kind='hex')

"""
Explanation: Using jointplot to create a 2D hex bin plot comparing Time on App and Length of Membership
End of explanation
"""


sns.pairplot(customers)

"""
Explanation: Using a pairplot to explore patterns in the entire dataset
End of explanation
"""


sns.lmplot(x='Length of Membership', y='Yearly Amount Spent', data=customers)

"""
Explanation: Length of membership seems to be the most correlated feature with Yearly Amount Spent
Creating a linear model plot of  Yearly Amount Spent vs. Length of Membership
End of explanation
"""


customers.columns

X = customers[['Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership']]

y = customers['Yearly Amount Spent']

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)

"""
Explanation: Training and Testing Data
End of explanation
"""


from sklearn.linear_model import LinearRegression

lm = LinearRegression()

lm.fit(X_train, y_train)

"""
Explanation: Training the Model
Time to train our model on our training data!
End of explanation
"""


print(lm.coef_)

"""
Explanation: Print out the coefficients of the model
End of explanation
"""


predictions = lm.predict(X_test)

predictions

"""
Explanation: Predicting Test Data
End of explanation
"""


plt.scatter(predictions, y_test)
plt.xlabel('Y test')
plt.ylabel('Predicted Y')

"""
Explanation: Create a scatterplot of the real test values versus the predicted values.
End of explanation
"""


from sklearn import metrics

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

"""
Explanation: Evaluating the Model
End of explanation
"""


sns.distplot((y_test - predictions), bins=50)

"""
Explanation: Residuals
End of explanation
"""


df = pd.DataFrame(lm.coef_, X.columns, columns=['Coefficient'])

df

"""
Explanation: Conclusion
Do we focus our efforts on the app or on the development of the website?
End of explanation
"""


