{"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 1a⚕️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-30T18:57:27.942521Z","iopub.execute_input":"2024-05-30T18:57:27.943426Z","iopub.status.idle":"2024-05-30T19:02:07.086033Z","shell.execute_reply.started":"2024-05-30T18:57:27.943376Z","shell.execute_reply":"2024-05-30T19:02:07.084646Z"},"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-30T19:02:07.088584Z","iopub.execute_input":"2024-05-30T19:02:07.089036Z","iopub.status.idle":"2024-05-30T19:02:10.911896Z","shell.execute_reply.started":"2024-05-30T19:02:07.088992Z","shell.execute_reply":"2024-05-30T19:02:10.910536Z"},"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-30T19:02:10.913716Z","iopub.execute_input":"2024-05-30T19:02:10.914612Z","iopub.status.idle":"2024-05-30T19:02:32.681326Z","shell.execute_reply.started":"2024-05-30T19:02:10.914575Z","shell.execute_reply":"2024-05-30T19:02:32.680218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Duplicates in frag0:',  frag0.duplicated().sum())","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:02:32.684923Z","iopub.execute_input":"2024-05-30T19:02:32.685401Z","iopub.status.idle":"2024-05-30T19:02:42.810569Z","shell.execute_reply.started":"2024-05-30T19:02:32.68536Z","shell.execute_reply":"2024-05-30T19:02:42.809232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frag0.drop_duplicates(inplace=True)\nfrag0.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:02:42.812042Z","iopub.execute_input":"2024-05-30T19:02:42.812513Z","iopub.status.idle":"2024-05-30T19:02:52.727091Z","shell.execute_reply.started":"2024-05-30T19:02:42.812463Z","shell.execute_reply":"2024-05-30T19:02:52.725901Z"},"trusted":true},"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-30T19:02:52.728644Z","iopub.execute_input":"2024-05-30T19:02:52.729039Z","iopub.status.idle":"2024-05-30T19:03:11.49275Z","shell.execute_reply.started":"2024-05-30T19:02:52.729008Z","shell.execute_reply":"2024-05-30T19:03:11.491681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Duplicates in frag1:',  frag1.duplicated().sum())","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:03:11.494073Z","iopub.execute_input":"2024-05-30T19:03:11.494433Z","iopub.status.idle":"2024-05-30T19:03:23.695856Z","shell.execute_reply.started":"2024-05-30T19:03:11.494402Z","shell.execute_reply":"2024-05-30T19:03:23.694588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frag1.drop_duplicates(inplace=True)\nfrag1.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:03:23.69744Z","iopub.execute_input":"2024-05-30T19:03:23.698422Z","iopub.status.idle":"2024-05-30T19:03:47.461434Z","shell.execute_reply.started":"2024-05-30T19:03:23.698388Z","shell.execute_reply":"2024-05-30T19:03:47.459632Z"},"trusted":true},"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-30T19:03:47.464077Z","iopub.execute_input":"2024-05-30T19:03:47.464613Z","iopub.status.idle":"2024-05-30T19:03:54.606366Z","shell.execute_reply.started":"2024-05-30T19:03:47.464568Z","shell.execute_reply":"2024-05-30T19:03:54.605231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del frag0, frag1\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:03:54.611268Z","iopub.execute_input":"2024-05-30T19:03:54.611676Z","iopub.status.idle":"2024-05-30T19:03:54.798424Z","shell.execute_reply.started":"2024-05-30T19:03:54.611645Z","shell.execute_reply":"2024-05-30T19:03:54.797074Z"},"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-30T19:03:54.799951Z","iopub.execute_input":"2024-05-30T19:03:54.800419Z","iopub.status.idle":"2024-05-30T19:03:55.163693Z","shell.execute_reply.started":"2024-05-30T19:03:54.800384Z","shell.execute_reply":"2024-05-30T19:03:55.162386Z"},"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-30T19:03:55.165189Z","iopub.execute_input":"2024-05-30T19:03:55.165599Z","iopub.status.idle":"2024-05-30T19:04:23.318054Z","shell.execute_reply.started":"2024-05-30T19:03:55.165567Z","shell.execute_reply":"2024-05-30T19:04:23.316822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X, y\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:04:23.31962Z","iopub.execute_input":"2024-05-30T19:04:23.320058Z","iopub.status.idle":"2024-05-30T19:04:23.465665Z","shell.execute_reply.started":"2024-05-30T19:04:23.320016Z","shell.execute_reply":"2024-05-30T19:04:23.464204Z"},"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-30T19:04:23.467743Z","iopub.execute_input":"2024-05-30T19:04:23.46823Z","iopub.status.idle":"2024-05-30T19:18:35.366811Z","shell.execute_reply.started":"2024-05-30T19:04:23.468191Z","shell.execute_reply":"2024-05-30T19:18:35.365352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:18:35.368593Z","iopub.execute_input":"2024-05-30T19:18:35.368962Z","iopub.status.idle":"2024-05-30T19:18:35.494465Z","shell.execute_reply.started":"2024-05-30T19:18:35.368932Z","shell.execute_reply":"2024-05-30T19:18:35.493127Z"},"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-30T19:18:35.506454Z","iopub.execute_input":"2024-05-30T19:18:35.506962Z","iopub.status.idle":"2024-05-30T19:29:40.680158Z","shell.execute_reply.started":"2024-05-30T19:18:35.50692Z","shell.execute_reply":"2024-05-30T19:29:40.678063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, y_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:29:40.682842Z","iopub.execute_input":"2024-05-30T19:29:40.68321Z","iopub.status.idle":"2024-05-30T19:29:41.726667Z","shell.execute_reply.started":"2024-05-30T19:29:40.683181Z","shell.execute_reply":"2024-05-30T19:29:41.724085Z"},"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-30T19:29:41.730375Z","iopub.execute_input":"2024-05-30T19:29:41.73152Z","iopub.status.idle":"2024-05-30T19:30:28.151187Z","shell.execute_reply.started":"2024-05-30T19:29:41.731479Z","shell.execute_reply":"2024-05-30T19:30:28.149832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_test, y_test\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:30:28.152932Z","iopub.execute_input":"2024-05-30T19:30:28.153402Z","iopub.status.idle":"2024-05-30T19:30:28.277917Z","shell.execute_reply.started":"2024-05-30T19:30:28.153359Z","shell.execute_reply":"2024-05-30T19:30:28.276749Z"},"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-30T19:30:28.279385Z","iopub.execute_input":"2024-05-30T19:30:28.279835Z","iopub.status.idle":"2024-05-30T19:38:40.806125Z","shell.execute_reply.started":"2024-05-30T19:30:28.279802Z","shell.execute_reply":"2024-05-30T19:38:40.804832Z"},"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-30T19:38:40.807804Z","iopub.execute_input":"2024-05-30T19:38:40.808166Z","iopub.status.idle":"2024-05-30T19:38:41.319689Z","shell.execute_reply.started":"2024-05-30T19:38:40.808129Z","shell.execute_reply":"2024-05-30T19:38:41.318596Z"},"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-30T19:38:41.321472Z","iopub.execute_input":"2024-05-30T19:38:41.321868Z","iopub.status.idle":"2024-05-30T19:38:41.342121Z","shell.execute_reply.started":"2024-05-30T19:38:41.321836Z","shell.execute_reply":"2024-05-30T19:38:41.34082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.to_csv('pred1a.csv', index=False)\n!ls","metadata":{"execution":{"iopub.status.busy":"2024-05-30T19:38:41.343473Z","iopub.execute_input":"2024-05-30T19:38:41.343832Z","iopub.status.idle":"2024-05-30T19:38:44.975203Z","shell.execute_reply.started":"2024-05-30T19:38:41.343792Z","shell.execute_reply":"2024-05-30T19:38:44.973337Z"},"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":{}}]}