#Libraries and Imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.cross_validation import train_test_split

"""
Explanation: Auto-MPG dataset
Authors
Written by: Neeraj Asthana (under Professor Robert Brunner)
University of Illinois at Urbana-Champaign
Summer 2016
Acknowledgements
Dataset found on UCI Machine Learning repository at: http://archive.ics.uci.edu/ml/datasets/Auto+MPG
Dataset Information
This data set tries to predict the mpg (miles per gallon) of a car (continuous) using many different predcitors.
A description of the dataset can be found at: http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.names
Predictors:

cylinders (multi-valued discrete)
displacement (continuous)
horsepower (continuous)
weight (continuous)
acceleration (continuous)
model year (multi-valued discrete)
origin (multi-valued discrete)
car name (string - unique)

Imports
End of explanation
"""


#Names of all of the columns
names = [
       'mpg'
    ,  'cylinders'
    ,  'displacement'
    ,  'horsepower'
    ,  'weight'
    ,  'acceleration'
    ,  'model_year'
    ,  'origin'
    ,  'car_name'
]

#Import dataset
data = pd.read_csv('auto-mpg.data', sep = '\s+', header = None, names = names)

data.head()

data.shape

"""
Explanation: Read Data
End of explanation
"""


#Drop nas (labelled as ? in this dataset) -> dropped 6 rows
data_clean=data.applymap(lambda x: np.nan if x == '?' else x).dropna()

data_clean.shape

"""
Explanation: Clean Data (remove NaNs)
End of explanation
"""


#Select Predictor columns
X = data_clean[['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model_year', "origin"]]

#Select target column
y = data_clean['mpg']

"""
Explanation: Separate predictors from labels
End of explanation
"""


#Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, test_size=0.2)

"""
Explanation: Split into Training and Testing Sets
End of explanation
"""


#Train a simple linear regression model
regr = linear_model.LinearRegression()
regr.fit(X_train, y_train)

#Print coefficients
list(zip(names[1:8], regr.coef_))

"""
Explanation: Train Simple Linear Regression
End of explanation
"""


#Mean Squared error and R-squared on the training set
preds = regr.predict(X_train)
mse = np.mean((preds - y_train) ** 2)
rsq = regr.score(X_train, y_train)

print("Mean Squared Error: %.4f \n R-squared: %.4f" % (mse,rsq))

#Test model on held out test set
#Mean Squared error on the testing set
preds_ = regr.predict(X_test)
mse_ = np.mean((preds_ - y_test) ** 2)
rsq_ = regr.score(X_test, y_test)

print("Mean Squared Error: %.4f \n R-squared: %.4f" % (mse_,rsq_))

"""
Explanation: Model Evaluation
End of explanation
"""


%pylab inline
#Predicted vs. errors plot -> demonstrates an issue with this fit (high bias)
plt.scatter(regr.predict(X_train), regr.predict(X_train)-y_train)
plt.plot([-5,40],[0,0], color = "red")

#place testing data on the plot as well
plt.scatter(regr.predict(X_test), regr.predict(X_test)-y_test, color = "yellow")

"""
Explanation: Diagnostic Plot (errors vs. predicted)
End of explanation
"""


