<jupyter_start><jupyter_text>Forecasting using SVM- One-step recursive forecasting<jupyter_code>import pandas as pd
import numpy as np
import os
import time

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


# 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

from sklearn.preprocessing import StandardScaler

import graphviz
import warnings
warnings.filterwarnings('ignore')
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = 15, 7<jupyter_output><empty_output><jupyter_text>1. Data import<jupyter_code>DATADIR = '../data/power-consumption/'
MODELDIR = '../checkpoints/power/svm/model/'
# directory for saving model 
if os.path.exists(MODELDIR):
    pass
else:
    os.makedirs(MODELDIR)
data = pd.read_csv(os.path.join(DATADIR, 'processed_data.csv'))
data.head()<jupyter_output><empty_output><jupyter_text>2. Train test split<jupyter_code>y = data[['Global_active_power']].copy()
X = data.drop(columns=['date', 'Global_active_power', 'Median_active_power', 'Lagged_active_power', 'median_residual'], axis=1)

# last 40 weeks data for testing
test_size = np.int16(7*1)
train_size = X.shape[0] - test_size

# scaling weekly sales, dependent variable
y_scaler = StandardScaler()
y_scaled = y_scaler.fit_transform(y.values)

# scaling numerical column from features
x_scaler = StandardScaler()
X_scaled = x_scaler.fit_transform(X.values)

X_train, X_test = X_scaled[:train_size-1, :], X_scaled[train_size:, :]
y_train, y_test = y_scaled[:train_size-1, :], y_scaled[train_size:, :]

X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>3. Grid search<jupyter_code># grid_param = {
#     'C': [0.1, 1, 10, 100],
#     'epsilon' : [0.1, 0.2, 0.3]
# }

# svm = SVR(gamma='scale', cache_size=5120)
# svm_grid = GridSearchCV(estimator=svm, param_grid=grid_param)
# svm_grid.fit(X_train, y_train)
# print(f'Best Paramters: {svm_grid.best_params_}')<jupyter_output><empty_output><jupyter_text>4. Model fitting<jupyter_code>def svm_model(X_train, y_train):
    
    start = time.time()
    
    model = SVR(kernel='poly', epsilon=0.1, C=100, cache_size=5120, degree=3)
    
    model.fit(X_train, y_train)
    y_train_pred = model.predict(X_train)
    
    onestep_pred = []
    for i in range(X_test.shape[0]):
        model.fit(X_train, y_train)

        pred = model.predict(X_test[i, :].reshape(1, -1))
        onestep_pred.append(pred)

        tempX = np.vstack((X_train, X_test[i, :]))    
        X_train_v = tempX.copy()

        arr_pred = np.array([pred]).reshape(-1, 1)
        tempY = np.vstack((y_train, arr_pred))
        y_train_v = tempY.copy()
    
    y_test_pred = np.array(onestep_pred)
    
    end = time.time()
    time_taken = np.round((end-start), 3)
    print(f'Time taken to complete the process: {time_taken} seconds')
    
    return y_train_pred, y_test_pred
y_train_pred, y_test_pred = svm_model(X_train, y_train)<jupyter_output>Time taken to complete the process: 125.457 seconds<jupyter_text>5. Model evaluation<jupyter_code>def model_evaluation(y_train, y_test, y_train_pred, y_test_pred):
    
    y_train_inv = y_scaler.inverse_transform(y_train)
    y_test_inv = y_scaler.inverse_transform(y_test)
    
    y_train_pred_inv = y_scaler.inverse_transform(y_train_pred)
    y_test_pred_inv = y_scaler.inverse_transform(y_test_pred)
    
    # MAE and NRMSE calculation
    train_rmse = np.sqrt(mean_squared_error(y_train_inv, y_train_pred_inv))
    train_mae = np.round(mean_absolute_error(y_train_inv, y_train_pred_inv), 3)
    train_nrmse = np.round(train_rmse/np.std(y_train_inv), 3)

    test_rmse = np.sqrt(mean_squared_error(y_test_inv, y_test_pred_inv))
    test_mae = np.round(mean_absolute_error(y_test_inv, y_test_pred_inv), 3)
    test_nrmse = np.round(test_rmse/np.std(y_test_inv), 3)

    print(f'Training MAE: {train_mae}')
    print(f'Trainig NRMSE: {train_nrmse}')

    print(f'Test MAE: {test_mae}')
    print(f'Test NRMSE: {test_nrmse}')
    
    return y_train_inv, y_train_pred_inv, y_test_inv, y_test_pred_inv
y_train_inv, y_train_pred_inv, y_test_inv, y_test_pred_inv = model_evaluation(y_train, y_test, y_train_pred, y_test_pred)
plt.plot(y_train_inv, label='actual')
plt.plot(y_train_pred_inv, label='predicted')
plt.ylabel('kWatt')
plt.xlabel('index')
plt.title('Actual vs Predicted on Training data', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()
plt.plot(y_test_inv, label='actual')
plt.plot(y_test_pred_inv, label='predicted')
plt.ylabel('kWatt')
plt.xlabel('index')
plt.title('Actual vs predicted on test data using recursive SVM', fontsize=14)
plt.legend()
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12340862422997946, 'fraction_numerical': 0.013552361396303902, 'mean_word_length': 5.334200260078023, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '659502', 'license': 'MIT', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1886, 'n_words': 326, 'path': '03.power_consumption/05.energy-forecasting-SVM.ipynb', 'repo_name': 'mkumar73/time-series', 'size': '4870'}