<jupyter_start><jupyter_text>2. Modelling SVR Linear---<jupyter_code>## load modules and run mlflow_logging.ipynb to get function to track model information on MLFLow
import sys
sys.path.append("..")
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.svm import LinearSVR, SVR
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler, StandardScaler


from sklearn.model_selection import GridSearchCV

from modeling.functions import modelling, log_to_mlflow, get_features, save_models, load_models
data = pd.read_csv('../data/GEFCom2014Data/Wind/raw_data_incl_features.csv', parse_dates=['TIMESTAMP'])
data.head()
data.dropna(inplace=True)
data.info()
RSEED = 42
data = pd.get_dummies(data, columns = ['WD100CARD','WD10CARD'])
data.head()
## train-test-split
data_train, data_test = train_test_split(data, test_size=0.25, random_state=RSEED, stratify=data.ZONEID)
# define features and feature dict
feature_dict = get_features(data)

features = feature_dict['all']
# define zone
zone = 1
# split train and test data in feature and TARGETVAR parts and cut data to desired zones
X_train = data_train[data_train.ZONEID == zone][features]
y_train = data_train[data_train.ZONEID == zone].TARGETVAR

X_test = data_test[data_test.ZONEID == zone][features]
y_test = data_test[data_test.ZONEID == zone].TARGETVAR
# Scale data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = SVR()
for ii in range(5, 6):
    model = SVR(kernel='poly', degree=ii, C=1, cache_size=100)
    model.fit(X_train, y_train)
    y_pred_train = model.predict(X_train)
    y_pred_train = [1 if value >= 1 else 0 if value <= 0 else value for value in y_pred_train]
    y_pred = model.predict(X_test)
    y_pred = [1 if value >= 1 else 0 if value <= 0 else value for value in y_pred]
    print(ii,': train',mean_squared_error(y_train, y_pred_train, squared=False),
            ', test:', mean_squared_error(y_test, y_pred, squared=False))
model = SVR()
scaler = MinMaxScaler()

model_dict = {}
results = {}
results_train = {}
param_grid = [ 
    {           
        'C': [0.01, 0.1, 1, 5, 10, 100], 
        'degree': [5, 6, 7, 9],
        'kernel': ['poly']
    },{           
        'C': [0.01, 0.1, 1, 5, 10, 100], 
        'kernel': ['rbf']
    }
    ]

for key in feature_dict.keys():
    print(f'{key}')
    results_train[key],results[key], model_dict[key] = modelling(data_train, data_test, feature_dict[key], model = model, scaler=scaler, print_scores=True, log=None, infotext_mlflow=None, save_models= True, perform_gridCV= True, param_grid= param_grid)
    results[key] = {k : np.round(value,5) for k,value in results[key].items()}
results<jupyter_output><empty_output><jupyter_text>knn'all': {'ZONE1': 0.15672,\  'ZONE2': 0.13847,\  'ZONE3': 0.14915,\  'ZONE4': 0.17287,\  'ZONE5': 0.17664,\  'ZONE6': 0.1737,\  'ZONE7': 0.13239,\  'ZONE8': 0.15114,\  'ZONE9': 0.15061,\  'ZONE10': 0.19886,\  'TOTAL': 0.1612},<jupyter_code>model_dict
print('Best parameters')
for key, value in model_dict['all'].items():
    print(f'Zone: {key}: {value.best_estimator_}')<jupyter_output>Best parameters
Zone: 1: SVR(C=10)
Zone: 2: SVR(C=10)
Zone: 3: SVR(C=10)
Zone: 4: SVR(C=10)
Zone: 5: SVR(C=10)
Zone: 6: SVR(C=10)
Zone: 7: SVR(C=10)
Zone: 8: SVR(C=5, degree=6, kernel='poly')
Zone: 9: SVR(C=10)
Zone: 10: SVR(C=10)
{'fraction_non_alphanumeric': 0.14665895284929129, 'fraction_numerical': 0.048018513161700895, 'mean_word_length': 4.792294807370184, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '408229', 'license': 'MIT', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1311, 'n_words': 317, 'path': 'notebooks/models/2_modelling_svr_rbf_ferdinand.ipynb', 'repo_name': 'JeromeSauer/Capstone_WindPowerPredicting', 'size': '3457'}