<jupyter_start><jupyter_text>SVM model for Bike sharing dataset<jupyter_code>import pandas as pd
import numpy as np
import os

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

# preprocessing methods
from sklearn.preprocessing import StandardScaler

# accuracy measures and data spliting
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV

from sklearn.svm import SVR

import warnings
import pickle
plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = 15, 7

warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>1. Data import<jupyter_code>DATADIR = '../data/bike/'
MODELDIR = '../checkpoints/bike-sharing/svm/'

data_path = os.path.join(DATADIR, 'bike-sharing-processed.csv')
data = pd.read_csv(data_path)
data.set_index('date', inplace=True)
data.sort_index(inplace=True)
data.head()
plt.plot(data.cnt, '.')
plt.title('Bike sharing count')
plt.xlabel('sample id')
plt.ylabel('count')
plt.show()<jupyter_output><empty_output><jupyter_text>2. Train test split<jupyter_code>y = data[['cnt']].copy()
X = data.drop(columns=['cnt'], axis=1)

print(f'X and y shape:')
print(X.shape, y.shape)
# date selection
datelist = data.index.unique()

# two month data for testset
print(f'Test start date: {datelist[-61]}')
# Train test split : last 60 days for test set

X_train = X[X.index < datelist[-61]]
X_test = X[X.index >= datelist[-61]]

y_train = y[y.index < datelist[-61]]
y_test = y[y.index >= datelist[-61]]

print(f'Size of train and test set respectively:')
print(X_train.shape,X_test.shape, y_train.shape, y_test.shape)<jupyter_output>Size of train and test set respectively:
(670, 12) (61, 12) (670, 1) (61, 1)<jupyter_text>3. Parameter selection using grid search<jupyter_code>grid_param = {'gamma': ['scale'],
          'C' : [0.1, 1, 10, 100],
          'epsilon': [0.1, 0.2, 0.3],
          'shrinking': [True, False],
          'kernel': ['poly', 'rbf']
         }

svm = SVR()
svm = GridSearchCV(estimator=svm, param_grid=grid_param)
svm.fit(X_train, y_train)

print(f'Best Paramters: {svm.best_params_}')
# final parameters
params = {'gamma': 'scale',
          'C' : 100,
          'epsilon': 0.1,
          'shrinking': True,
          'kernel': 'poly'
         }

model = svm = SVR(**params)
model.fit(X_train, y_train)

# save model weights and 
if os.path.exists(MODELDIR):
    pass
else:
    os.makedirs(MODELDIR)

pickle.dump(model, open(os.path.join(MODELDIR, 'svm.model'), 'wb'))
def model_evaluation(X_train, X_test, y_train, y_test):
    
    # predict and tranform to original scale
    y_train_pred = model.predict(X_train)
    y_test_pred = model.predict(X_test)


    # MAE and NRMSE calculation
    train_rmse = np.sqrt(mean_squared_error(y_train, y_train_pred))
    train_mae = mean_absolute_error(y_train, y_train_pred)
    train_nrmse = train_rmse/np.std(y_train.values)

    test_rmse = np.sqrt(mean_squared_error(y_test, y_test_pred))
    test_mae = mean_absolute_error(y_test, y_test_pred)
    test_nrmse = test_rmse/np.std(y_test.values)

    print(f'Training MAE: {np.round(train_mae, 3)}')
    print(f'Trainig NRMSE: {np.round(train_nrmse, 3)}')
    print()

    print(f'Test MAE: {np.round(test_mae)}')
    print(f'Test NRMSE: {np.round(test_nrmse)}')
    
    return y_train_pred, y_test_pred
y_train_pred, y_test_pred = model_evaluation(X_train, X_test, y_train, y_test)<jupyter_output>Training MAE: 855.998
Trainig NRMSE: 0.57

Test MAE: 1857.0
Test NRMSE: 1.0<jupyter_text>6. Result plotting<jupyter_code>plt.plot(y_train.values, label='actual')
plt.plot(y_train_pred, label='predicted')
plt.ylabel('count')
plt.xlabel('sample id')
plt.title('Actual vs Predicted on training data using SVM')
plt.legend()
plt.tight_layout()
plt.show()
plt.plot(y_test.values, label='actual')
plt.plot(y_test_pred, label='predicted')
plt.ylabel('count')
plt.xlabel('sample id')
plt.title('Actual vs Predicted on test data using SVM')
plt.legend()
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1398161586840832, 'fraction_numerical': 0.017658442186744075, 'mean_word_length': 4.97543352601156, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '632889', 'license': 'MIT', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1559, 'n_words': 328, 'path': '02.bike_count/15.bike-count-SVM.ipynb', 'repo_name': 'mkumar73/time-series', 'size': '4134'}