{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport json\n\nimport catboost as cb\nimport os\nimport gc\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom collections import defaultdict\nfrom IPython.display import FileLink        \nimport pickle as pkl\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.gridspec as gridspec\nimport lightgbm as lgmb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-22T22:09:35.039820Z","iopub.execute_input":"2023-02-22T22:09:35.041158Z","iopub.status.idle":"2023-02-22T22:09:36.640587Z","shell.execute_reply.started":"2023-02-22T22:09:35.041018Z","shell.execute_reply":"2023-02-22T22:09:36.639737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RES_BASE_DIR = \"/kaggle/input/catboost-models-v7/models\"","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.642374Z","iopub.execute_input":"2023-02-22T22:09:36.642934Z","iopub.status.idle":"2023-02-22T22:09:36.646982Z","shell.execute_reply.started":"2023-02-22T22:09:36.642895Z","shell.execute_reply":"2023-02-22T22:09:36.646129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correlation_score(y_true, y_pred):\n    \"\"\"Scores the predictions according to the competition rules. \n    \n    It is assumed that the predictions are not constant.\n    \n    Returns the average of each sample's Pearson correlation coefficient\"\"\"\n    \n    y2 = y_pred.copy()\n    y2 -= y2.mean(axis=0);    y2 /= y2.std(axis=0) \n    y1 = y_true.copy(); \n    y1 -= y1.mean(axis=0);    y1 /= y1.std(axis=0) \n        \n    c = (y1*y2).mean().mean()# Correlation for rescaled matrices is just matrix product and average \n        \n    return c","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.648357Z","iopub.execute_input":"2023-02-22T22:09:36.648981Z","iopub.status.idle":"2023-02-22T22:09:36.658142Z","shell.execute_reply.started":"2023-02-22T22:09:36.648938Z","shell.execute_reply":"2023-02-22T22:09:36.657231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalization(values):\n    return (values - np.mean(values)) / np.std(values)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.660857Z","iopub.execute_input":"2023-02-22T22:09:36.661281Z","iopub.status.idle":"2023-02-22T22:09:36.668665Z","shell.execute_reply.started":"2023-02-22T22:09:36.661250Z","shell.execute_reply":"2023-02-22T22:09:36.667562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def intersaction_coeff(list1, list2):\n    return len(set(list1).intersection(set(list2)))","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.670132Z","iopub.execute_input":"2023-02-22T22:09:36.670416Z","iopub.status.idle":"2023-02-22T22:09:36.679297Z","shell.execute_reply.started":"2023-02-22T22:09:36.670391Z","shell.execute_reply":"2023-02-22T22:09:36.678151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cos_sim(a, b):\n    cos_sim = np.dot(a, b)/(np.linalg.norm(a)*np.linalg.norm(b))\n    return cos_sim","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.680811Z","iopub.execute_input":"2023-02-22T22:09:36.681274Z","iopub.status.idle":"2023-02-22T22:09:36.688586Z","shell.execute_reply.started":"2023-02-22T22:09:36.681243Z","shell.execute_reply":"2023-02-22T22:09:36.687607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def open_json(path):\n    with open(path, \"r\") as json_file:\n        data = json.load(json_file)\n        return data","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:36.689825Z","iopub.execute_input":"2023-02-22T22:09:36.690154Z","iopub.status.idle":"2023-02-22T22:09:36.698624Z","shell.execute_reply.started":"2023-02-22T22:09:36.690124Z","shell.execute_reply":"2023-02-22T22:09:36.697846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = open_json(f\"{RES_BASE_DIR}/msci_config_experiment.json\")\nlgbm_df = pd.read_feather(f\"{RES_BASE_DIR}/fi_all_lgbm.feather\")\nlgbm_dupl_df = pd.read_feather(f\"{RES_BASE_DIR}/fi_dupl_all_lgbm.feather\")\nlgbm_perm_df = pd.read_feather(f\"{RES_BASE_DIR}/fi_perm_all_lgbm.feather\")\n\nduplicate_columns = config[\"duplicate_columns\"]\npermutate_columns = config[\"permutate_columns\"]\ncolumns = config[\"columns\"]\nbatches_config = config[\"batches\"]","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:53:43.517246Z","iopub.execute_input":"2023-02-22T22:53:43.517711Z","iopub.status.idle":"2023-02-22T22:53:44.657985Z","shell.execute_reply.started":"2023-02-22T22:53:43.517674Z","shell.execute_reply":"2023-02-22T22:53:44.656840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_batch_id(cd):\n    for idx, batch_conf in enumerate(config[\"batches\"]):\n        if cd in batch_conf[\"cds\"]:\n            return idx","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:52:54.639038Z","iopub.execute_input":"2023-02-22T22:52:54.639782Z","iopub.status.idle":"2023-02-22T22:52:54.648132Z","shell.execute_reply.started":"2023-02-22T22:52:54.639745Z","shell.execute_reply":"2023-02-22T22:52:54.647164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_perm_df.sort_values(\"corr\", ascending=False, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:43:30.612025Z","iopub.execute_input":"2023-02-22T22:43:30.612898Z","iopub.status.idle":"2023-02-22T22:43:30.619997Z","shell.execute_reply.started":"2023-02-22T22:43:30.612845Z","shell.execute_reply":"2023-02-22T22:43:30.618959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_perm_cd36_df = lgbm_perm_df[\n    (lgbm_perm_df[\"CD\"] == \"CD36\") & (lgbm_perm_df[\"corr\"] > 0.7)\n]","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:43:31.641311Z","iopub.execute_input":"2023-02-22T22:43:31.641667Z","iopub.status.idle":"2023-02-22T22:43:31.648581Z","shell.execute_reply.started":"2023-02-22T22:43:31.641641Z","shell.execute_reply":"2023-02-22T22:43:31.647396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_perm_cd36_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:43:36.329511Z","iopub.execute_input":"2023-02-22T22:43:36.329896Z","iopub.status.idle":"2023-02-22T22:43:36.348207Z","shell.execute_reply.started":"2023-02-22T22:43:36.329865Z","shell.execute_reply":"2023-02-22T22:43:36.346910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sr = lgbm_perm_cd36_df.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:43:38.428396Z","iopub.execute_input":"2023-02-22T22:43:38.429239Z","iopub.status.idle":"2023-02-22T22:43:38.435092Z","shell.execute_reply.started":"2023-02-22T22:43:38.429189Z","shell.execute_reply":"2023-02-22T22:43:38.433856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_fi(\n    fi,        \n    prefix=\"PERM\",\n    N = 20,\n    title = None\n):\n    if prefix == \"DUPL\":\n        columns = duplicate_columns\n    elif prefix == \"PERM\":\n        columns = permutate_columns\n    else:\n        raise \"Not supported prefix\"\n    \n    sorted_index = np.argsort(fi)[::-1]\n    sorted_fi = fi[sorted_index]\n    sorted_columns = np.array(columns)[sorted_index]    \n    \n    vanil_indexes = []\n    no_vanil_indexes = []\n    for idx, column_name in enumerate(sorted_columns[:N]):\n        if column_name.startswith(prefix):\n            no_vanil_indexes.append(idx)\n        else:\n            vanil_indexes.append(idx)   \n            \n    _ = plt.scatter(\n        vanil_indexes, \n        sorted_fi[vanil_indexes],\n        label=\"vanil\"\n    )    \n\n    _ = plt.scatter(\n        no_vanil_indexes, \n        sorted_fi[no_vanil_indexes],\n        s=8,\n        #alpha=0.5,\n        label=prefix,\n        color=\"orange\"\n    )     \n    _ = plt.xlabel(\"Feature RANK\")\n    _ = plt.ylabel(\"Feature score\")\n    if title is not None:\n        _ = plt.title(title)\n    \n    plt.legend()\n    return sorted_columns[:N], vanil_indexes, no_vanil_indexes","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:50:56.949900Z","iopub.execute_input":"2023-02-22T23:50:56.950341Z","iopub.status.idle":"2023-02-22T23:50:56.960540Z","shell.execute_reply.started":"2023-02-22T23:50:56.950310Z","shell.execute_reply":"2023-02-22T23:50:56.959819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_columns, vanil_indexes, no_vanil_indexes = plot_fi(\n    sr.fi, \n    N=40, \n    title=f\"{sr.CD}_{sr.day}_{sr.donor}\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:52:33.866212Z","iopub.execute_input":"2023-02-22T23:52:33.866586Z","iopub.status.idle":"2023-02-22T23:52:34.193444Z","shell.execute_reply.started":"2023-02-22T23:52:33.866556Z","shell.execute_reply":"2023-02-22T23:52:34.192501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cite_input_df = pd.read_hdf(\"/kaggle/input/open-problems-multimodal/train_cite_inputs.h5\")\ntrain_cite_target_df = pd.read_hdf(\"/kaggle/input/open-problems-multimodal/train_cite_targets.h5\")\nmetadata_df = pd.read_csv(\"/kaggle/input/open-problems-multimodal/metadata.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:09:38.522962Z","iopub.execute_input":"2023-02-22T22:09:38.523246Z","iopub.status.idle":"2023-02-22T22:10:34.889398Z","shell.execute_reply.started":"2023-02-22T22:09:38.523220Z","shell.execute_reply":"2023-02-22T22:10:34.888378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_df.set_index(\"cell_id\", inplace=True)\ntrain_cite_target_df = train_cite_target_df.join(metadata_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:10:34.890945Z","iopub.execute_input":"2023-02-22T22:10:34.891461Z","iopub.status.idle":"2023-02-22T22:10:35.014934Z","shell.execute_reply.started":"2023-02-22T22:10:34.891416Z","shell.execute_reply":"2023-02-22T22:10:35.013835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sr","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:10:35.016202Z","iopub.execute_input":"2023-02-22T22:10:35.016521Z","iopub.status.idle":"2023-02-22T22:10:35.025324Z","shell.execute_reply.started":"2023-02-22T22:10:35.016492Z","shell.execute_reply":"2023-02-22T22:10:35.023990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.random.seed(42)\n\n# fi_df = defaultdict(list)\n# cd_it = sr.CD\n# day_it = sr.day\n# donor_it = sr.donor\n\n# train_test_index = train_cite_target_df[\n#     (train_cite_target_df.day == day_it) & \n#     (train_cite_target_df.donor == donor_it)\n# ].index.tolist()        \n\n# train_test_input_df = train_cite_input_df.loc[train_test_index]\n# for column in columns:\n#     values = train_test_input_df[column].values.copy()\n#     np.random.shuffle(values)\n#     train_test_input_df[f\"PERM_{column}\"] = values\n\n# train_test_input_df = train_test_input_df[permutate_columns]\n\n# train_index, test_index = train_test_split(\n#     train_test_index, \n#     test_size=0.33, \n#     random_state=42\n# )\n\n# # model = lgmb.LGBMRegressor(\n# #     verbose = 1\n# # )\n\n# train_X = train_test_input_df.loc[train_index]\n# train_Y = train_cite_target_df.loc[train_index][cd_it]\n\n# test_X = train_test_input_df.loc[test_index]\n# test_Y = train_cite_target_df.loc[test_index][cd_it].values","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:39:29.004655Z","iopub.execute_input":"2023-02-22T23:39:29.005120Z","iopub.status.idle":"2023-02-22T23:39:29.013089Z","shell.execute_reply.started":"2023-02-22T23:39:29.005062Z","shell.execute_reply":"2023-02-22T23:39:29.011575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# corrs = []\n# for column in sorted_columns:\n#     values_X = test_X[column].values\n#     test_corr = correlation_score(\n#         values_X,\n#         test_Y\n#     )\n#     corrs.append(test_corr)\n#     #print(column, test_corr)\n    \n# _ = plt.plot(sorted_columns, corrs, label=\"vanil\")\n# _ = plt.scatter(\n#     no_vanil_indexes, \n#     np.array(corrs)[no_vanil_indexes], \n#     color=\"orange\",\n#     label=\"PERM\"\n# )\n# _ = plt.xticks(rotation=90)\n# _ = plt.legend()\n# _ = plt.xlabel(\"RNA\")\n# _ = plt.ylabel(\"Corr(test_x, test_y)\")","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:39:27.229771Z","iopub.execute_input":"2023-02-22T23:39:27.230272Z","iopub.status.idle":"2023-02-22T23:39:27.236312Z","shell.execute_reply.started":"2023-02-22T23:39:27.230234Z","shell.execute_reply":"2023-02-22T23:39:27.235019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del train_test_input_df\n# del train_X\n# del train_Y\n# del test_X\n# del test_Y\n# del train_index\n# del test_index\n# gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:39:32.281431Z","iopub.execute_input":"2023-02-22T23:39:32.281831Z","iopub.status.idle":"2023-02-22T23:39:32.289386Z","shell.execute_reply.started":"2023-02-22T23:39:32.281802Z","shell.execute_reply":"2023-02-22T23:39:32.288301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Repeat exp to check stable shuffle","metadata":{"execution":{"iopub.status.busy":"2023-02-22T21:43:02.236996Z","iopub.execute_input":"2023-02-22T21:43:02.238106Z","iopub.status.idle":"2023-02-22T21:43:02.244415Z","shell.execute_reply.started":"2023-02-22T21:43:02.238052Z","shell.execute_reply":"2023-02-22T21:43:02.243471Z"}}},{"cell_type":"code","source":"batch_it = get_batch_id(sr.CD)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:54:07.771130Z","iopub.execute_input":"2023-02-22T22:54:07.771864Z","iopub.status.idle":"2023-02-22T22:54:07.776970Z","shell.execute_reply.started":"2023-02-22T22:54:07.771827Z","shell.execute_reply":"2023-02-22T22:54:07.776181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cds = batches_config[batch_it][\"cds\"]\nday_donor = batches_config[batch_it][\"day_donor\"]","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:54:10.604146Z","iopub.execute_input":"2023-02-22T22:54:10.605162Z","iopub.status.idle":"2023-02-22T22:54:10.609172Z","shell.execute_reply.started":"2023-02-22T22:54:10.605121Z","shell.execute_reply":"2023-02-22T22:54:10.608351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-22T22:54:18.781273Z","iopub.execute_input":"2023-02-22T22:54:18.781695Z","iopub.status.idle":"2023-02-22T22:54:18.787143Z","shell.execute_reply.started":"2023-02-22T22:54:18.781663Z","shell.execute_reply":"2023-02-22T22:54:18.786139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fi_df = defaultdict(list)\ncd_it = sr.CD\nday_it = sr.day\ndonor_it = sr.donor\n\ntrain_test_index = train_cite_target_df[\n    (train_cite_target_df.day == day_it) & \n    (train_cite_target_df.donor == donor_it)\n].index.tolist()        \n\ntrain_test_input_df = train_cite_input_df.loc[train_test_index]\nfor column in columns:\n    np.random.seed(42)\n    values = train_test_input_df[column].values.copy()\n    np.random.shuffle(values)\n    train_test_input_df[f\"PERM_{column}\"] = values\n\ntrain_test_input_df = train_test_input_df[permutate_columns]\n\ntrain_index, test_index = train_test_split(\n    train_test_index, \n    test_size=0.33, \n    random_state=42\n)\n\ntrain_X = train_test_input_df.loc[train_index]\ntrain_Y = train_cite_target_df.loc[train_index][cd_it]\n\ntest_X = train_test_input_df.loc[test_index]\ntest_Y = train_cite_target_df.loc[test_index][cd_it].values\n\nmodel = lgmb.LGBMRegressor(\n    verbose = 1,\n    random_state=42\n)\n\nmodel.fit(\n    train_X,\n    train_Y\n)\n\nmodel_dir_path = f\"/kaggle/working/lgmb_{cd_it}_{day_it}_{donor_it}\"    \nos.makedirs(model_dir_path, exist_ok=True)\n\nmodel_path = model_dir_path + \"/model.txt\"\nmodel.booster_.save_model(model_path)\n\ny_true = test_Y\n\ny_pred = model.predict(\n    test_X\n)    \n\ncorr_score = correlation_score(y_true, y_pred)\nprint(f\"{cd_it}_{day_it}_{donor_it}\", corr_score)\n\nfi_df[\"CD\"].append(cd_it)\nfi_df[\"day\"].append(day_it)\nfi_df[\"donor\"].append(donor_it)\nfi_df[\"corr\"].append(corr_score)\n\nfeature_importance = model.feature_importances_\nfi_df[\"fi\"].append(feature_importance)\n\ndel train_test_input_df\ndel y_true\ndel y_pred\ndel model\ndel train_index\ndel test_index\ngc.collect()\n\nfi_df = pd.DataFrame(fi_df)\nfi_df.to_pickle(f\"/kaggle/working/fi_perm_batch_{batch_it}.pkl\")","metadata":{"execution":{"iopub.status.busy":"2023-02-22T23:53:56.288603Z","iopub.execute_input":"2023-02-22T23:53:56.289058Z","iopub.status.idle":"2023-02-23T00:00:13.781087Z","shell.execute_reply.started":"2023-02-22T23:53:56.289022Z","shell.execute_reply":"2023-02-23T00:00:13.779771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_columns, vanil_indexes, no_vanil_indexes = plot_fi(feature_importance, N=40, title=f\"{sr.CD}_{sr.day}_{sr.donor}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:01:30.666408Z","iopub.execute_input":"2023-02-23T00:01:30.667420Z","iopub.status.idle":"2023-02-23T00:01:31.004457Z","shell.execute_reply.started":"2023-02-23T00:01:30.667375Z","shell.execute_reply":"2023-02-23T00:01:31.003542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrs = []\nfor column in sorted_columns:\n    values_X = test_X[column].values\n    test_corr = correlation_score(\n        values_X,\n        test_Y\n    )\n    corrs.append(test_corr)\n    #print(column, test_corr)\n    \n_ = plt.plot(sorted_columns, corrs, label=\"vanil\")\n_ = plt.scatter(\n    no_vanil_indexes, \n    np.array(corrs)[no_vanil_indexes], \n    color=\"orange\",\n    label=\"PERM\"\n)\n_ = plt.xticks(rotation=90)\n_ = plt.legend()\n_ = plt.xlabel(\"RNA\")\n_ = plt.ylabel(\"Corr(test_x, test_y)\")","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:01:37.045421Z","iopub.execute_input":"2023-02-23T00:01:37.045838Z","iopub.status.idle":"2023-02-23T00:01:37.851571Z","shell.execute_reply.started":"2023-02-23T00:01:37.045805Z","shell.execute_reply":"2023-02-23T00:01:37.849738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_columns.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:08:57.343581Z","iopub.execute_input":"2023-02-23T00:08:57.343972Z","iopub.status.idle":"2023-02-23T00:08:57.353787Z","shell.execute_reply.started":"2023-02-23T00:08:57.343942Z","shell.execute_reply":"2023-02-23T00:08:57.352622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sorted_columns)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:01:55.720787Z","iopub.execute_input":"2023-02-23T00:01:55.721857Z","iopub.status.idle":"2023-02-23T00:01:55.728712Z","shell.execute_reply.started":"2023-02-23T00:01:55.721799Z","shell.execute_reply":"2023-02-23T00:01:55.727608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fi_df = defaultdict(list)\ncd_it = sr.CD\nday_it = sr.day\ndonor_it = sr.donor\n\nfor i in range(1, len(sorted_columns)):\n    target_features = sorted_columns[:i]\n\n    model = lgmb.LGBMRegressor(\n        verbose = 0,\n        force_col_wise=True\n    )\n\n    model.fit(\n        train_X[target_features],\n        train_Y\n    )\n\n    model_dir_path = f\"/kaggle/working/lgmb_{cd_it}_{day_it}_{donor_it}\"    \n    os.makedirs(model_dir_path, exist_ok=True)\n\n    model_path = model_dir_path + \"/model.txt\"\n    model.booster_.save_model(model_path)\n\n    y_true = test_Y\n\n    y_pred = model.predict(\n        test_X[target_features]\n    )    \n\n    corr_score = correlation_score(y_true, y_pred)\n    print(f\"{i}) {cd_it}_{day_it}_{donor_it}\", corr_score)\n\n    fi_df[\"CD\"].append(cd_it)\n    fi_df[\"day\"].append(day_it)\n    fi_df[\"donor\"].append(donor_it)\n    fi_df[\"corr\"].append(corr_score)\n    fi_df[\"target_features\"].append(target_features)\n\n    feature_importance = model.feature_importances_\n    fi_df[\"fi\"].append(feature_importance)\n\n    del y_true\n    del y_pred\n    del model\n    gc.collect()\n\nfi_df = pd.DataFrame(fi_df)\nfi_df.to_pickle(f\"/kaggle/working/fi_perm_batch_{batch_it}_top_n_f.pkl\")","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:01:58.729537Z","iopub.execute_input":"2023-02-23T00:01:58.729940Z","iopub.status.idle":"2023-02-23T00:02:31.742410Z","shell.execute_reply.started":"2023-02-23T00:01:58.729908Z","shell.execute_reply":"2023-02-23T00:02:31.741257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = plt.xlabel(\"Count of features\")\n_ = plt.ylabel(\"Corr\")\n_ = plt.title(f\"{sr.CD}_{sr.day}_{sr.donor}\")\n_ = plt.plot(fi_df[\"corr\"])\n_ = plt.scatter(\n    no_vanil_indexes, \n    fi_df[\"corr\"].values[no_vanil_indexes], \n    color=\"orange\",\n    label=\"PERM\"\n)\n_ = plt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T00:02:36.665916Z","iopub.execute_input":"2023-02-23T00:02:36.666370Z","iopub.status.idle":"2023-02-23T00:02:36.965906Z","shell.execute_reply.started":"2023-02-23T00:02:36.666334Z","shell.execute_reply":"2023-02-23T00:02:36.964970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}