{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67356,"databundleVersionId":8006601,"sourceType":"competition"},{"sourceId":8084868,"sourceType":"datasetVersion","datasetId":4772411},{"sourceId":8464709,"sourceType":"datasetVersion","datasetId":5046404}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## <div style=\"color:lightgray;background-color:navy;padding:1.2%;border-radius:12px 12px;font-size:1em;text-align:center\">BELKA 2⚕️Protein BRD4⚕️XGB & LGBM⚕️For All [binds=1]</div>","metadata":{}},{"cell_type":"code","source":"!pip install datatable","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-30T17:34:38.647569Z","iopub.execute_input":"2024-05-30T17:34:38.648001Z","iopub.status.idle":"2024-05-30T17:39:28.184398Z","shell.execute_reply.started":"2024-05-30T17:34:38.647968Z","shell.execute_reply":"2024-05-30T17:39:28.182879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport random\nimport numpy as np \nimport pandas as pd\nimport datatable as dt \n\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\nfrom sklearn.metrics import average_precision_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.calibration import CalibratedClassifierCV\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n!ls ../input/*","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:39:28.187743Z","iopub.execute_input":"2024-05-30T17:39:28.188275Z","iopub.status.idle":"2024-05-30T17:39:32.250919Z","shell.execute_reply.started":"2024-05-30T17:39:28.18821Z","shell.execute_reply":"2024-05-30T17:39:32.249231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"border-bottom: 25px solid lightcyan\"></p>\n<p style=\"border-bottom: 5px solid navy\"></p>\n\n# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:cyan;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>XGBClassifier & LGBMClassifier⚕️For All [binds=1]</p></div>\n\n### <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:pink;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:darkred;overflow:hidden;font-size:85%;letter-spacing:0.5px;margin:0\"><b> </b>trainset [binds=0]</p></div>","metadata":{}},{"cell_type":"code","source":"%%time\nfrag0 = dt.fread('../input/belka-frag-1/frag_train_2.csv').to_pandas().iloc[:300000] \nfrag0.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:39:32.253457Z","iopub.execute_input":"2024-05-30T17:39:32.254784Z","iopub.status.idle":"2024-05-30T17:39:53.958242Z","shell.execute_reply.started":"2024-05-30T17:39:32.254741Z","shell.execute_reply":"2024-05-30T17:39:53.9568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Duplicates in frag0:',  frag0.duplicated().sum())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frag0.drop_duplicates(inplace=True)\nfrag0.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:pink;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:darkred;overflow:hidden;font-size:85%;letter-spacing:0.5px;margin:0\"><b> </b>trainset [binds=1]</p></div>","metadata":{}},{"cell_type":"code","source":"%%time\nfrag1 = dt.fread('../input/frag-binds1/frag_train_2.csv').to_pandas()\nfrag1.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:39:53.962851Z","iopub.execute_input":"2024-05-30T17:39:53.963329Z","iopub.status.idle":"2024-05-30T17:40:10.159599Z","shell.execute_reply.started":"2024-05-30T17:39:53.963295Z","shell.execute_reply":"2024-05-30T17:40:10.157959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Duplicates in frag1:',  frag1.duplicated().sum())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frag1.drop_duplicates(inplace=True)\nfrag1.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nX = pd.concat([frag0, frag1], ignore_index=True).astype(int)\ny = X.pop('binds')\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:10.162092Z","iopub.execute_input":"2024-05-30T17:40:10.162607Z","iopub.status.idle":"2024-05-30T17:40:17.724956Z","shell.execute_reply.started":"2024-05-30T17:40:10.162565Z","shell.execute_reply":"2024-05-30T17:40:17.723093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del frag0, frag1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:17.727163Z","iopub.execute_input":"2024-05-30T17:40:17.728589Z","iopub.status.idle":"2024-05-30T17:40:17.883157Z","shell.execute_reply.started":"2024-05-30T17:40:17.728529Z","shell.execute_reply":"2024-05-30T17:40:17.881881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.gca().set_facecolor('lightyellow')\ny.value_counts(normalize=True).plot(kind='barh', figsize=(12,1.2), color=['pink','lightblue'])\n\npd.DataFrame(data= {'Number': y.value_counts(), \n                    'Percent': y.value_counts(normalize=True)})","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:17.884658Z","iopub.execute_input":"2024-05-30T17:40:17.885533Z","iopub.status.idle":"2024-05-30T17:40:18.317699Z","shell.execute_reply.started":"2024-05-30T17:40:17.885471Z","shell.execute_reply":"2024-05-30T17:40:18.316638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"border-bottom: 25px solid lightcyan\"></p>\n<p style=\"border-bottom: 5px solid navy\"></p>","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, shuffle=True, random_state=420)\nX_train.shape, X_test.shape, y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:18.319507Z","iopub.execute_input":"2024-05-30T17:40:18.319912Z","iopub.status.idle":"2024-05-30T17:40:59.13433Z","shell.execute_reply.started":"2024-05-30T17:40:18.31988Z","shell.execute_reply":"2024-05-30T17:40:59.131873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X, y\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:59.137004Z","iopub.execute_input":"2024-05-30T17:40:59.138099Z","iopub.status.idle":"2024-05-30T17:40:59.380951Z","shell.execute_reply.started":"2024-05-30T17:40:59.138059Z","shell.execute_reply":"2024-05-30T17:40:59.379789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### XGBoost Classifier.","metadata":{}},{"cell_type":"code","source":"%%time\nmodel1 = XGBClassifier(n_estimators=500, random_state=424)\nmodel1.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:59.388235Z","iopub.execute_input":"2024-05-30T17:40:59.388703Z","iopub.status.idle":"2024-05-30T17:54:34.745392Z","shell.execute_reply.started":"2024-05-30T17:40:59.388669Z","shell.execute_reply":"2024-05-30T17:54:34.743455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:54:34.748629Z","iopub.execute_input":"2024-05-30T17:54:34.749602Z","iopub.status.idle":"2024-05-30T17:54:34.926919Z","shell.execute_reply.started":"2024-05-30T17:54:34.749564Z","shell.execute_reply":"2024-05-30T17:54:34.925305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### LightGBM Classifier.\n\n> __init__(boosting_type='gbdt', num_leaves=31, max_depth=-1, learning_rate=0.1, n_estimators=100, subsample_for_bin=200000, objective=None, class_weight=None, min_split_gain=0.0, min_child_weight=0.001, min_child_samples=20, subsample=1.0, subsample_freq=0, colsample_bytree=1.0, reg_alpha=0.0, reg_lambda=0.0, random_state=None, n_jobs=None, importance_type='split', **kwargs","metadata":{}},{"cell_type":"code","source":"%%time\nmodel2 = LGBMClassifier(n_estimators=5500, random_state=425, verbose=-1)\nmodel2.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:54:34.94266Z","iopub.execute_input":"2024-05-30T17:54:34.943246Z","iopub.status.idle":"2024-05-30T18:16:52.914273Z","shell.execute_reply.started":"2024-05-30T17:54:34.943204Z","shell.execute_reply":"2024-05-30T18:16:52.912845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, y_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:16:52.916022Z","iopub.execute_input":"2024-05-30T18:16:52.916422Z","iopub.status.idle":"2024-05-30T18:16:53.103393Z","shell.execute_reply.started":"2024-05-30T18:16:52.91639Z","shell.execute_reply":"2024-05-30T18:16:53.1019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"border-bottom: 25px solid lightcyan\"></p>\n<p style=\"border-bottom: 5px solid navy\"></p>\n\n# <div style=\"color:yellow;display:inline-block;border-radius:5px;background-color:cyan;font-block:Nexa;overflow:hidden\"><p style=\"padding:15px;color:navy;overflow:hidden;font-size:70%;letter-spacing:0.5px;margin:0\"><b> </b>⚕️Prediction</p></div>","metadata":{}},{"cell_type":"code","source":"%%time\npredict_test = (model1.predict_proba(X_test)[:, 1] * 0.50) + (model2.predict_proba(X_test)[:, 1] * 0.50)\nscore = average_precision_score(y_test, predict_test) \nscore","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:16:53.105632Z","iopub.execute_input":"2024-05-30T18:16:53.105995Z","iopub.status.idle":"2024-05-30T18:19:24.063478Z","shell.execute_reply.started":"2024-05-30T18:16:53.105966Z","shell.execute_reply":"2024-05-30T18:19:24.062011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_test, y_test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:19:24.066074Z","iopub.execute_input":"2024-05-30T18:19:24.066557Z","iopub.status.idle":"2024-05-30T18:19:24.216347Z","shell.execute_reply.started":"2024-05-30T18:19:24.066516Z","shell.execute_reply":"2024-05-30T18:19:24.214866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nindx = np.array([])\npred = np.array([])\n\nfor XX in pd.read_csv('../input/frag-binds1/frag_test_2.csv', chunksize=100000):\n    \n    idx = XX.pop('id')\n    indx = np.concatenate((indx, idx), axis=0)\n    \n    pr = (model1.predict_proba(XX.astype(int))[:, 1] * 0.50) + (model2.predict_proba(XX.astype(int))[:, 1] * 0.50) \n    pred = np.concatenate((pred, pr), axis=0)  \n    \n# pred = np.clip(pred, 0.0, 1.0)\ndel model1, model2\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:19:24.218812Z","iopub.execute_input":"2024-05-30T18:19:24.219299Z","iopub.status.idle":"2024-05-30T18:29:19.117769Z","shell.execute_reply.started":"2024-05-30T18:19:24.219259Z","shell.execute_reply":"2024-05-30T18:29:19.116522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 4))\nplt.hist(pred, bins=50)\n\nplt.gca().set_facecolor('lightyellow')\nplt.suptitle('Prediction Histogram', y=0.96, fontsize=16, c='darkred')\n\nround(min(pred), 3), round(max(pred), 3)","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:29:19.119596Z","iopub.execute_input":"2024-05-30T18:29:19.119981Z","iopub.status.idle":"2024-05-30T18:29:19.795268Z","shell.execute_reply.started":"2024-05-30T18:29:19.11995Z","shell.execute_reply":"2024-05-30T18:29:19.793979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = pd.DataFrame(data=indx, columns=['id'])\nprediction['binds']= pred.copy()    \nprediction","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:29:19.797283Z","iopub.execute_input":"2024-05-30T18:29:19.79774Z","iopub.status.idle":"2024-05-30T18:29:19.826565Z","shell.execute_reply.started":"2024-05-30T18:29:19.797705Z","shell.execute_reply":"2024-05-30T18:29:19.82463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.to_csv('pred2.csv', index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:29:19.828618Z","iopub.execute_input":"2024-05-30T18:29:19.828981Z","iopub.status.idle":"2024-05-30T18:29:23.790088Z","shell.execute_reply.started":"2024-05-30T18:29:19.828952Z","shell.execute_reply":"2024-05-30T18:29:23.788377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"border-bottom: 25px solid lightcyan\"></p>\n<p style=\"border-bottom: 5px solid navy\"></p>","metadata":{}}]}