{"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":"!pip install lightgbm\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nimport lightgbm as lgbm\nfrom sklearn.decomposition import PCA","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-16T11:53:42.110896Z","iopub.execute_input":"2023-02-16T11:53:42.112168Z","iopub.status.idle":"2023-02-16T11:53:54.725759Z","shell.execute_reply.started":"2023-02-16T11:53:42.112040Z","shell.execute_reply":"2023-02-16T11:53:54.724333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/research-project-01-around-multimodal-singlecell/HVG_cite_train_test_donor_3000.csv')\ntrain_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\")\n\nday_4_cell_ids = set(metadata_df[metadata_df[\"day\"] == 4][\"cell_id\"])\n\nday_4_test_index = train_cite_input_df.index[train_cite_input_df.index.isin(day_4_cell_ids)]\ntrain_index = train_cite_input_df.index[~train_cite_input_df.index.isin(day_4_cell_ids)]\n\n#new_features = df['seurat_v3']\nnew_feat = train_cite_input_df[train_cite_input_df.columns[train_cite_input_df.columns.isin(df['seurat_v3'])]]","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:53:57.645595Z","iopub.execute_input":"2023-02-16T11:53:57.645966Z","iopub.status.idle":"2023-02-16T11:54:46.702203Z","shell.execute_reply.started":"2023-02-16T11:53:57.645938Z","shell.execute_reply":"2023-02-16T11:54:46.700853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_types = [\n#    \"HSC\",\n    \"EryP\",\n#    \"BP\",\n#    \"MasP\",\n#    \"MkP\",\n#    \"Mop\",\n#    \"NeuP\",\n#    \"hidden\"\n]","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:55:00.157526Z","iopub.execute_input":"2023-02-16T11:55:00.158126Z","iopub.status.idle":"2023-02-16T11:55:00.163721Z","shell.execute_reply.started":"2023-02-16T11:55:00.158049Z","shell.execute_reply":"2023-02-16T11:55:00.162701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_cds = [\n    \"CD36\",\n#    \"CD41\",\n#    \"CD32\",\n#    \"CD45\",\n#     \"CD88\",\n#     \"CD48\",\n#     \"CD62L\",\n#     \"CD49b\",\n#     \"CD45RA\",\n#     \"CD82\",\n#     \"CD38\",\n#     \"CD71\",\n#     \"CD115\",\n#     \"CD11a\",\n#     \"CD244\",\n]","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:55:02.858668Z","iopub.execute_input":"2023-02-16T11:55:02.859123Z","iopub.status.idle":"2023-02-16T11:55:02.864799Z","shell.execute_reply.started":"2023-02-16T11:55:02.859090Z","shell.execute_reply":"2023-02-16T11:55:02.863013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cd_it in target_cds:\n    for cell_type_it in cell_types:\n        set_cell_type_it = set(metadata_df[metadata_df[\"cell_type\"] == cell_type_it][\"cell_id\"].tolist())\n        cell_type_test_index = day_4_test_index[day_4_test_index.isin(set_cell_type_it)]\n        cell_type_train_index = train_index[train_index.isin(set_cell_type_it)]    \n\n        del set_cell_type_it","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:55:05.017104Z","iopub.execute_input":"2023-02-16T11:55:05.017478Z","iopub.status.idle":"2023-02-16T11:55:05.089118Z","shell.execute_reply.started":"2023-02-16T11:55:05.017448Z","shell.execute_reply":"2023-02-16T11:55:05.087702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = new_feat.loc[cell_type_train_index]\ny = train_cite_target_df.loc[cell_type_train_index][cd_it]","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:55:06.504359Z","iopub.execute_input":"2023-02-16T11:55:06.504737Z","iopub.status.idle":"2023-02-16T11:55:06.632018Z","shell.execute_reply.started":"2023-02-16T11:55:06.504708Z","shell.execute_reply":"2023-02-16T11:55:06.630615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define model & train","metadata":{}},{"cell_type":"code","source":"data = X  \nlabel = y\ntrain_data = lgbm.Dataset(data, label=label)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:55:09.257294Z","iopub.execute_input":"2023-02-16T11:55:09.257848Z","iopub.status.idle":"2023-02-16T11:55:09.261994Z","shell.execute_reply.started":"2023-02-16T11:55:09.257799Z","shell.execute_reply":"2023-02-16T11:55:09.261021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param = {\n    'boosting_type':'gbdt',\n    'num_leaves':31,\n    'max_depth': -1, \n    'learning_rate':0.1,\n    'n_estimators': 100, \n    'subsample_for_bin':200000, \n    'objective':None, \n    'class_weight':None, \n    'min_split_gain':0.0, \n    'min_child_weight':0.001, \n    'min_child_samples':20, \n    'subsample':1.0, \n    'subsample_freq':0,\n    'colsample_bytree':1.0, \n    'reg_alpha':0.0, \n    'reg_lambda':0.0, \n    'random_state':42, \n    'n_jobs':None, \n    'importance_type':'split',\n}","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:56:03.648644Z","iopub.execute_input":"2023-02-16T11:56:03.649103Z","iopub.status.idle":"2023-02-16T11:56:03.656488Z","shell.execute_reply.started":"2023-02-16T11:56:03.649068Z","shell.execute_reply":"2023-02-16T11:56:03.655166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_round = 10\nbst = lgbm.LGBMRegressor()\nbst.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:57:15.438344Z","iopub.execute_input":"2023-02-16T11:57:15.438702Z","iopub.status.idle":"2023-02-16T11:57:28.750743Z","shell.execute_reply.started":"2023-02-16T11:57:15.438675Z","shell.execute_reply":"2023-02-16T11:57:28.748770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LGBM FI","metadata":{}},{"cell_type":"markdown","source":"### FI lgbm trained seurat","metadata":{}},{"cell_type":"code","source":"lgbm_fi_seurat = bst.feature_importances_\nlgbm_fi_seurat = pd.DataFrame(lgbm_fi_seurat, index = [x for x in range(len(lgbm_fi_seurat))])\nlgbm_fi_seurat['gene_id'] = X.columns.tolist()\nlgbm_fi_seurat = lgbm_fi_seurat.rename(columns={0: 'fi_value'})\nlgbm_fi_seurat = lgbm_fi_seurat.query('fi_value > 0')\nlgbm_fi_seurat = lgbm_fi_seurat.sort_values(by='fi_value', ascending=False)\nlgbm_fi_seurat = lgbm_fi_seurat[:100]\nlgbm_fi_seurat = lgbm_fi_seurat.reset_index(drop=True)\nlgbm_fi_seurat","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:59:22.496848Z","iopub.execute_input":"2023-02-16T11:59:22.497198Z","iopub.status.idle":"2023-02-16T11:59:22.522744Z","shell.execute_reply.started":"2023-02-16T11:59:22.497169Z","shell.execute_reply":"2023-02-16T11:59:22.522095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA 'seurat'","metadata":{}},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:59:29.749952Z","iopub.execute_input":"2023-02-16T11:59:29.750308Z","iopub.status.idle":"2023-02-16T11:59:29.791513Z","shell.execute_reply.started":"2023-02-16T11:59:29.750281Z","shell.execute_reply":"2023-02-16T11:59:29.790367Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA()\ns = pca.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:59:33.080828Z","iopub.execute_input":"2023-02-16T11:59:33.081171Z","iopub.status.idle":"2023-02-16T11:59:39.612981Z","shell.execute_reply.started":"2023-02-16T11:59:33.081144Z","shell.execute_reply":"2023-02-16T11:59:39.612175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:59:41.982196Z","iopub.execute_input":"2023-02-16T11:59:41.982829Z","iopub.status.idle":"2023-02-16T11:59:41.989601Z","shell.execute_reply.started":"2023-02-16T11:59:41.982778Z","shell.execute_reply":"2023-02-16T11:59:41.988610Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = pd.DataFrame(s, columns=X.columns)\ns","metadata":{"execution":{"iopub.status.busy":"2023-02-16T11:59:45.124661Z","iopub.execute_input":"2023-02-16T11:59:45.125178Z","iopub.status.idle":"2023-02-16T11:59:45.167359Z","shell.execute_reply.started":"2023-02-16T11:59:45.125143Z","shell.execute_reply":"2023-02-16T11:59:45.166224Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LGBM FI seurat","metadata":{}},{"cell_type":"code","source":"num_round = 10\nbst_pca = lgbm.LGBMRegressor()\nbst_pca.fit(s,y)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:03:06.183526Z","iopub.execute_input":"2023-02-16T12:03:06.183931Z","iopub.status.idle":"2023-02-16T12:03:31.834612Z","shell.execute_reply.started":"2023-02-16T12:03:06.183900Z","shell.execute_reply":"2023-02-16T12:03:31.833722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### feature importance pca_seurat","metadata":{}},{"cell_type":"code","source":"lgbm_PCA_fi_seurat = bst_pca.feature_importances_\nlgbm_PCA_fi_seurat = pd.DataFrame(lgbm_PCA_fi_seurat, index = [x for x in range(len(lgbm_PCA_fi_seurat))])\nlgbm_PCA_fi_seurat['gene_id'] = X.columns.tolist()\nlgbm_PCA_fi_seurat = lgbm_PCA_fi_seurat.rename(columns={0: 'fi_value'})\nlgbm_PCA_fi_seurat = lgbm_PCA_fi_seurat.query('fi_value > 0')\nlgbm_PCA_fi_seurat = lgbm_PCA_fi_seurat.sort_values(by='fi_value', ascending=False)\nlgbm_PCA_fi_seurat = lgbm_PCA_fi_seurat[:100]\nlgbm_PCA_fi_seurat = lgbm_PCA_fi_seurat.reset_index(drop=True)\nlgbm_PCA_fi_seurat","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:04:31.069488Z","iopub.execute_input":"2023-02-16T12:04:31.069863Z","iopub.status.idle":"2023-02-16T12:04:31.089700Z","shell.execute_reply.started":"2023-02-16T12:04:31.069833Z","shell.execute_reply":"2023-02-16T12:04:31.088864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LGBM FI PCA.invers_transform()","metadata":{}},{"cell_type":"code","source":"ss = pca.inverse_transform(s)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:05:36.298750Z","iopub.execute_input":"2023-02-16T12:05:36.299179Z","iopub.status.idle":"2023-02-16T12:05:37.051663Z","shell.execute_reply.started":"2023-02-16T12:05:36.299147Z","shell.execute_reply":"2023-02-16T12:05:37.050874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:05:38.658918Z","iopub.execute_input":"2023-02-16T12:05:38.659280Z","iopub.status.idle":"2023-02-16T12:05:38.667629Z","shell.execute_reply.started":"2023-02-16T12:05:38.659251Z","shell.execute_reply":"2023-02-16T12:05:38.666470Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param = {\n    'boosting_type':'gbdt',\n    'num_leaves':31,\n    'max_depth': -1, \n    'learning_rate':0.1,\n    'n_estimators': 100, \n    'subsample_for_bin':200000, \n    'objective':None, \n    'class_weight':None, \n    'min_split_gain':0.0, \n    'min_child_weight':0.001, \n    'min_child_samples':20, \n    'subsample':1.0, \n    'subsample_freq':0,\n    'colsample_bytree':1.0, \n    'reg_alpha':0.0, \n    'reg_lambda':0.0, \n    'random_state':42, \n    'n_jobs':None, \n    'importance_type':'split',\n}","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:05:52.230298Z","iopub.execute_input":"2023-02-16T12:05:52.230690Z","iopub.status.idle":"2023-02-16T12:05:52.237367Z","shell.execute_reply.started":"2023-02-16T12:05:52.230658Z","shell.execute_reply":"2023-02-16T12:05:52.236427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_round = 10\nbst_pca_it = lgbm.LGBMRegressor()\nbst_pca_it.fit(ss,y)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:06:37.249116Z","iopub.execute_input":"2023-02-16T12:06:37.249876Z","iopub.status.idle":"2023-02-16T12:07:02.356454Z","shell.execute_reply.started":"2023-02-16T12:06:37.249834Z","shell.execute_reply":"2023-02-16T12:07:02.355546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_PCA_inv_t_fi_seurat = bst_pca_it.feature_importances_\nlgbm_PCA_inv_t_fi_seurat = pd.DataFrame(lgbm_PCA_inv_t_fi_seurat, index = [x for x in range(len(lgbm_PCA_inv_t_fi_seurat))])\nlgbm_PCA_inv_t_fi_seurat['gene_id'] = X.columns.tolist()\nlgbm_PCA_inv_t_fi_seurat = lgbm_PCA_inv_t_fi_seurat.rename(columns={0: 'fi_value'})\nlgbm_PCA_inv_t_fi_seurat = lgbm_PCA_inv_t_fi_seurat.query('fi_value > 0')\nlgbm_PCA_inv_t_fi_seurat = lgbm_PCA_inv_t_fi_seurat.sort_values(by='fi_value', ascending=False)\nlgbm_PCA_inv_t_fi_seurat = lgbm_PCA_inv_t_fi_seurat[:100]\nlgbm_PCA_inv_t_fi_seurat = lgbm_PCA_inv_t_fi_seurat.reset_index(drop=True)\nlgbm_PCA_inv_t_fi_seurat","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:07:22.334670Z","iopub.execute_input":"2023-02-16T12:07:22.335747Z","iopub.status.idle":"2023-02-16T12:07:22.358165Z","shell.execute_reply.started":"2023-02-16T12:07:22.335706Z","shell.execute_reply":"2023-02-16T12:07:22.356894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Intersect FI's LGBM vs PCA_LGBM vs PCA_inv_t_LGBM","metadata":{}},{"cell_type":"markdown","source":"### PCA_LGBM vs PCA_inv_t_LGBM","metadata":{}},{"cell_type":"code","source":"len(set(lgbm_PCA_inv_t_fi_seurat['gene_id'])&set(lgbm_PCA_fi_seurat['gene_id']))","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:07:37.806725Z","iopub.execute_input":"2023-02-16T12:07:37.807131Z","iopub.status.idle":"2023-02-16T12:07:37.815684Z","shell.execute_reply.started":"2023-02-16T12:07:37.807100Z","shell.execute_reply":"2023-02-16T12:07:37.814060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LGBM vs PCA_inv_t_LGBM","metadata":{}},{"cell_type":"code","source":"len(set(lgbm_fi_seurat['gene_id'])&set(lgbm_PCA_inv_t_fi_seurat['gene_id']))","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:07:39.994931Z","iopub.execute_input":"2023-02-16T12:07:39.995307Z","iopub.status.idle":"2023-02-16T12:07:40.003754Z","shell.execute_reply.started":"2023-02-16T12:07:39.995278Z","shell.execute_reply":"2023-02-16T12:07:40.002447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LGBM vs PCA_LGBM","metadata":{}},{"cell_type":"code","source":"len(set(lgbm_fi_seurat['gene_id'])&set(lgbm_PCA_fi_seurat['gene_id']))","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:07:41.884947Z","iopub.execute_input":"2023-02-16T12:07:41.885278Z","iopub.status.idle":"2023-02-16T12:07:41.893731Z","shell.execute_reply.started":"2023-02-16T12:07:41.885255Z","shell.execute_reply":"2023-02-16T12:07:41.892908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LGBM vs PCA_LGBM vs PCA_inv_t_LGBM","metadata":{}},{"cell_type":"code","source":"len(set(lgbm_fi_seurat['gene_id'])&set(lgbm_PCA_inv_t_fi_seurat['gene_id'])&set(lgbm_PCA_fi_seurat['gene_id']))","metadata":{"execution":{"iopub.status.busy":"2023-02-16T12:07:44.499586Z","iopub.execute_input":"2023-02-16T12:07:44.499966Z","iopub.status.idle":"2023-02-16T12:07:44.507232Z","shell.execute_reply.started":"2023-02-16T12:07:44.499938Z","shell.execute_reply":"2023-02-16T12:07:44.505992Z"},"trusted":true},"execution_count":null,"outputs":[]}]}