{"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 1b⚕️Protein sEH⚕️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:07.350704Z","iopub.execute_input":"2024-05-30T17:34:07.351196Z","iopub.status.idle":"2024-05-30T17:38:55.091759Z","shell.execute_reply.started":"2024-05-30T17:34:07.351149Z","shell.execute_reply":"2024-05-30T17:38:55.090364Z"},"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":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-30T17:38:55.094047Z","iopub.execute_input":"2024-05-30T17:38:55.094406Z","iopub.status.idle":"2024-05-30T17:38:59.148698Z","shell.execute_reply.started":"2024-05-30T17:38:55.094372Z","shell.execute_reply":"2024-05-30T17:38:59.146957Z"},"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_1.csv').to_pandas().iloc[:300000] \nfrag0.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:38:59.153704Z","iopub.execute_input":"2024-05-30T17:38:59.154765Z","iopub.status.idle":"2024-05-30T17:39:38.491289Z","shell.execute_reply.started":"2024-05-30T17:38:59.154721Z","shell.execute_reply":"2024-05-30T17:39:38.490436Z"},"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_1.csv').to_pandas().iloc[360000:]\nfrag1.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:39:38.494425Z","iopub.execute_input":"2024-05-30T17:39:38.495685Z","iopub.status.idle":"2024-05-30T17:40:12.512819Z","shell.execute_reply.started":"2024-05-30T17:39:38.49564Z","shell.execute_reply":"2024-05-30T17:40:12.510456Z"},"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:12.516003Z","iopub.execute_input":"2024-05-30T17:40:12.516783Z","iopub.status.idle":"2024-05-30T17:40:19.36802Z","shell.execute_reply.started":"2024-05-30T17:40:12.516721Z","shell.execute_reply":"2024-05-30T17:40:19.366709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del frag0, frag1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:19.369814Z","iopub.execute_input":"2024-05-30T17:40:19.372106Z","iopub.status.idle":"2024-05-30T17:40:19.505248Z","shell.execute_reply.started":"2024-05-30T17:40:19.372049Z","shell.execute_reply":"2024-05-30T17:40:19.50378Z"},"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:19.506837Z","iopub.execute_input":"2024-05-30T17:40:19.507755Z","iopub.status.idle":"2024-05-30T17:40:19.849754Z","shell.execute_reply.started":"2024-05-30T17:40:19.507716Z","shell.execute_reply":"2024-05-30T17:40:19.848492Z"},"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.10, 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:19.851546Z","iopub.execute_input":"2024-05-30T17:40:19.852008Z","iopub.status.idle":"2024-05-30T17:40:51.91783Z","shell.execute_reply.started":"2024-05-30T17:40:19.851968Z","shell.execute_reply":"2024-05-30T17:40:51.916267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X, y\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:40:51.91987Z","iopub.execute_input":"2024-05-30T17:40:51.920426Z","iopub.status.idle":"2024-05-30T17:40:52.107588Z","shell.execute_reply.started":"2024-05-30T17:40:51.920382Z","shell.execute_reply":"2024-05-30T17:40:52.106257Z"},"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:52.112084Z","iopub.execute_input":"2024-05-30T17:40:52.112539Z","iopub.status.idle":"2024-05-30T17:55:17.109985Z","shell.execute_reply.started":"2024-05-30T17:40:52.112503Z","shell.execute_reply":"2024-05-30T17:55:17.108022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T17:55:17.11258Z","iopub.execute_input":"2024-05-30T17:55:17.11312Z","iopub.status.idle":"2024-05-30T17:55:17.302427Z","shell.execute_reply.started":"2024-05-30T17:55:17.113068Z","shell.execute_reply":"2024-05-30T17:55:17.300925Z"},"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:55:17.317687Z","iopub.execute_input":"2024-05-30T17:55:17.3181Z","iopub.status.idle":"2024-05-30T18:18:31.17866Z","shell.execute_reply.started":"2024-05-30T17:55:17.318066Z","shell.execute_reply":"2024-05-30T18:18:31.176648Z"},"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:18:31.181062Z","iopub.execute_input":"2024-05-30T18:18:31.181955Z","iopub.status.idle":"2024-05-30T18:18:31.760083Z","shell.execute_reply.started":"2024-05-30T18:18:31.181911Z","shell.execute_reply":"2024-05-30T18:18:31.758825Z"},"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:18:31.761772Z","iopub.execute_input":"2024-05-30T18:18:31.762169Z","iopub.status.idle":"2024-05-30T18:19:30.272772Z","shell.execute_reply.started":"2024-05-30T18:18:31.762133Z","shell.execute_reply":"2024-05-30T18:19:30.271499Z"},"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:30.274893Z","iopub.execute_input":"2024-05-30T18:19:30.275307Z","iopub.status.idle":"2024-05-30T18:19:30.403145Z","shell.execute_reply.started":"2024-05-30T18:19:30.275263Z","shell.execute_reply":"2024-05-30T18:19:30.401978Z"},"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_1.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:30.404773Z","iopub.execute_input":"2024-05-30T18:19:30.405151Z","iopub.status.idle":"2024-05-30T18:30:39.137348Z","shell.execute_reply.started":"2024-05-30T18:19:30.405119Z","shell.execute_reply":"2024-05-30T18:30:39.135857Z"},"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:30:39.139329Z","iopub.execute_input":"2024-05-30T18:30:39.140626Z","iopub.status.idle":"2024-05-30T18:30:39.782392Z","shell.execute_reply.started":"2024-05-30T18:30:39.140571Z","shell.execute_reply":"2024-05-30T18:30:39.781013Z"},"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:30:39.784095Z","iopub.execute_input":"2024-05-30T18:30:39.784608Z","iopub.status.idle":"2024-05-30T18:30:39.813096Z","shell.execute_reply.started":"2024-05-30T18:30:39.78456Z","shell.execute_reply":"2024-05-30T18:30:39.811755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.to_csv('pred1b.csv', index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-05-30T18:30:39.814666Z","iopub.execute_input":"2024-05-30T18:30:39.815396Z","iopub.status.idle":"2024-05-30T18:30:43.544248Z","shell.execute_reply.started":"2024-05-30T18:30:39.815311Z","shell.execute_reply":"2024-05-30T18:30:43.54237Z"},"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":{}}]}