{"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":"markdown","source":"This notebook shows how to ensemble predictions using Polars library. \n\nI use [RADEK OSMULSKI](https://www.kaggle.com/code/radek1/2-methods-how-to-ensemble-predictions)'s notebook. Please visit and vote his work.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Loading the data","metadata":{}},{"cell_type":"code","source":"!pip install polars # why are we using polars? it has much smaller memory footprint than pandas!","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:18:55.242074Z","iopub.execute_input":"2023-01-08T01:18:55.242682Z","iopub.status.idle":"2023-01-08T01:19:10.921263Z","shell.execute_reply.started":"2023-01-08T01:18:55.242549Z","shell.execute_reply":"2023-01-08T01:19:10.919745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:19:20.959754Z","iopub.execute_input":"2023-01-08T01:19:20.960205Z","iopub.status.idle":"2023-01-08T01:19:21.031390Z","shell.execute_reply.started":"2023-01-08T01:19:20.960165Z","shell.execute_reply":"2023-01-08T01:19:21.029505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = ['../input/otto-submissions-for-ensembling/submission_rerank_0575.csv', '../input/otto-submissions-for-ensembling/submission_test_all_0522.csv', '../input/otto-submissions-for-ensembling/submission_matrix_factorization_0493.csv']","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:19:26.483103Z","iopub.execute_input":"2023-01-08T01:19:26.484301Z","iopub.status.idle":"2023-01-08T01:19:26.490536Z","shell.execute_reply.started":"2023-01-08T01:19:26.484259Z","shell.execute_reply":"2023-01-08T01:19:26.489354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_sub(path, weight=1): # by default let us assing the weight of 1 to predictions from each submission, this will be akin to a standard vote ensemble\n    '''a helper function for loading and preprocessing submissions'''\n    return (\n        pl.read_csv(path)\n            .with_column(pl.col('labels').str.split(by=' '))\n            .with_column(pl.lit(weight).alias('vote'))\n            .explode('labels')\n            .rename({'labels': 'aid'})\n            .with_column(pl.col('aid').cast(pl.UInt32)) # we are casting the `aids` to `Int32`! memory management is super important to ensure we don't run out of resources\n            .with_column(pl.col('vote').cast(pl.UInt8))\n    )","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:19:31.499705Z","iopub.execute_input":"2023-01-08T01:19:31.500381Z","iopub.status.idle":"2023-01-08T01:19:31.514745Z","shell.execute_reply.started":"2023-01-08T01:19:31.500329Z","shell.execute_reply":"2023-01-08T01:19:31.513397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs = [read_sub(path) for path in paths]\nsubs[0].head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:19:35.469506Z","iopub.execute_input":"2023-01-08T01:19:35.470057Z","iopub.status.idle":"2023-01-08T01:20:43.611476Z","shell.execute_reply.started":"2023-01-08T01:19:35.470005Z","shell.execute_reply":"2023-01-08T01:20:43.604896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs = subs[0].join(subs[1], how='outer', on=['session_type', 'aid']).join(subs[2], how='outer', on=['session_type', 'aid'], suffix='_right2')\nsubs.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:21:07.789769Z","iopub.execute_input":"2023-01-08T01:21:07.790265Z","iopub.status.idle":"2023-01-08T01:24:24.769732Z","shell.execute_reply.started":"2023-01-08T01:21:07.790216Z","shell.execute_reply":"2023-01-08T01:24:24.768117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subs = (subs\n    .fill_null(0)\n    .with_column((pl.col('vote') + pl.col('vote_right') + pl.col('vote_right2')).alias('vote_sum'))\n    .drop(['vote', 'vote_right', 'vote_right2'])\n    .sort(by='vote_sum')\n    .reverse()\n)\n\nsubs.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:25:46.821678Z","iopub.execute_input":"2023-01-08T01:25:46.822205Z","iopub.status.idle":"2023-01-08T01:26:22.031519Z","shell.execute_reply.started":"2023-01-08T01:25:46.822160Z","shell.execute_reply":"2023-01-08T01:26:22.030118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = subs.groupby('session_type').agg([\n    pl.col('aid').head(20).alias('labels')\n])\n\npreds = preds.with_column(pl.col('labels').apply(lambda lst: ' '.join([str(aid) for aid in lst])))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:26:34.823363Z","iopub.execute_input":"2023-01-08T01:26:34.823826Z","iopub.status.idle":"2023-01-08T01:32:11.278620Z","shell.execute_reply.started":"2023-01-08T01:26:34.823794Z","shell.execute_reply":"2023-01-08T01:32:11.277248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\npreds.write_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T01:33:39.095804Z","iopub.execute_input":"2023-01-08T01:33:39.096381Z","iopub.status.idle":"2023-01-08T01:33:41.010836Z","shell.execute_reply.started":"2023-01-08T01:33:39.096339Z","shell.execute_reply":"2023-01-08T01:33:41.009784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Summary\n\nWe now have a way to perfom voting ensemble (including using custom weights) even within the limits of a Kaggle VM! Ensembling will certainly be a major component of strong submissions.\n\n**If you enjoyed this notebook, please upvote! 🙏 Thank you!**\n\nThank you for reading, happy Kaggling! 🙂","metadata":{}}]}