<reponame>Naoyg/HomeCredit
"""
変数選択
"""
from pathlib import Path

import hydra
import pandas as pd
from sklearn.feature_selection import SequentialFeatureSelector
from sklearn.linear_model import LogisticRegression

REPO = Path(__file__).resolve().parents[2]

col_idx = "SK_ID_CURR"
col_target = "TARGET"


def load_dataset(cols):
    train_df = pd.read_pickle(REPO / "data" / "processed" / "train_df.pkl")
    test_df = pd.read_pickle(REPO / "data" / "processed" / "test_df.pkl")
    train_x = train_df[cols]
    test_x = test_df[cols]
    train_y = train_df[col_target]
    return train_x, test_x, train_y


@hydra.main(config_path="conf", config_name="config")
def main(cfg):
    seed = cfg.job.seeds[0]
    feature_list = pd.read_csv(REPO / "data" / "processed" / "feature_list.csv")
    cols = feature_list.loc[feature_list["採用"] == 1, "変数"].values
    train_x, _, train_y = load_dataset(cols)

    clf = LogisticRegression(
        random_state=seed,
        **cfg.model.params)
    sfs = SequentialFeatureSelector(clf,
                                    **cfg.selection)
    sfs.fit(train_x, train_y)
    selected_cols = train_x.columns.values[sfs.get_support()]
    # 保存
    feature_list["変数選択"] = 0
    feature_list.loc[feature_list["変数"].isin(selected_cols), "変数選択"] = 1
    feature_list.to_csv(REPO / "data" / "processed" / "feature_list.csv")


if __name__ == "__main__":
    main()

"""
変数選択
"""
from pathlib import Path

import hydra
import pandas as pd
from sklearn.feature_selection import SequentialFeatureSelector
from sklearn.linear_model import LogisticRegression

REPO = Path(__file__).resolve().parents[2]

col_idx = "SK_ID_CURR"
col_target = "TARGET"


def load_dataset(cols):
    train_df = pd.read_pickle(REPO / "data" / "processed" / "train_df.pkl")
    test_df = pd.read_pickle(REPO / "data" / "processed" / "test_df.pkl")
    train_x = train_df[cols]
    test_x = test_df[cols]
    train_y = train_df[col_target]
    return train_x, test_x, train_y


@hydra.main(config_path="conf", config_name="config")
def main(cfg):
    seed = cfg.job.seeds[0]
    feature_list = pd.read_csv(REPO / "data" / "processed" / "feature_list.csv")
    cols = feature_list.loc[feature_list["採用"] == 1, "変数"].values
    train_x, _, train_y = load_dataset(cols)

    clf = LogisticRegression(
        random_state=seed,
        **cfg.model.params)
    sfs = SequentialFeatureSelector(clf,
                                    **cfg.selection)
    sfs.fit(train_x, train_y)
    selected_cols = train_x.columns.values[sfs.get_support()]
    # 保存
    feature_list["変数選択"] = 0
    feature_list.loc[feature_list["変数"].isin(selected_cols), "変数選択"] = 1
    feature_list.to_csv(REPO / "data" / "processed" / "feature_list.csv")


if __name__ == "__main__":
    main()