text_html_output_1.png
from math import sqrt

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import StratifiedKFold

import lightgbm as lgb

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

train = pd.read_csv('/kaggle/input/electrical-consumption/train_6BJx641.csv')
test = pd.read_csv('/kaggle/input/electrical-consumption/test_pavJagI.csv')

train['datetime'] = pd.to_datetime(train['datetime'])
test['datetime'] = pd.to_datetime(test['datetime'])

def booleancon(x):
    if x == True:
        return 1
    else:
        return 0
def var2(x):
    if x == 'A':
        return 1
    elif x == 'B':
        return 2
    else:
        return 3
def time_pr(train):
    train = add_datepart(train, 'datetime', drop=True, time=True)
    train['datetimeIs_month_end'] = train['datetimeIs_month_end'].apply(booleancon)
    train['datetimeIs_month_start'] = train['datetimeIs_month_start'].apply(booleancon)
    train['datetimeIs_quarter_start'] = train['datetimeIs_quarter_start'].apply(booleancon)
    train['datetimeIs_quarter_end'] = train['datetimeIs_quarter_end'].apply(booleancon)
    train['datetimeIs_year_start'] = train['datetimeIs_quarter_start'].apply(booleancon)
    train['datetimeIs_year_end'] = train['datetimeIs_quarter_end'].apply(booleancon)
    train['var2'] = train['var2'].apply(var2)
    return train

pd.set_option('display.max_columns', 1000)
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_colwidth', 199)
train = time_pr(train)
test = time_pr(test)

col = []
for i in train.columns:
    if i != 'electricity_consumption' and i != 'ID' and (i != 'datetime'):
        col.append(i)

X = train[col].values
Y = train['electricity_consumption'].values

import concurrent.futures
import lightgbm as lgb
import numpy as np
from sklearn.model_selection._split import KFold
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix
kf = StratifiedKFold(n_splits=10, random_state=None, shuffle=True)
index = []
for train_index, test_index in kf.split(X, Y):
    index.append([train_index, test_index])
def model(train_index, test_index, XX, YY):
    X_train, X_test = (XX[train_index], XX[test_index])
    Y_train, Y_test = (YY[train_index], YY[test_index])
    d_train = lgb.Dataset(X_train, label=Y_train)
    params = {}
    params['application'] = 'root_mean_squared_error'
    params['num_boost_round'] = 800
    params['learning_rate'] = 0.01
    params['boosting_type'] = 'gbdt'
    params['metric'] = 'rmse'
    params['sub_feature'] = 0.833
    params['num_leaves'] = 207
    params['min_split_gain'] = 0.05
    params['min_child_weight'] = 27
    params['max_depth'] = -1
    params['num_threads'] = 10
    params['max_bin'] = 50
    params['lambda_l2'] = 0.1
    params['lambda_l1'] = 0.3
    params['feature_fraction'] = 0.833
    params['bagging_fraction'] = 0.979
    clf = lgb.train(params, d_train, 1000)
    pred = clf.predict(X_test)
    from sklearn.metrics import mean_squared_error
    from math import sqrt
    rmae = sqrt(mean_squared_error(pred, Y_test))
    return rmae
with concurrent.futures.ThreadPoolExecutor() as executor:
    future1 = executor.submit(model, index[0][0], index[0][1], X, Y)
    return_value1 = future1.result()
    future2 = executor.submit(model, index[1][0], index[1][1], X, Y)
    return_value2 = future2.result()
    future3 = executor.submit(model, index[2][0], index[2][1], X, Y)
    return_value3 = future3.result()
    future4 = executor.submit(model, index[3][0], index[3][1], X, Y)
    return_value4 = future4.result()
    future5 = executor.submit(model, index[4][0], index[4][1], X, Y)
    return_value5 = future5.result()
    future6 = executor.submit(model, index[5][0], index[5][1], X, Y)
    return_value6 = future6.result()
    future7 = executor.submit(model, index[6][0], index[6][1], X, Y)
    return_value7 = future7.result()
    future8 = executor.submit(model, index[7][0], index[7][1], X, Y)
    return_value8 = future8.result()
    future9 = executor.submit(model, index[8][0], index[8][1], X, Y)
    return_value9 = future9.result()
    future10 = executor.submit(model, index[9][0], index[9][1], X, Y)
    return_value10 = future10.result()
rmae = [return_value1, return_value2, return_value3, return_value4, return_value5, return_value6, return_value7, return_value8, return_value9, return_value10]
print(sum(rmae) / len(rmae))