{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-18T09:07:12.372845Z","iopub.execute_input":"2023-10-18T09:07:12.373926Z","iopub.status.idle":"2023-10-18T09:07:12.389112Z","shell.execute_reply.started":"2023-10-18T09:07:12.373848Z","shell.execute_reply":"2023-10-18T09:07:12.387143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:13:18.667313Z","iopub.execute_input":"2023-10-18T09:13:18.667882Z","iopub.status.idle":"2023-10-18T09:13:18.674120Z","shell.execute_reply.started":"2023-10-18T09:13:18.667842Z","shell.execute_reply":"2023-10-18T09:13:18.672951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparation","metadata":{}},{"cell_type":"code","source":"de_train = pd.read_parquet('../input/open-problems-single-cell-perturbations/de_train.parquet')\noutput_names = de_train.iloc[:,5:].columns.values.tolist()\nde_train","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:07:14.919747Z","iopub.execute_input":"2023-10-18T09:07:14.920430Z","iopub.status.idle":"2023-10-18T09:07:17.672632Z","shell.execute_reply.started":"2023-10-18T09:07:14.920388Z","shell.execute_reply":"2023-10-18T09:07:17.671366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(list(de_train['cell_type'].unique()))\nprint(len(list(de_train['sm_name'].unique())))","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:07:41.342638Z","iopub.execute_input":"2023-10-18T09:07:41.343016Z","iopub.status.idle":"2023-10-18T09:07:41.351872Z","shell.execute_reply.started":"2023-10-18T09:07:41.342980Z","shell.execute_reply":"2023-10-18T09:07:41.350525Z"},"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',index_col=0)\nprint(list(id_map['cell_type'].unique()))\nprint(len(list(id_map['sm_name'].unique())))","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:07:48.142196Z","iopub.execute_input":"2023-10-18T09:07:48.142575Z","iopub.status.idle":"2023-10-18T09:07:48.164066Z","shell.execute_reply.started":"2023-10-18T09:07:48.142544Z","shell.execute_reply":"2023-10-18T09:07:48.162986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = preprocessing.LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:07:57.810916Z","iopub.execute_input":"2023-10-18T09:07:57.811354Z","iopub.status.idle":"2023-10-18T09:07:57.817917Z","shell.execute_reply.started":"2023-10-18T09:07:57.811320Z","shell.execute_reply":"2023-10-18T09:07:57.816481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_df = pd.DataFrame()\nnum_df['cell_type'] = encoder.fit_transform(de_train['cell_type'])\nnum_df['sm_name'] = encoder.fit_transform(de_train['sm_name'])\nnum_df['sm_lincs_id'] =encoder.fit_transform(de_train['sm_lincs_id'])\nnum_df['smiles'] =encoder.fit_transform(de_train['SMILES'])\nnum_df['control'] = de_train['control'].fillna(False).astype('int')\n\nnum_df","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:08:06.665084Z","iopub.execute_input":"2023-10-18T09:08:06.665516Z","iopub.status.idle":"2023-10-18T09:08:06.697768Z","shell.execute_reply.started":"2023-10-18T09:08:06.665483Z","shell.execute_reply":"2023-10-18T09:08:06.696111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame()\ndf['cell_type'] = encoder.fit_transform(id_map['cell_type'])\ndf['sm_name'] = encoder.fit_transform(id_map['sm_name'])\n\ndf","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:08:13.642452Z","iopub.execute_input":"2023-10-18T09:08:13.642841Z","iopub.status.idle":"2023-10-18T09:08:13.660147Z","shell.execute_reply.started":"2023-10-18T09:08:13.642814Z","shell.execute_reply":"2023-10-18T09:08:13.658900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(num_df.iloc[:,:2], de_train.iloc[:,5:], test_size=0.3, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:08:24.549265Z","iopub.execute_input":"2023-10-18T09:08:24.549616Z","iopub.status.idle":"2023-10-18T09:08:24.716376Z","shell.execute_reply.started":"2023-10-18T09:08:24.549588Z","shell.execute_reply":"2023-10-18T09:08:24.714647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create the model","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsRegressor\n\nkn_model = KNeighborsRegressor()\nkn_model.fit(X_train, y_train)\n\nprint(f\"Train accuracy = {kn_model.score(X_train,y_train) * 100}%\")\nprint(f\"Test accuracy = {kn_model.score(X_test,y_test) * 100}%\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:23:02.997149Z","iopub.execute_input":"2023-10-18T09:23:02.997641Z","iopub.status.idle":"2023-10-18T09:23:04.532942Z","shell.execute_reply.started":"2023-10-18T09:23:02.997604Z","shell.execute_reply":"2023-10-18T09:23:04.531554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\ngrid_params = { 'n_neighbors' : [4,15, 30, 60, 100],\n               'weights' : ['uniform','distance'],\n               'algorithm':['auto', 'ball_tree', 'kd_tree', 'brute'],\n               'metric' : ['minkowski','euclidean','manhattan']}\n\ngs = GridSearchCV(kn_model, grid_params, verbose = 1, cv=3, n_jobs = -1)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:52:03.006923Z","iopub.execute_input":"2023-10-18T09:52:03.007339Z","iopub.status.idle":"2023-10-18T09:52:03.015381Z","shell.execute_reply.started":"2023-10-18T09:52:03.007310Z","shell.execute_reply":"2023-10-18T09:52:03.013656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g_res = gs.fit(X_train, y_train)\n\nprint(f\" The best score for model {g_res.best_score_}\")\nprint(f\"The best parameters for model {g_res.best_params_}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:52:05.154202Z","iopub.execute_input":"2023-10-18T09:52:05.155628Z","iopub.status.idle":"2023-10-18T09:57:13.870078Z","shell.execute_reply.started":"2023-10-18T09:52:05.155574Z","shell.execute_reply":"2023-10-18T09:57:13.868200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = KNeighborsRegressor(metric='manhattan', \n                            n_neighbors=100, \n                            algorithm = 'brute',\n                            weights='distance')\nmodel.fit(X_train, y_train)\n\nprint(f\"Train score = {model.score(X_train,y_train) * 100}%\")\nprint(f\"Test score = {model.score(X_test,y_test) * 100}%\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T09:50:22.947965Z","iopub.execute_input":"2023-10-18T09:50:22.948396Z","iopub.status.idle":"2023-10-18T09:50:26.687735Z","shell.execute_reply.started":"2023-10-18T09:50:22.948365Z","shell.execute_reply":"2023-10-18T09:50:26.686911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(df)\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:16:10.123787Z","iopub.execute_input":"2023-10-18T10:16:10.124945Z","iopub.status.idle":"2023-10-18T10:16:11.461905Z","shell.execute_reply.started":"2023-10-18T10:16:10.124888Z","shell.execute_reply":"2023-10-18T10:16:11.460040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculate the error","metadata":{}},{"cell_type":"code","source":"test_out = model.predict(X_test)\nmrrmse_score = np.sqrt(np.square(y_test - test_out).mean(axis=1))","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:16:43.400966Z","iopub.execute_input":"2023-10-18T10:16:43.402351Z","iopub.status.idle":"2023-10-18T10:16:44.555624Z","shell.execute_reply.started":"2023-10-18T10:16:43.402284Z","shell.execute_reply":"2023-10-18T10:16:44.553957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f' RMMSE = {mrrmse_score.mean()}')","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:16:53.991262Z","iopub.execute_input":"2023-10-18T10:16:53.991994Z","iopub.status.idle":"2023-10-18T10:16:53.999972Z","shell.execute_reply.started":"2023-10-18T10:16:53.991945Z","shell.execute_reply":"2023-10-18T10:16:53.998937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.scatter(list(range(0, 185)), mrrmse_score)\nplt.xlabel('Iterations')\nplt.ylabel('MRRMSE')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:17:01.517296Z","iopub.execute_input":"2023-10-18T10:17:01.518458Z","iopub.status.idle":"2023-10-18T10:17:01.833129Z","shell.execute_reply.started":"2023-10-18T10:17:01.518419Z","shell.execute_reply":"2023-10-18T10:17:01.831976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Results ","metadata":{}},{"cell_type":"code","source":"output = pd.DataFrame(predictions, index=id_map.index, columns=output_names)\noutput","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:17:25.133470Z","iopub.execute_input":"2023-10-18T10:17:25.133871Z","iopub.status.idle":"2023-10-18T10:17:25.174463Z","shell.execute_reply.started":"2023-10-18T10:17:25.133842Z","shell.execute_reply":"2023-10-18T10:17:25.173407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-18T10:18:09.696850Z","iopub.execute_input":"2023-10-18T10:18:09.697281Z","iopub.status.idle":"2023-10-18T10:18:19.220001Z","shell.execute_reply.started":"2023-10-18T10:18:09.697243Z","shell.execute_reply":"2023-10-18T10:18:19.218513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}