{"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":"gpu","dataSources":[{"sourceId":64733,"databundleVersionId":7762802,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Multilabel classification (Dask + XGBoost) ","metadata":{}},{"cell_type":"code","source":"! pip install dask-expr dask xgboost -q\n\n\nimport numpy as np \nimport pandas as pd \n\nimport dask\n\nimport dask.dataframe as dd\n\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-21T18:20:45.574289Z","iopub.execute_input":"2024-03-21T18:20:45.574650Z","iopub.status.idle":"2024-03-21T18:20:58.113939Z","shell.execute_reply.started":"2024-03-21T18:20:45.574620Z","shell.execute_reply":"2024-03-21T18:20:58.112641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading Data","metadata":{}},{"cell_type":"code","source":"#po_train= pd.read_csv('/kaggle/input/geolifeclef-2024/GLC24_P0_metadata_train.csv')\npa_train = pd.read_csv('/kaggle/input/geolifeclef-2024/GLC24_PA_metadata_train.csv')\npa_test = pd.read_csv('/kaggle/input/geolifeclef-2024/GLC24_PA_metadata_train.csv')\nsample_sub = pd.read_csv('/kaggle/input/geolifeclef-2024/GLC24_SAMPLE_SUBMISSION.csv')\n\npa_train = dd.from_pandas(pa_train, npartitions=100)\npa_test = dd.from_pandas(pa_test, npartitions=100)\n\n#print('PO')\n#display(po_train.head())\nprint('PA')\ndisplay(pa_train.head())\ndisplay(pa_test.head())\nprint('Sub')\ndisplay(sample_sub.head())\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:20:58.116199Z","iopub.execute_input":"2024-03-21T18:20:58.116521Z","iopub.status.idle":"2024-03-21T18:21:01.970475Z","shell.execute_reply.started":"2024-03-21T18:20:58.116488Z","shell.execute_reply":"2024-03-21T18:21:01.969631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train['speciesId'] = pa_train['speciesId'].astype(int)\npa_train = pa_train.categorize(columns=['speciesId'])","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:04.296510Z","iopub.execute_input":"2024-03-21T18:21:04.297161Z","iopub.status.idle":"2024-03-21T18:21:04.768593Z","shell.execute_reply.started":"2024-03-21T18:21:04.297131Z","shell.execute_reply":"2024-03-21T18:21:04.767622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:04.770374Z","iopub.execute_input":"2024-03-21T18:21:04.771023Z","iopub.status.idle":"2024-03-21T18:21:04.777908Z","shell.execute_reply.started":"2024-03-21T18:21:04.770990Z","shell.execute_reply":"2024-03-21T18:21:04.776907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:04.792366Z","iopub.execute_input":"2024-03-21T18:21:04.792639Z","iopub.status.idle":"2024-03-21T18:21:04.822755Z","shell.execute_reply.started":"2024-03-21T18:21:04.792616Z","shell.execute_reply":"2024-03-21T18:21:04.821921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Excluding rare classes","metadata":{}},{"cell_type":"code","source":"sum(pa_train.compute()['speciesId'].value_counts() > 40)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:05.871408Z","iopub.execute_input":"2024-03-21T18:21:05.871779Z","iopub.status.idle":"2024-03-21T18:21:06.720299Z","shell.execute_reply.started":"2024-03-21T18:21:05.871748Z","shell.execute_reply":"2024-03-21T18:21:06.719394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"used = pa_train.compute()['speciesId'].value_counts() > 40\n\nused = used.iloc[np.where(used)[0]].index.values\n\nlen(used)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:06.852065Z","iopub.execute_input":"2024-03-21T18:21:06.852383Z","iopub.status.idle":"2024-03-21T18:21:07.677171Z","shell.execute_reply.started":"2024-03-21T18:21:06.852359Z","shell.execute_reply":"2024-03-21T18:21:07.676243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train.compute().shape","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:07.678540Z","iopub.execute_input":"2024-03-21T18:21:07.678801Z","iopub.status.idle":"2024-03-21T18:21:08.477720Z","shell.execute_reply.started":"2024-03-21T18:21:07.678779Z","shell.execute_reply":"2024-03-21T18:21:08.476787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train = pa_train[pa_train['speciesId'].isin(used)]","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:08.479232Z","iopub.execute_input":"2024-03-21T18:21:08.479489Z","iopub.status.idle":"2024-03-21T18:21:08.488263Z","shell.execute_reply.started":"2024-03-21T18:21:08.479467Z","shell.execute_reply":"2024-03-21T18:21:08.487492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_train.compute().shape","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:08.860475Z","iopub.execute_input":"2024-03-21T18:21:08.861204Z","iopub.status.idle":"2024-03-21T18:21:09.893380Z","shell.execute_reply.started":"2024-03-21T18:21:08.861177Z","shell.execute_reply":"2024-03-21T18:21:09.892425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reset dummies so get_dummies works properly\npa_train['speciesId'] = pa_train.compute()['speciesId'].cat.remove_unused_categories()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:12.421684Z","iopub.execute_input":"2024-03-21T18:21:12.422397Z","iopub.status.idle":"2024-03-21T18:21:13.489317Z","shell.execute_reply.started":"2024-03-21T18:21:12.422360Z","shell.execute_reply":"2024-03-21T18:21:13.488520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ohed_pa_train = dd.get_dummies(pa_train.compute(), columns=['speciesId', ], drop_first=True)\n\nohed_pa_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:13.490840Z","iopub.execute_input":"2024-03-21T18:21:13.491144Z","iopub.status.idle":"2024-03-21T18:21:16.799838Z","shell.execute_reply.started":"2024-03-21T18:21:13.491119Z","shell.execute_reply":"2024-03-21T18:21:16.798918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Grouping the target","metadata":{}},{"cell_type":"code","source":"grouped_target = ohed_pa_train[ohed_pa_train.columns[7:]]\ngrouped_target = grouped_target.groupby(by='surveyId').max().reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:16.801749Z","iopub.execute_input":"2024-03-21T18:21:16.802221Z","iopub.status.idle":"2024-03-21T18:21:27.608696Z","shell.execute_reply.started":"2024-03-21T18:21:16.802187Z","shell.execute_reply":"2024-03-21T18:21:27.607669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_features = pa_train[pa_train.columns[:9]].drop_duplicates(subset='surveyId').compute()\npa_features.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:27.634032Z","iopub.execute_input":"2024-03-21T18:21:27.634388Z","iopub.status.idle":"2024-03-21T18:21:32.690002Z","shell.execute_reply.started":"2024-03-21T18:21:27.634362Z","shell.execute_reply":"2024-03-21T18:21:32.689033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Cleanup","metadata":{}},{"cell_type":"code","source":"print(pa_features.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:21:32.691556Z","iopub.execute_input":"2024-03-21T18:21:32.691900Z","iopub.status.idle":"2024-03-21T18:21:32.716109Z","shell.execute_reply.started":"2024-03-21T18:21:32.691868Z","shell.execute_reply":"2024-03-21T18:21:32.715192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### areaInM2 has also inf values so I replace them to","metadata":{}},{"cell_type":"code","source":"pa_features['geoUncertaintyInM'] = pa_features['geoUncertaintyInM'].fillna(pa_features['geoUncertaintyInM'].dropna().mean())\npa_features['areaInM2'] = pa_features['areaInM2'].replace([np.inf, -np.inf], np.nan).fillna(pa_features['areaInM2'].replace([np.inf, -np.inf], np.nan).dropna().mean())","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:24.786692Z","iopub.execute_input":"2024-03-21T18:23:24.787073Z","iopub.status.idle":"2024-03-21T18:23:24.798823Z","shell.execute_reply.started":"2024-03-21T18:23:24.787042Z","shell.execute_reply":"2024-03-21T18:23:24.798027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pa_features = pa_features.sort_values('surveyId')\ngrouped_target = grouped_target.sort_values('surveyId')","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:26.321547Z","iopub.execute_input":"2024-03-21T18:23:26.322227Z","iopub.status.idle":"2024-03-21T18:23:26.400654Z","shell.execute_reply.started":"2024-03-21T18:23:26.322193Z","shell.execute_reply":"2024-03-21T18:23:26.399543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### One hot encoding of categorical columns","metadata":{}},{"cell_type":"code","source":"pa_features = dd.get_dummies(pa_features, columns=['region', 'country', 'year'], drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:28.211728Z","iopub.execute_input":"2024-03-21T18:23:28.212124Z","iopub.status.idle":"2024-03-21T18:23:28.245528Z","shell.execute_reply.started":"2024-03-21T18:23:28.212093Z","shell.execute_reply":"2024-03-21T18:23:28.244573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The result of aggregation:","metadata":{}},{"cell_type":"code","source":"print('Features')\ndisplay(pa_features.head())\nprint('Multilabel Binary Targets')\ndisplay(grouped_target.head())","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:29.511295Z","iopub.execute_input":"2024-03-21T18:23:29.511657Z","iopub.status.idle":"2024-03-21T18:23:29.558741Z","shell.execute_reply.started":"2024-03-21T18:23:29.511627Z","shell.execute_reply":"2024-03-21T18:23:29.555882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pa_features.shape, grouped_target.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:32.929344Z","iopub.execute_input":"2024-03-21T18:23:32.929952Z","iopub.status.idle":"2024-03-21T18:23:32.934864Z","shell.execute_reply.started":"2024-03-21T18:23:32.929922Z","shell.execute_reply":"2024-03-21T18:23:32.933878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Base modeling","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:34.099821Z","iopub.execute_input":"2024-03-21T18:23:34.100545Z","iopub.status.idle":"2024-03-21T18:23:34.104731Z","shell.execute_reply.started":"2024-03-21T18:23:34.100512Z","shell.execute_reply":"2024-03-21T18:23:34.103626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(pa_features.drop(columns=['surveyId', 'speciesId']), grouped_target.drop(columns='surveyId'))","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:23:34.820628Z","iopub.execute_input":"2024-03-21T18:23:34.820994Z","iopub.status.idle":"2024-03-21T18:23:35.108803Z","shell.execute_reply.started":"2024-03-21T18:23:34.820946Z","shell.execute_reply":"2024-03-21T18:23:35.107993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:27:45.833869Z","iopub.execute_input":"2024-03-21T18:27:45.834268Z","iopub.status.idle":"2024-03-21T18:27:45.865234Z","shell.execute_reply.started":"2024-03-21T18:27:45.834238Z","shell.execute_reply":"2024-03-21T18:27:45.864349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_val.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:28:04.915372Z","iopub.execute_input":"2024-03-21T18:28:04.916214Z","iopub.status.idle":"2024-03-21T18:28:04.922625Z","shell.execute_reply.started":"2024-03-21T18:28:04.916182Z","shell.execute_reply":"2024-03-21T18:28:04.921589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:28:05.736407Z","iopub.execute_input":"2024-03-21T18:28:05.736769Z","iopub.status.idle":"2024-03-21T18:28:05.741157Z","shell.execute_reply.started":"2024-03-21T18:28:05.736742Z","shell.execute_reply":"2024-03-21T18:28:05.740134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = XGBClassifier(\n    objective='binary:logistic',\n    colsample_bylevel=0.90,\n    colsample_bytree=0.85,\n    colsample_bynode=0.80,\n    reg_lambda=0.25,\n    reg_alpha=0.70,\n    device='gpu',\n    early_stopping_rounds=5\n    )\n\nmodel.fit(X_train, y_train,eval_set=[(X_val, y_val)], verbose=5)","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:28:37.854977Z","iopub.execute_input":"2024-03-21T18:28:37.855331Z","iopub.status.idle":"2024-03-21T18:35:12.934728Z","shell.execute_reply.started":"2024-03-21T18:28:37.855304Z","shell.execute_reply":"2024-03-21T18:35:12.933749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Saving the model","metadata":{}},{"cell_type":"code","source":"model.save_model('xgb_clf')","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:35:18.097537Z","iopub.execute_input":"2024-03-21T18:35:18.097896Z","iopub.status.idle":"2024-03-21T18:35:18.386809Z","shell.execute_reply.started":"2024-03-21T18:35:18.097868Z","shell.execute_reply":"2024-03-21T18:35:18.386007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making predictions","metadata":{}},{"cell_type":"code","source":"sample_sub.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-21T18:36:17.868849Z","iopub.execute_input":"2024-03-21T18:36:17.869223Z","iopub.status.idle":"2024-03-21T18:36:17.877997Z","shell.execute_reply.started":"2024-03-21T18:36:17.869193Z","shell.execute_reply":"2024-03-21T18:36:17.877134Z"},"trusted":true},"execution_count":null,"outputs":[]}]}