text_html_output_1.png
from sklearn.metrics import r2_score

from sklearn.metrics import r2_score

import lightgbm as lgbm

import numpy as np

import pandas as pd

import warnings

import warnings

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

import warnings
warnings.filterwarnings(action='ignore', category=UserWarning)
check = x_train
max_score = 1
all_score = scorer(val)
drop = list()
while max_score >= all_score:
    col_list = list()
    score_list = list()
    for col in check.columns:
        col_list.append(col)
        temp = check.drop(col, axis=1)
        B = get_dataset(temp, y_train)
        s = scorer(B)
        score_list.append(s)
    val_df = pd.DataFrame(list(zip(col_list, score_list)), columns=['col_list', 'score_list'])
    val_df = val_df.sort_values(by='score_list', ascending=False)
    max_col = val_df.iloc[0, 0]
    max_score = val_df.iloc[0, 1]
    if max_score >= all_score:
        drop.append(max_col)
        all_score = max_score
        check = check.drop(max_col, axis=1)

def auto_selector(x_train, y_train):
    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
    import warnings
    warnings.filterwarnings(action='ignore', category=UserWarning)
    check = x_train
    max_score = 1
    all_score = scorer(val)
    drop = list()
    while max_score >= all_score:
        col_list = list()
        score_list = list()
        for col in check.columns:
            col_list.append(col)
            temp = check.drop(col, axis=1)
            B = get_dataset(temp, y_train)
            s = scorer(B)
            score_list.append(s)
        val_df = pd.DataFrame(list(zip(col_list, score_list)), columns=['col_list', 'score_list'])
        val_df = val_df.sort_values(by='score_list', ascending=False)
        max_col = val_df.iloc[0, 0]
        max_score = val_df.iloc[0, 1]
        if max_score >= all_score:
            drop.append(max_col)
            all_score = max_score
            check = check.drop(max_col, axis=1)
    return (x_train.drop(drop, axis=1), y_train)

final_df = auto_selector(x_train, y_train)
(final_df[0].shape, final_df[1].shape)