<jupyter_start><jupyter_text>Bikes Assessment  ObjectiveThe objective of the project is - using historical usage patterns and weather data, forecast(predict) bike rental demand (number of bike users (‘cnt’)) on hourly basis.Use the provided “Bikes Rental” data set to predict the bike demand (bike users count - 'cnt') using various best possible models (ML algorithms). Also, report the model that performs best, fine-tune the same model using one of the model fine-tuning techniques, and report the best possible combination of hyperparameters for the selected model. Lastly, use the selected model to make final predictions and compare the predicted values with the actual values.  Data DescriptionBelow are the details of the features list for the given Bikes data set:1. instant: record index2. dteday : date3. season: season (1: springer, 2: summer, 3: fall, 4: winter)4. yr: year (0: 2011, 1:2012)5. mnth: month (1 to 12)6. hr: hour (0 to 23)7. holiday: whether the day is a holiday or not8. weekday: day of the week9. workingday: if day is neither weekend nor holiday is 1, otherwise is 0.10. 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 + Fog11. temp: Normalized temperature in Celsius. The values are derived via (tt_min)/(t_maxt_min), t_min=*8, t_max=+39 (only in hourly scale)12. atemp: Normalized feeling temperature in Celsius. The values are derived via (tt_min)/(t_maxt_min), t_min=*16, t_max=+50 (only in hourly scale)13. hum: Normalized humidity. The values are divided to 100 (max)14. windspeed: Normalized wind speed. The values are divided to 67 (max)15. casual: count of casual users16. registered: count of registered users17. cnt: count of total rental bikes including both casual and registered usersThe "target" data set ('y') should have only one 'label' i.e. 'cnt'.  Solution   Libraries<jupyter_code># Standard Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Preprocessing libraries
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler 
from sklearn.model_selection import train_test_split

#Hypter Tuning libraries 
from sklearn.model_selection import GridSearchCV

# Model Evaluation libraies
from sklearn.metrics import mean_squared_error 
from sklearn.model_selection import cross_val_predict, cross_val_score


# Model Libraries
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from xgboost import XGBRegressor<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>train_data = pd.read_csv('Datasets/Bike-Sharing/hour.csv')
train_data.head(2)
train_data.info()
train_data.describe()<jupyter_output><empty_output><jupyter_text>We can see that instant, casual , registered, atemp and dteday are not important features so we will proceed to drop them   Preprocessing<jupyter_code>columnstodrop = ['instant','dteday', 'atemp', 'registered','casual']
columnstodrop
train_data = train_data.drop(columnstodrop, axis = 1)
train_data.head()<jupyter_output><empty_output><jupyter_text>Few features are needed to be scaled because they have different ranges ('temp', 'hum', 'windspeed')<jupyter_code>columnstoscale = ['temp', 'hum', 'windspeed']
scalar = StandardScaler()
train_data[columnstoscale] = scalar.fit_transform(train_data[columnstoscale])
train_data.describe()
train_data['dayCount'] = pd.Series(range(train_data.shape[0]))/24
train_data.head()
# Split data into train_set and test_set 

np.random.seed(42)
train_set, test_set = train_test_split(train_data, test_size = 0.30, random_state = 42)

train_set.sort_values('dayCount', axis= 0, inplace=True) 
test_set.sort_values('dayCount', axis= 0, inplace=True)
print(len(train_set), "train +", len(test_set), "test")
def display_scores(scores):
    print("Scores:", scores)
    print("Mean:", scores.mean())
    print("Standard deviation:", scores.std())
trainingCols = train_set.drop('cnt', axis = 1)
trainLabels = train_set['cnt']<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code># Create a decision tree regressor
np.random.seed(42)
dec_reg = DecisionTreeRegressor()
dt_mae_scores = -cross_val_score(dec_reg, trainingCols, trainLabels, cv = 10, scoring = "neg_mean_absolute_error")
display_scores(dt_mae_scores)
dt_mse_scores = -cross_val_score(dec_reg, trainingCols, trainLabels, cv = 10, scoring = "neg_mean_squared_error")
display_scores(dt_mae_scores)
# Create a Linear regressor model
np.random.seed(42)
lin_reg = LinearRegression()
lr_mae_scores = -cross_val_score(lin_reg,trainingCols, trainLabels, cv = 10, scoring = "neg_mean_absolute_error")
display_scores(lr_mae_scores)
lr_mse_scores = np.sqrt(-cross_val_score(lin_reg,trainingCols, trainLabels, cv = 10, scoring = "neg_mean_squared_error"))
display_scores(lr_mse_scores)
# Create model for random forest regressor 

np.random.seed(42)
forest_reg = RandomForestRegressor()
rf_mae_scores = -cross_val_score(forest_reg,trainingCols, trainLabels, cv = 10, scoring = "neg_mean_absolute_error")
display_scores(rf_mae_scores)
rf_mse_scores = np.sqrt(-cross_val_score(forest_reg,trainingCols, trainLabels, cv = 10, scoring = "neg_mean_squared_error"))
display_scores(rf_mse_scores)<jupyter_output>Scores: [ 47.49185074  52.30467686  44.19671758  52.31801493  60.81876308
  94.05821741  66.28927183  65.76846101  60.72060819 134.9296847 ]
Mean: 67.88962663218157
Standard deviation: 25.981543248399895<jupyter_text>Hyper Tuning of RandomForestRegressor<jupyter_code># initialize paramters for hyper tuning
param_grid = [
    {'n_estimators': [120, 150], 'max_features': [10, 12], 'max_depth': [15, 28]},
]
grid_search = GridSearchCV(forest_reg, param_grid, cv = 5, scoring = "neg_mean_squared_error")
grid_search.fit(trainingCols, trainLabels)
# Print the best estimator and parameters
print(grid_search.best_estimator_)
print(grid_search.best_params_)
# To print the feature importances 

featureimportance = grid_search.best_estimator_.feature_importances_
print(featureimportance)<jupyter_output>[0.00506077 0.00463056 0.00573747 0.57641289 0.00205046 0.01775569
 0.0729443  0.016771   0.09549576 0.03463266 0.00944615 0.15906227]<jupyter_text>Predicting for test data<jupyter_code>final_model = grid_search.best_estimator_
test_set.sort_values('dayCount', axis= 0, inplace=True)
test_x_cols = (test_set.drop(['cnt'], axis=1)).columns.values
test_y_cols = 'cnt'
X_test = test_set.loc[:,test_x_cols]
y_test = test_set.loc[:,test_y_cols]
test_set.loc[:,'predictedCounts_test'] = final_model.predict(X_test)
mse = mean_squared_error(y_test, test_set.loc[:,'predictedCounts_test'])
final_mse=np.sqrt(mse)
print(final_mse)
times = [9,18]
for time in times:
    fig = plt.figure(figsize=(8, 6))
    fig.clf()
    ax = fig.gca()
    test_set_freg_time = test_set[test_set.hr == time]
    test_set_freg_time.plot(kind = 'line', x = 'dayCount', y = 'cnt', ax = ax)
    test_set_freg_time.plot(kind = 'line', x = 'dayCount', y = 'predictedCounts_test', ax =ax)
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10133369998625051, 'fraction_numerical': 0.048948164443833354, 'mean_word_length': 6.47584789311408, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '434620', 'license': 'MIT', 'n_tokens_mistral': 2712, 'n_tokens_neox': 2408, 'n_words': 666, 'path': 'Solutions/Bikes-Assessment.ipynb', 'repo_name': 'Anjitha95/ML_projects', 'size': '7273'}