import pandas as pd
import numpy as np

%matplotlib inline
import matplotlib.pyplot as plt

# read the data and set the datetime as the index
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/bikeshare.csv'
bikes = pd.read_csv(url, index_col='datetime', parse_dates=True)

# "count" is a method, so it's best to name that column something else
bikes.rename(columns={'count':'total'}, inplace=True)

bikes.head()

"""
Explanation: Exercise 04
Estimate a regression using the Capital Bikeshare data
Forecast use of a city bikeshare system
We'll be working with a dataset from Capital Bikeshare that was used in a Kaggle competition (data dictionary).
Get started on this competition through Kaggle Scripts
Bike sharing systems are a means of renting bicycles where the process of obtaining membership, rental, and bike return is automated via a network of kiosk locations throughout a city. Using these systems, people are able rent a bike from a one location and return it to a different place on an as-needed basis. Currently, there are over 500 bike-sharing programs around the world.
The data generated by these systems makes them attractive for researchers because the duration of travel, departure location, arrival location, and time elapsed is explicitly recorded. Bike sharing systems therefore function as a sensor network, which can be used for studying mobility in a city. In this competition, participants are asked to combine historical usage patterns with weather data in order to forecast bike rental demand in the Capital Bikeshare program in Washington, D.C.
End of explanation
"""


bikes.shape

"""
Explanation: datetime - hourly date + timestamp  
season -  
1 = spring
2 = summer 
3 = fall 
4 = winter 
holiday - whether the day is considered a holiday
workingday - whether the day is neither a weekend nor holiday
weather - 
1: Clear, Few clouds, Partly cloudy, Partly cloudy 
2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist 
3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds 
4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog 
temp - temperature in Celsius
atemp - "feels like" temperature in Celsius
humidity - relative humidity
windspeed - wind speed
casual - number of non-registered user rentals initiated
registered - number of registered user rentals initiated
total - number of total rentals
End of explanation
"""


# Pandas scatter plot
bikes.plot(kind='scatter', x='temp', y='total', alpha=0.2)

feature_cols = ['temp']
X1 = bikes[feature_cols]
Y1 = bikes.total

from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import LinearRegression

clf1 = LinearRegression()
clf1.fit(X1, Y1)
clf1.predict(X1)

print(clf1.coef_)
print(clf1.intercept_)

"""
Explanation: Exercise 4.1
What is the relation between the temperature and total?
For a one percent increase in temperature how much the bikes shares increases?
Using sklearn estimate a linear regression and predict the total bikes share when the temperature is 31 degrees
End of explanation
"""


prediction=clf1.intercept_+(clf1.coef_*31)
prediction

"""
Explanation: La relación entre la temperatura y el total es directamente proporcional. Podemos observar en la primer grafica que a medida que aumenta la temperatura, el numero total de bicicletas rentadas aumenta también, esto lo podemos corroborar con el coeficiente del modelo lineal de regresion (B1) que al ser positivo indica que cuando la variable X (Temp) aumenta, la variable Y (Total) lo hace también.
Si la temperatura aumenta en 1 unidad, tenemos un incremento de 9 unidades en el total de ciclas rentadas.
End of explanation
"""


Y1_pred=clf1.predict(X1)
Y1_pred

from sklearn import metrics
import numpy as np
print('MSE:', metrics.mean_squared_error(Y1, Y1_pred))


"""
Explanation: El total de bibiclitas rentadas cuando la temperatura es de 31º es de 290 unds.
Exercise 04.2
Evaluate the model using the MSE
End of explanation
"""


bikes["temp_conv"]=(bikes.temp*(9/5))+32
bikes.head()

feature_cols2 = ['temp_conv']
X2 = bikes[feature_cols]
Y2= bikes.total

clf2 = LinearRegression()
clf2.fit(X2, Y2)
Y2_pred=clf2.predict(X2)
Y2_pred

Y1_pred-Y2_pred

"""
Explanation: Exercise 04.3
Does the scale of the features matter?
Let's say that temperature was measured in Fahrenheit, rather than Celsius. How would that affect the model?
End of explanation
"""


bikes['temp2']=bikes.temp**2
bikes.head()

feature_cols3 = ['temp','temp2']
X3 = bikes[feature_cols]
Y3 = bikes.total

clf3 = LinearRegression()
clf3.fit(X3, Y3)
clf3.coef_,clf3.intercept_

Y3_pred=clf3.predict(X3)
Y3_pred

"""
Explanation: Cómo se ve la diferencia entre las predicciones o valores estimados de la regresión con la temperatura en grados Celsius y los grados Fahrenheit, es muy cercana a cero casi nula. Es decir, que a pesar que la escala de la temperatura esté diferente y la escala cambie, las predicciones no van a tener ninguna variación. 
Exercise 04.4
Run a regression model using as features the temperature and temperature$^2$ using the OLS equations
End of explanation
"""


