{"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 xgboost as xgb\nfrom sklearn.metrics import confusion_matrix, roc_auc_score, accuracy_score\nfrom sklearn.model_selection import train_test_split, GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:28.729541Z","iopub.execute_input":"2023-06-03T03:45:28.729897Z","iopub.status.idle":"2023-06-03T03:45:28.734742Z","shell.execute_reply.started":"2023-06-03T03:45:28.729868Z","shell.execute_reply":"2023-06-03T03:45:28.733772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_xgb = xgb.XGBClassifier(objective=\"binary:logistic\", random_state=42, tree_method = \"gpu_hist\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:28.983308Z","iopub.execute_input":"2023-06-03T03:45:28.983888Z","iopub.status.idle":"2023-06-03T03:45:29.065763Z","shell.execute_reply.started":"2023-06-03T03:45:28.983857Z","shell.execute_reply":"2023-06-03T03:45:29.064877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf_xgb.load_model(\"/kaggle/input/xgbdata/xgboost-full-2905-5pm.json\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:29.309841Z","iopub.execute_input":"2023-06-03T03:45:29.310122Z","iopub.status.idle":"2023-06-03T03:45:42.978766Z","shell.execute_reply.started":"2023-06-03T03:45:29.310099Z","shell.execute_reply":"2023-06-03T03:45:42.977825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = np.load(\"/kaggle/input/xgbdata/Y_1499.npy\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:42.980844Z","iopub.execute_input":"2023-06-03T03:45:42.981189Z","iopub.status.idle":"2023-06-03T03:45:43.425867Z","shell.execute_reply.started":"2023-06-03T03:45:42.981160Z","shell.execute_reply":"2023-06-03T03:45:43.424951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_to_consider = np.load(\"/kaggle/input/xgbdata/Y_1499_labels.npy\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.427340Z","iopub.execute_input":"2023-06-03T03:45:43.427733Z","iopub.status.idle":"2023-06-03T03:45:43.433899Z","shell.execute_reply.started":"2023-06-03T03:45:43.427698Z","shell.execute_reply":"2023-06-03T03:45:43.432862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fn = '/kaggle/input/t5embeds/test_embeds.npy'","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.436473Z","iopub.execute_input":"2023-06-03T03:45:43.436986Z","iopub.status.idle":"2023-06-03T03:45:43.443615Z","shell.execute_reply.started":"2023-06-03T03:45:43.436951Z","shell.execute_reply":"2023-06-03T03:45:43.442720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(fn)\nif '.csv' in fn:\n    df = pd.read_csv(fn, index_col = 0)\n    X = df.values\nelif '.npy' in fn:\n    X = np.load(fn)\nprint(X.shape)\nX","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.445074Z","iopub.execute_input":"2023-06-03T03:45:43.445401Z","iopub.status.idle":"2023-06-03T03:45:43.769263Z","shell.execute_reply.started":"2023-06-03T03:45:43.445371Z","shell.execute_reply":"2023-06-03T03:45:43.768219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## If taking new Predictions, run following 2 cells","metadata":{}},{"cell_type":"code","source":"# Y_pred = clf_xgb.predict(X)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.770770Z","iopub.execute_input":"2023-06-03T03:45:43.771213Z","iopub.status.idle":"2023-06-03T03:45:43.776054Z","shell.execute_reply.started":"2023-06-03T03:45:43.771180Z","shell.execute_reply":"2023-06-03T03:45:43.774798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.save(\"xgb-prediction-full-2905-5pm.json\", Y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.778093Z","iopub.execute_input":"2023-06-03T03:45:43.778412Z","iopub.status.idle":"2023-06-03T03:45:43.786384Z","shell.execute_reply.started":"2023-06-03T03:45:43.778383Z","shell.execute_reply":"2023-06-03T03:45:43.785142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = np.load(\"/kaggle/input/predictions-t5-test-embeds/xgb-prediction-test-embeds-full-2905-5pm.json.npy\")","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:43.787798Z","iopub.execute_input":"2023-06-03T03:45:43.788873Z","iopub.status.idle":"2023-06-03T03:45:56.633544Z","shell.execute_reply.started":"2023-06-03T03:45:43.788842Z","shell.execute_reply":"2023-06-03T03:45:56.632533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \ndf_finalSubmission = pd.DataFrame(columns = ['Protein Id', 'GO Term Id','Prediction'])","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:56.635153Z","iopub.execute_input":"2023-06-03T03:45:56.635493Z","iopub.status.idle":"2023-06-03T03:45:56.682584Z","shell.execute_reply.started":"2023-06-03T03:45:56.635462Z","shell.execute_reply":"2023-06-03T03:45:56.681344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfn = '/kaggle/input/t5embeds/test_ids.npy'\nvec_test_protein_ids = np.load(fn)\nprint(vec_test_protein_ids.shape)\nvec_test_protein_ids","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:56.686350Z","iopub.execute_input":"2023-06-03T03:45:56.687169Z","iopub.status.idle":"2023-06-03T03:45:56.751807Z","shell.execute_reply.started":"2023-06-03T03:45:56.687137Z","shell.execute_reply":"2023-06-03T03:45:56.750981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_batch_sz = 5000","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:56.753022Z","iopub.execute_input":"2023-06-03T03:45:56.753428Z","iopub.status.idle":"2023-06-03T03:45:56.758220Z","shell.execute_reply.started":"2023-06-03T03:45:56.753398Z","shell.execute_reply":"2023-06-03T03:45:56.757034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \nl = []\nfor k in list(vec_test_protein_ids[:test_data_batch_sz]):\n    l += [ k] * Y_pred.shape[1]\nprint(len(l), l[:20])    \n\ndf_finalSubmission['Protein Id'] = l","metadata":{"execution":{"iopub.status.busy":"2023-06-03T03:45:56.759811Z","iopub.execute_input":"2023-06-03T03:45:56.760145Z","iopub.status.idle":"2023-06-03T03:45:58.931600Z","shell.execute_reply.started":"2023-06-03T03:45:56.760115Z","shell.execute_reply":"2023-06-03T03:45:58.930586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_to_consider_repeated = [item for _ in range(test_data_batch_sz) for item in labels_to_consider]\nlabels_to_consider_repeated","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_finalSubmission['GO Term Id'] = labels_to_consider * test_data_batch_sz","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion_matrix(Y[IX_test,:],Y_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = []\nfor i in range(Y.shape[1]):\n    if len(np.unique(Y[IX_test,i]) ) > 1:\n        s = roc_auc_score(Y[IX_test,i], Y_pred[:,i]);\n    else:\n        s = 0.5\n    l.append(s)        \n    if i %10 == 0:\n        print(i, s)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_models_stat = pd.DataFrame()\ndf_models_stat.loc[\"XGB\",'RocAuc Mean Test'] = np.mean(l)\ndf_models_stat.loc[\"XGB\",'Test Size'] = len(IX_test)\ndf_models_stat","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.hist(l)\nplt.show()\npd.Series(l).describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = []\nfor i in range(Y.shape[1]):\n    if len(np.unique(Y[IX_test,i]) ) > 1:\n        s = accuracy_score(Y[IX_test,i], Y_pred[:,i]);\n    else:\n        s = 0.5\n    l.append(s)        \n    if i %10 == 0:\n        print(i, s)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_finalSubmission = pd.DataFrame(columns = ['Protein Id', 'GO Term Id','Prediction'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfn = '/kaggle/input/t5embeds/test_ids.npy'\nvec_test_protein_ids = np.load(fn)\nprint(vec_test_protein_ids.shape)\nvec_test_protein_ids","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = []\nfor k in list(vec_test_protein_ids):\n    l += [ k] * Y_pred.shape[1]\nprint(len(l), l[:20])    \n\ndf_finalSubmission['Protein Id'] = l","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"default_params = {}\ngparams = clf_xgb.get_params()\nfor key in gparams.keys():\n    gp = gparams[key]\n    default_params[key] = [gp]\n\n    \n#benchmark model. Grid search is not performed, since only single values are provided as parameter grid.\n#However, cross-validation is still executed\nclf0 = GridSearchCV(estimator=clf_xgb, scoring='roc_auc', param_grid=default_params, return_train_score=True, verbose=1, cv=3)\nclf0.fit(X[IX_test,:], Y[IX_train,:].values.ravel())\n\ndf = pd.DataFrame(clf0.cv_results_)\n\ntrain_predictions = clf0.predict(X_train)\ntest_predictions = clf0.predict(X_test)\n\naccs_train = accuracy_score(y_train, train_predictions)\naccs_test = accuracy_score(y_test, test_predictions)\n\n\n","metadata":{},"execution_count":null,"outputs":[]}]}