image_output_1.png
from category_encoders import OneHotEncoder, OrdinalEncoder

from eli5.sklearn import PermutationImportance

from sklearn.ensemble import RandomForestRegressor

from sklearn.impute import SimpleImputer

from sklearn.linear_model import Ridge, LinearRegression

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

from xgboost import XGBRegressor

import eli5

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

import shap

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)

onehot = OneHotEncoder(use_cat_names=True)
onehot_fit = onehot.fit(X_train)
XT_train = onehot.transform(X_train)
XT_val = onehot.transform(X_val)
simp = SimpleImputer(strategy='mean')
simp_fit = simp.fit(XT_train)
XT_train = simp.transform(XT_train)
XT_val = simp.transform(XT_val)
model_lr = LinearRegression()
model_r = Ridge()
model_r.fit(XT_train, y_train)
model_lr.fit(XT_train, y_train)
def check_metrics(model):
    pass
model = [model_r, model_lr]
for m in model:
    check_metrics(m)

ordinal = OrdinalEncoder()
ordinal_fit = ordinal.fit(X_train)
XT_train = ordinal.transform(X_train)
XT_val = ordinal.transform(X_val)
simp = SimpleImputer(strategy='mean')
simp_fit = simp.fit(XT_train)
XT_train = simp.transform(XT_train)
XT_val = simp.transform(XT_val)
model_rfr = RandomForestRegressor()
model_xgbr = XGBRegressor()
model_rfr.fit(XT_train, y_train)
model_xgbr.fit(XT_train, y_train)
def check_metrics(model):
    pass
model = [model_xgbr, model_rfr]
for m in model:
    check_metrics(m)

samp = pd.DataFrame(XT_val, columns=ordinal_fit.get_feature_names())

explainer = shap.TreeExplainer(model_xgbr)
shap_values = explainer()
shap.plots.waterfall(shap_values[0])

explainer = shap.TreeExplainer(model_xgbr)
shap_values = explainer.shap_values()
shap.initjs()

perm = PermutationImportance(model_xgbr, random_state=42).fit(XT_val, y_val)
eli5.show_weights(perm, feature_names=samp.columns.tolist())