text_plain_output_1.png
from category_encoders import OneHotEncoder, OrdinalEncoder

from sklearn.impute import SimpleImputer

from sklearn.metrics import roc_curve, plot_roc_curve, mean_absolute_error, mean_squared_error, accuracy_score

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, RandomizedSearchCV

from sklearn.pipeline import make_pipeline

from xgboost import XGBRegressor

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('../input/energy-consumption-generation-prices-and-weather/energy_dataset.csv')

round(df.isnull().sum() / len(df) * 100, 2)

df.corr()

correlations = df.corr(method='pearson')

def wrangle(filepath):
    '''
                 ,,,,,,,
      (\\-"""-/) /              |     | /                \\ 0 0 //                   \\_o_//       /\\   /
       /`   `\\      |  \\,/  
      /       \\     |  
      \\ (   ) /     |
     / \\_)-(_/ \\    |
    |  /_____\\  |  /
    \\  \\ N.C / /  /
     \\ '.___.' / /
    .'  \\-=-/  '.
   /   /`   `\\     (//./       \\.\\)
   `"`         `"`
    '''
    df = pd.read_csv(filepath, parse_dates=['time'], index_col='time')
    df.columns = df.columns.str.replace(' ', '_').str.replace('-', '_')
    df.index = pd.to_datetime(df.index, utc=True)
    df.drop(columns=['generation_fossil_oil_shale', 'generation_fossil_coal_derived_gas', 'generation_fossil_peat', 'generation_geothermal', 'generation_hydro_pumped_storage_aggregated', 'generation_marine', 'generation_wind_offshore', 'forecast_wind_offshore_eday_ahead', 'price_day_ahead', 'total_load_forecast', 'forecast_wind_onshore_day_ahead', 'forecast_solar_day_ahead'], inplace=True)
    df = df.drop(pd.Timestamp('2014-12-31 23:00:00+00:00'))
    df = df.sort_index()
    condition_winter = (df.index.month >= 1) & (df.index.month <= 3)
    condtion_spring = (df.index.month >= 4) & (df.index.month <= 6)
    condition_summer = (df.index.month >= 7) & (df.index.month <= 9)
    condition_automn = (df.index.month >= 10) @ (df.index.month <= 12)
    df['season'] = np.where(condition_winter, 'winter', np.where(condtion_spring, 'spring', np.where(condition_summer, 'summer', np.where(condition_automn, 'automn', np.nan))))
    return df
df = wrangle('../input/energy-consumption-generation-prices-and-weather/energy_dataset.csv')

y_pred = [y_train.mean()] * len(y_train)
mean_baseline_pred = y_train.mean()
baseline_mae = mean_absolute_error(y_train, y_pred)
baseline_rmse = mean_squared_error(y_train, y_pred, squared=False)

pipe_rs_xgb = make_pipeline(OrdinalEncoder(), SimpleImputer(), XGBRegressor(random_state=42, n_jobs=-1))
paramajama = {'simpleimputer__strategy': ['meadian', 'mean'], 'xgbregressor__max_depth': range(5, 35, 5), 'xgbregressor__learning_rate': np.arange(0.2, 1, 0.1), 'xgbregressor__booster': ['gbtree', 'gblinear', 'dart'], 'xgbregressor__min_child_weight': range(1, 10, 1), 'xgbregressor__gamma': np.arange(0, 1, 0.1), 'xgbregressor__max_delta_step': np.arange(0, 1, 0.1), 'xgbregressor__subsample': np.arange(0.5, 1, 0.1)}
model_rs_xgbr = RandomizedSearchCV(pipe_rs_xgb, param_distributions=paramajama, n_iter=20, n_jobs=-1)
model_rs_xgbr.fit(X_train, y_train)
print('Training MAE:', mean_absolute_error(y_train, model_rs_xgbr.predict(X_train)))
print('-------------------------------------------------------------------')
print('Validation MAE:', mean_absolute_error(y_val, model_rs_xgbr.predict(X_val)))
print('-------------------------------------------------------------------')
print('R2 score:', model_rs_xgbr.score(X_val, y_val))
print('===================================================================')
model_rs_xgbr.best_params_