text_plain_output_2.png text_plain_output_1.png
from sklearn.metrics import r2_score

import lightgbm as lgbm

import numpy as np

import pandas as pd

import pandas as pd
import numpy as np
import lightgbm as lgbm
x_train = pd.read_csv('../input/regression-datasets/X_train_reg.csv')
y_train = pd.read_csv('../input/regression-datasets/y_train_reg.csv')

def get_dataset(df, target):
    L = df.shape[0]
    cv_test1 = df.iloc[:round(L / 5), :]
    cv_test2 = df.iloc[round(L / 5):round(2 * L / 5), :]
    cv_test3 = df.iloc[round(2 * L / 5):round(3 * L / 5), :]
    cv_test4 = df.iloc[round(3 * L / 5):round(4 * L / 5), :]
    cv_test5 = df.iloc[round(4 * L / 5):, :]
    cv_train1 = pd.concat([cv_test2, cv_test3, cv_test4, cv_test5], axis=0)
    cv_train2 = pd.concat([cv_test1, cv_test3, cv_test4, cv_test5], axis=0)
    cv_train3 = pd.concat([cv_test1, cv_test2, cv_test4, cv_test5], axis=0)
    cv_train4 = pd.concat([cv_test1, cv_test2, cv_test3, cv_test5], axis=0)
    cv_train5 = pd.concat([cv_test1, cv_test2, cv_test3, cv_test5], axis=0)
    T1 = target.iloc[0:round(L / 5)]
    T2 = target.iloc[round(L / 5):round(2 * L / 5)]
    T3 = target.iloc[round(2 * L / 5):round(3 * L / 5)]
    T4 = target.iloc[round(3 * L / 5):round(4 * L / 5)]
    T5 = target.iloc[round(4 * L / 5):]
    t1 = pd.concat([T2, T3, T4, T5], axis=0)
    t2 = pd.concat([T1, T3, T4, T5], axis=0)
    t3 = pd.concat([T1, T2, T4, T5], axis=0)
    t4 = pd.concat([T1, T2, T3, T5], axis=0)
    t5 = pd.concat([T1, T2, T3, T4], axis=0)
    cv_test = [cv_test1, cv_test2, cv_test3, cv_test4, cv_test5]
    cv_train = [cv_train1, cv_train2, cv_train3, cv_train4, cv_train5]
    T = [T1, T2, T3, T4, T5]
    t = [t1, t2, t3, t4, t5]
    return (cv_test, cv_train, T, t)
val = get_dataset(x_train, y_train)

def scorer(A):
    score = 0
    for i in range(0, 5):
        model = lgbm.LGBMRegressor(n_estimators=100, random_state=42)
        model.fit(A[1][i], np.array(A[3][i]))
        pred = model.predict(A[0][i])
        from sklearn.metrics import r2_score
        S = r2_score(pred, A[2][i])
        score = score + S
    return score / 5

all_score = scorer(val)
all_score