import math
import numpy
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error


cars_names = ['mpg','cylinders','displacement','horsepower',
              'weight','acceleration','model year','origin','car name']

cars = pd.read_table("auto-mpg.data.txt", delim_whitespace=True, names = cars_names)

#Size of the Dataset
cars.shape

cars.columns

cars.head(5)

cars.info()

#horsepower have to be numeric
#horsepower have 6 rows with the value '?'
#drop this rows and 

cars = cars[cars['horsepower'] != '?' ]
cars['horsepower'] = cars['horsepower'].astype('float32')


import seaborn as sns

cars = cars[['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
       'acceleration', 'model year', 'origin']]

#for i in c:
#    cars[i].hist()
#    plt.xlabel(i)
#    plt.show()

sns.pairplot(cars)

"""
Explanation: <h1>Exploration of Auto MPG</h1>
End of explanation
"""


c = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
       'acceleration', 'model year', 'origin']
limit = int(3*cars.shape[0]/4)
X_train = cars.iloc[:limit,1:]
y_train = cars.iloc[:limit,0]
X_test = cars.iloc[limit:,1:]
y_test = cars.iloc[limit:,0]


lr = LinearRegression()

#Remember the [[]], is the data in 1 column
lr.fit(X_train, y_train)
predictions = lr.predict(X_test)

#mean_squared_error
mse = mean_squared_error(y_test, predictions)

# The coefficients
print('Coefficients: ', lr.coef_)
# The mean square error
print("Residual sum of squares: %.2f" % mse)
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % lr.score(X_test, y_test))

plt.scatter(X_test["weight"], y_test, c='b')
plt.scatter(X_test["weight"], prediction, c='r')


plt.show()

"""
Explanation: <h2>Linear Regression</h2>
End of explanation
"""


c = ['mpg','horsepower', "weight",'acceleration']
limit = int(3*cars.shape[0]/4)
X_train = cars[c].iloc[:limit,1:]
y_train = cars.iloc[:limit,0]
X_test = cars[c].iloc[limit:,1:]
y_test = cars.iloc[limit:,0]

lr2 = LinearRegression()

#Remember the [[]], is the data in 1 column
lr2.fit(X_train, y_train)
predictions = lr2.predict(X_test)

#mean_squared_error
mse = mean_squared_error(y_test, predictions)

# The coefficients
print('Coefficients: ', lr2.coef_)
# The mean square error
print("Residual sum of squares: %.2f" % mse)
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % lr2.score(X_test, y_test))

feature = "weight"

plt.scatter(X_test[feature], y_test, c='b')
plt.scatter(X_test[feature], prediction, c='r')

plt.ylim([10,45])
plt.xlim([1500,4000])
plt.ylabel('MPG')
plt.xlabel(feature)
plt.show()

"""
Explanation: <p>The variance score is very low due to we use all the features.</p>
<p>For a better solution we aplied a feature selection. </p>
<p>From the pair graph, we can see that the features 'cylinders', 'model year' and 'origin' dont'show a clear correlation with the 'mpg' variable, so those are rejected.</p>
End of explanation
"""