# explore more features
feature_cols = ['temp', 'season', 'weather', 'humidity']

# multiple scatter plots in Pandas
fig, axs = plt.subplots(1, len(feature_cols), sharey=True)
for index, feature in enumerate(feature_cols):
    bikes.plot(kind='scatter', x=feature, y='total', ax=axs[index], figsize=(16, 3))

"""
Explanation: Exercise 04.5
Data visualization.
What behavior is unexpected?
End of explanation
"""


# pivot table of season and month
month = bikes.index.month
pd.pivot_table(bikes, index='season', columns=month, values='temp', aggfunc=np.count_nonzero).fillna(0)

# box plot of rentals, grouped by season
bikes.boxplot(column='total', by='season')

# line plot of rentals
bikes.total.plot()

"""
Explanation: Are you seeing anything that you did not expect?
seasons: 
  *  1 = spring
  * 2 = summer 
  * 3 = fall 
  * 4 = winter
End of explanation
"""


feature_cols4 = ['temp', 'season', 'weather', 'humidity']
X4 = bikes[feature_cols]
Y4 = bikes.total

clf4 = LinearRegression()
clf4.fit(X4, Y4)
clf4.coef_,clf4.intercept_

clf1.score(X1,Y1,sample_weight=None), clf4.score(X4,Y4,sample_weight=None)

Y4_pred=clf4.predict(bikes[['temp', 'season', 'weather', 'humidity']])
Y4_pred

print('MSE:', metrics.mean_squared_error(Y1, Y1_pred))

print('MSE:', metrics.mean_squared_error(Y4, Y4_pred))

"""
Explanation: El comportamiento inesperado es ver que se rentan más bicicletas en invierno. Uno pensaría que en las demás estaciones es cuando más rentan bicicletas, pero las gráficas nos dicen lo contrario. Podemos ver en los Boxplot que el promedio más alto es de "winter" y podemos ver en la gráfica anterior que en el mes de Octubre es cuando mas se rentan bicicletas y este mes corresponde a invierno. Otra razon que tenemos para decir que es un comportamiento inesperado es que en el primer punto de este taller nos pudimos dar cuenta que la relacion de la temperatura con el total de bicicletas rentadas es directamente proporcional entonces a más temperatura más bicicletas se rentan y este comportamiento no se ve gráficamente.
Exercise 04.6
Estimate a regression using more features ['temp', 'season', 'weather', 'humidity'].
How is the performance compared to using only the temperature?
End of explanation
"""


import numpy as np
from sklearn.cross_validation import train_test_split
X4_train, X4_test, Y4_train, Y4_test = train_test_split(X4, Y4, test_size=0.35, random_state=666)
print(Y4_train.shape, Y4_test.shape)

feature_cols = ['temp', 'season', 'weather']
X5 = bikes[feature_cols]
Y5 = bikes.total

import numpy as np
from sklearn.cross_validation import train_test_split
X5_train, X5_test, Y5_train, Y5_test = train_test_split(X5, Y5, test_size=0.35, random_state=666)
print(Y5_train.shape, Y5_test.shape)

feature_cols = ['temp', 'season', 'humidity']
X6 = bikes[feature_cols]
Y6 = bikes.total

import numpy as np
from sklearn.cross_validation import train_test_split
X6_train, X6_test, Y6_train, Y6_test = train_test_split(X6, Y6, test_size=0.35, random_state=333)
print(Y6_train.shape, Y6_test.shape)

clf4 = LinearRegression()
clf4.fit(X4_train, Y4_train)

clf5 = LinearRegression()
clf5.fit(X5_train, Y5_train)

clf6 = LinearRegression()
clf6.fit(X6_train, Y6_train)

Y4_pred=clf4.predict(X4_test)
(Y4_test == Y4_pred).mean()

Y4_test

Y4_pred

Y5_pred=clf5.predict(X5_test)
(Y5_test == Y5_pred).mean()

Y6_pred=clf6.predict(X6_test)
(Y6_test == Y6_pred).mean()

print('MSE:', metrics.mean_squared_error(Y4_test, Y4_pred)),
print('MSE:', metrics.mean_squared_error(Y5_test, Y5_pred)),
print('MSE:', metrics.mean_squared_error(Y6_test, Y6_pred))

"""
Explanation: Exercise 04.7 (3 points)
Split randomly the data in train and test
Which of the following models is the best in the testing set?
* ['temp', 'season', 'weather', 'humidity']
* ['temp', 'season', 'weather']
* ['temp', 'season', 'humidity']
End of explanation
"""


