<jupyter_start><jupyter_text>https://www.youtube.com/watch?v=eEGbTd7AG9s<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

from keras.models import Sequential
from keras.layers import Dense, Activation, Embedding, Flatten, LeakyReLU, Merge, BatchNormalization, Dropout
from keras.activations import relu, sigmoid
from keras.layers import LeakyReLU

from sklearn.preprocessing import StandardScaler

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Description of the data:- instant: record index- dteday : date- season : season (1:springer, 2:summer, 3:fall, 4:winter)- yr : year (0: 2011, 1:2012)- mnth : month ( 1 to 12)- hr : hour (0 to 23)- holiday : weather day is holiday or not (extracted from [Web Link])- weekday : day of the week- workingday : if day is neither weekend nor holiday is 1, otherwise is 0.+ weathersit :   - 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 : Normalized temperature in Celsius. The values are derived via (t-t_min)/(t_max-t_min), t_min=-8, t_max=+39 (only in hourly scale)- atemp: Normalized feeling temperature in Celsius. The values are derived via (t-t_min)/(t_max-t_min), t_min=-16, t_max=+50 (only in hourly scale)- hum: Normalized humidity. The values are divided to 100 (max)- windspeed: Normalized wind speed. The values are divided to 67 (max)- casual: count of casual users- registered: count of registered users- cnt: count of total rental bikes including both casual and registered<jupyter_code>rides = pd.read_csv('hour.csv')
rides['time'] = pd.to_datetime(rides['dteday'])+pd.Series([pd.DateOffset(hours = a ) for a in rides.hr.tolist()])
rides.drop(['instant','yr','dteday','casual','registered', 'atemp'],1,inplace=True)
rides['t'] = rides.time.values.astype(float)
rides.head()<jupyter_output>/Users/sachin/anaconda/lib/python3.5/site-packages/pandas/core/ops.py:539: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized
  "Series not vectorized", PerformanceWarning)<jupyter_text>Get **one hot encoded** values of the categorical variables:<jupyter_code>cat_features = ['season', 'hr', 'mnth', 'holiday', 'weekday', 'workingday', 'weathersit']
rides = pd.get_dummies(rides, columns=cat_features)
rides.head()
plt.plot(rides.time[:240],rides.cnt[:240])
plt.show()
quant_features = ['temp', 'hum', 'windspeed','t']
x_scaler = StandardScaler()
rides[quant_features] = x_scaler.fit_transform(rides[quant_features].values)

y_scaler = StandardScaler()
rides['cnt'] = y_scaler.fit_transform(rides['cnt'].values[:,None])

rides.head()<jupyter_output>/Users/sachin/anaconda/lib/python3.5/site-packages/sklearn/utils/validation.py:429: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, _DataConversionWarning)<jupyter_text>Initial model (1 hidden layer with Relu activations) Split data into testing and training<jupyter_code>train_data = rides[:-21*24]
test_data = rides[-21*24:]

train_x = train_data.drop(['cnt','time'],axis=1).values
test_x = test_data.drop(['cnt','time'],axis=1).values
train_x.shape
model = Sequential()
model.add(Dense(train_x.shape[1]//2,input_dim=train_x.shape[1]))
model.add(Activation('relu'))
model.add(Dense(1))
model.compile(optimizer='adadelta', loss='mse')
# Train the model
model.fit(train_x, train_data['cnt'], batch_size=256, epochs=11)
model.summary()
pred_y = model.predict(test_x, batch_size=256)
plt.figure(figsize=(12,5))
plt.plot(test_data.time, y_scaler.inverse_transform(test_data.cnt), label='Actual')
plt.plot(test_data.time, y_scaler.inverse_transform(pred_y), label='Prediction')
plt.legend()
plt.show()
model.evaluate(test_x, test_data['cnt'], batch_size=256)
#! pip install graphviz
#! pip install pydot-ng<jupyter_output><empty_output><jupyter_text>Uncomment the pip commands above if you want to get the image below. i.e. If the command below chucks an error uncomment the commands above.<jupyter_code># from IPython.display import SVG
# from keras.utils.vis_utils import model_to_dot

# SVG(model_to_dot(model).create(prog='dot', format='svg'))
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 29)                1740      
_________________________________________________________________
activation_1 (Activation)    (None, 29)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 30        
=================================================================
Total params: 1,770.0
Trainable params: 1,770.0
Non-trainable params: 0.0
_________________________________________________________________<jupyter_text>Two Layer hidden network<jupyter_code>train_x.shape
h_nodes = [40, 20]
model = Sequential()

# The two hidden layers (first layer needs the dimensions of x)
model.add(Dense(h_nodes[0],input_dim=train_x.shape[1]))
model.add(Activation(relu))
model.add(Dense(h_nodes[1]))
model.add(Activation('relu'))

model.add(Dense(1))
model.compile(optimizer='adadelta', loss='mse')
model.fit(train_x, train_data['cnt'], batch_size=256, epochs=11)
model.evaluate(test_x, test_data['cnt'], batch_size=256)
model.summary()
# SVG(model_to_dot(model).create(prog='dot', format='svg'))<jupyter_output><empty_output><jupyter_text>Best Hyper parameters<jupyter_code>from IPython.display import HTML

HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/eEGbTd7AG9s?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>')
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import GridSearchCV<jupyter_output><empty_output><jupyter_text>What is Cross Validation?https://stats.stackexchange.com/questions/1826/cross-validation-in-plain-english<jupyter_code># import keras.backend as K

def create_model(layers, activation):
    model = Sequential()
    for i, nodes in enumerate(layers):
        if i==0:
            model.add(Dense(nodes,input_dim=train_x.shape[1]))
            model.add(Activation(activation))
        else:
            model.add(Dense(nodes))
            model.add(Activation(activation))
    model.add(Dense(1)) # Note: no activation beyond this point
    
    model.compile(optimizer='adadelta', loss='mse')
    return model
    
model = KerasRegressor(build_fn=create_model, verbose=0)    
layers = [[30], [40, 20], [45, 30, 15]]
activations = [sigmoid, relu]
param_grid = dict(layers=layers, activation=activations, batch_size = [128, 256], epochs=[30])
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring='neg_mean_squared_error')
grid_result = grid.fit(train_x, train_data['cnt'].values)
[grid_result.best_score_, grid_result.best_params_]
for params, mean_score, scores in grid_result.grid_scores_:
    print("%f (%f) with: %r" % (scores.mean(), scores.std(), params))<jupyter_output>-0.354904 (0.155346) with: {'batch_size': 128, 'layers': [30], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.327239 (0.149877) with: {'batch_size': 128, 'layers': [40, 20], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.316260 (0.161740) with: {'batch_size': 128, 'layers': [45, 30, 15], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.376903 (0.187427) with: {'batch_size': 256, 'layers': [30], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.350373 (0.192372) with: {'batch_size': 256, 'layers': [40, 20], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.371596 (0.221870) with: {'batch_size': 256, 'layers': [45, 30, 15], 'activation': <function sigmoid at 0x112e911e0>, 'epochs': 30}
-0.172043 (0.052121) with: {'batch_size': 128, 'layers': [30], 'activation': <function relu at 0x112e910d0>, 'epochs': 30}
-0.152897 (0.074760) with: {'batch_size': 128, 'layers': [40, 20], 'activation': <function relu a[...]<jupyter_text>Finally predict the values on the actual test set with given params:<jupyter_code>pred_y = grid.predict(test_x)
error = test_data['cnt'] - pred_y
np.mean(np.square(error))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup