{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59094,"databundleVersionId":7010844,"sourceType":"competition"},{"sourceId":7062514,"sourceType":"datasetVersion","datasetId":4061914}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"***PLEASE UPVOTE https://www.kaggle.com/code/mehrankazeminia/3-op2-feature-augment-fragments-of-smiles?scriptVersionId=149631969***","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')\n\nimport pandas as pd\n\npd.set_option('display.max_columns', 30)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:22.099947Z","iopub.execute_input":"2023-11-27T15:02:22.100561Z","iopub.status.idle":"2023-11-27T15:02:22.629389Z","shell.execute_reply.started":"2023-11-27T15:02:22.100480Z","shell.execute_reply":"2023-11-27T15:02:22.628083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nSEED = 42\nnp.random.seed(SEED)","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:22.632054Z","iopub.execute_input":"2023-11-27T15:02:22.633010Z","iopub.status.idle":"2023-11-27T15:02:22.640057Z","shell.execute_reply.started":"2023-11-27T15:02:22.632967Z","shell.execute_reply":"2023-11-27T15:02:22.638350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"de_train = pd.read_parquet('/kaggle/input/open-problems-single-cell-perturbations/de_train.parquet')\n\nprint(de_train.shape)\nde_train","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:22.643869Z","iopub.execute_input":"2023-11-27T15:02:22.644406Z","iopub.status.idle":"2023-11-27T15:02:25.474303Z","shell.execute_reply.started":"2023-11-27T15:02:22.644365Z","shell.execute_reply":"2023-11-27T15:02:25.473391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_map = pd.read_csv ('/kaggle/input/open-problems-single-cell-perturbations/id_map.csv')\nid_map","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:25.476772Z","iopub.execute_input":"2023-11-27T15:02:25.477673Z","iopub.status.idle":"2023-11-27T15:02:25.501987Z","shell.execute_reply.started":"2023-11-27T15:02:25.477581Z","shell.execute_reply":"2023-11-27T15:02:25.500903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_0_531 = pd.read_csv('/kaggle/input/op-scp-submissions-4/submission_0_531.csv')\nsub_0_531 = pd.concat([id_map, sub_0_531], axis=1).drop(columns='id')\nsub_0_531","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:25.503641Z","iopub.execute_input":"2023-11-27T15:02:25.504707Z","iopub.status.idle":"2023-11-27T15:02:32.051542Z","shell.execute_reply.started":"2023-11-27T15:02:25.504652Z","shell.execute_reply":"2023-11-27T15:02:32.050653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"de_train = pd.concat([de_train, sub_0_531, sub_0_531, sub_0_531, sub_0_531, sub_0_531, sub_0_531],\n                     ignore_index=True)\nde_train","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.052828Z","iopub.execute_input":"2023-11-27T15:02:32.053682Z","iopub.status.idle":"2023-11-27T15:02:32.274366Z","shell.execute_reply.started":"2023-11-27T15:02:32.053646Z","shell.execute_reply":"2023-11-27T15:02:32.273040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = de_train.iloc[:, 5:]","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.276102Z","iopub.execute_input":"2023-11-27T15:02:32.276476Z","iopub.status.idle":"2023-11-27T15:02:32.382988Z","shell.execute_reply.started":"2023-11-27T15:02:32.276444Z","shell.execute_reply":"2023-11-27T15:02:32.381484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies_train = pd.get_dummies(de_train[['cell_type','sm_name']],\n                               columns=['cell_type','sm_name'])\n\nprint(dummies_train.shape)\ndummies_train","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.384752Z","iopub.execute_input":"2023-11-27T15:02:32.385308Z","iopub.status.idle":"2023-11-27T15:02:32.444743Z","shell.execute_reply.started":"2023-11-27T15:02:32.385252Z","shell.execute_reply":"2023-11-27T15:02:32.443469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummies_test = pd.get_dummies(id_map[['cell_type','sm_name']],\n                              columns=['cell_type','sm_name'])\n\nprint(dummies_test.shape)\ndummies_test","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.446325Z","iopub.execute_input":"2023-11-27T15:02:32.446685Z","iopub.status.idle":"2023-11-27T15:02:32.496961Z","shell.execute_reply.started":"2023-11-27T15:02:32.446653Z","shell.execute_reply":"2023-11-27T15:02:32.495707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_feats = [feat for feat in dummies_train if feat not in dummies_test]\ndrop_feats","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.500785Z","iopub.execute_input":"2023-11-27T15:02:32.501180Z","iopub.status.idle":"2023-11-27T15:02:32.510479Z","shell.execute_reply.started":"2023-11-27T15:02:32.501146Z","shell.execute_reply":"2023-11-27T15:02:32.509311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = dummies_train.drop(columns=drop_feats)\nX.shape[1], dummies_test.shape[1]","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.512222Z","iopub.execute_input":"2023-11-27T15:02:32.512564Z","iopub.status.idle":"2023-11-27T15:02:32.523434Z","shell.execute_reply.started":"2023-11-27T15:02:32.512535Z","shell.execute_reply":"2023-11-27T15:02:32.522285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.525097Z","iopub.execute_input":"2023-11-27T15:02:32.525437Z","iopub.status.idle":"2023-11-27T15:02:32.569567Z","shell.execute_reply.started":"2023-11-27T15:02:32.525396Z","shell.execute_reply":"2023-11-27T15:02:32.568030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_type_tr = de_train.drop(columns=['sm_name', 'sm_lincs_id', 'SMILES', 'control'])\ncell_type_tr","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.571121Z","iopub.execute_input":"2023-11-27T15:02:32.572005Z","iopub.status.idle":"2023-11-27T15:02:32.724921Z","shell.execute_reply.started":"2023-11-27T15:02:32.571967Z","shell.execute_reply":"2023-11-27T15:02:32.723754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sm_name_tr = de_train.drop(columns=['cell_type', 'sm_lincs_id', 'SMILES', 'control'])\nsm_name_tr","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.726216Z","iopub.execute_input":"2023-11-27T15:02:32.726577Z","iopub.status.idle":"2023-11-27T15:02:32.880579Z","shell.execute_reply.started":"2023-11-27T15:02:32.726544Z","shell.execute_reply":"2023-11-27T15:02:32.879155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_type_te = sub_0_531.drop(columns=['sm_name'])\ncell_type_te","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.882254Z","iopub.execute_input":"2023-11-27T15:02:32.882824Z","iopub.status.idle":"2023-11-27T15:02:32.946426Z","shell.execute_reply.started":"2023-11-27T15:02:32.882784Z","shell.execute_reply":"2023-11-27T15:02:32.945108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sm_name_te = sub_0_531.drop(columns=['cell_type'])\nsm_name_te","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:32.948183Z","iopub.execute_input":"2023-11-27T15:02:32.949223Z","iopub.status.idle":"2023-11-27T15:02:33.019436Z","shell.execute_reply.started":"2023-11-27T15:02:32.949175Z","shell.execute_reply":"2023-11-27T15:02:33.018296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_submit(submit, version, i):\n    \n    submit.columns = de_train.columns[5:]\n    submit.index.name = 'id'\n    \n    submit.to_csv(f'submission_v{version}_i{i}.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:33.021020Z","iopub.execute_input":"2023-11-27T15:02:33.022274Z","iopub.status.idle":"2023-11-27T15:02:33.029111Z","shell.execute_reply.started":"2023-11-27T15:02:33.022224Z","shell.execute_reply":"2023-11-27T15:02:33.027620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nfrom sklearn.neighbors import KNeighborsRegressor\nfrom sklearn.svm import LinearSVR\n\n\nN_NEIGHBORS = [20, 25]\nMAX_ITER = [5000, 7000]\nID_MAP_LEN = len(id_map)\nVERSION = 15\nI_TOTAL = 99\ni_total = I_TOTAL\ni_blend_submit = 0\n\nY_submit_preds = np.zeros((ID_MAP_LEN,  Y.shape[1]))\n\nfor n_neighbors in N_NEIGHBORS:\n    \n    for max_iter in MAX_ITER:\n        \n        i_total += 1\n        model_knr = KNeighborsRegressor(n_neighbors=n_neighbors)\n        model_lsvr = LinearSVR(max_iter=max_iter, epsilon=0.1)\n        \n        Y_preds = []\n        \n        for gene in tqdm(range(Y.shape[1])):\n\n            Y_gene = Y.iloc[:, gene].copy()\n\n            X_gene = X.join(cell_type_tr.iloc[:, gene + 1]).copy()\n            X_gene = X_gene.join(sm_name_tr.iloc[:, gene + 1], lsuffix='_cell_type', rsuffix='_sm_name')\n\n            test_gene = dummies_test.join(cell_type_te.iloc[:, gene + 1]).copy()\n            test_gene = test_gene.join(sm_name_te.iloc[:, gene + 1], lsuffix='_cell_type', rsuffix='_sm_name')\n\n            model_knr.fit(X_gene, Y_gene)\n            model_lsvr.fit(X_gene, Y_gene)\n\n            Y_pred_knr = model_knr.predict(test_gene)\n            Y_pred_lsvr = model_lsvr.predict(test_gene)\n\n            Y_preds.append(Y_pred_knr * 0.3 + Y_pred_lsvr * 0.7)\n            \n        print(f'I_TOTAL: {i_total}\\nI_BLEND_SUBMIT: {i_blend_submit}\\nN_NEIGHBORS: {n_neighbors}\\nMAX_ITER: {max_iter}')\n        print(30 * '-')\n        \n        submit = pd.DataFrame(Y_preds).T\n        if i_total != I_TOTAL + 1:\n            save_submit(submit, VERSION, i_total) \n            \n        Y_submit_preds = (Y_submit_preds * i_blend_submit + submit) / (i_blend_submit + 1)\n        save_submit(Y_submit_preds, VERSION, i_blend_submit)    \n        \n        i_blend_submit += 1","metadata":{"execution":{"iopub.status.busy":"2023-11-27T15:02:33.030786Z","iopub.execute_input":"2023-11-27T15:02:33.031111Z","iopub.status.idle":"2023-11-27T16:51:52.154838Z","shell.execute_reply.started":"2023-11-27T15:02:33.031082Z","shell.execute_reply":"2023-11-27T16:51:52.153501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(f'/kaggle/working/submission_v{VERSION}_i0.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T16:51:52.156942Z","iopub.execute_input":"2023-11-27T16:51:52.157313Z","iopub.status.idle":"2023-11-27T16:51:57.775672Z","shell.execute_reply.started":"2023-11-27T16:51:52.157280Z","shell.execute_reply":"2023-11-27T16:51:57.774310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}