{"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":"# [OTTO – Multi-Objective Recommender System](https://www.kaggle.com/competitions/otto-recommender-system)","metadata":{}},{"cell_type":"markdown","source":"## Many thanks to:\n- [0.578 | Ensemble of Public Notebooks](https://www.kaggle.com/code/karakasatarik/0-578-ensemble-of-public-notebooks)\n- [💡 [2 methods] How-to ensemble predictions 🏅🏅🏅](https://www.kaggle.com/code/radek1/2-methods-how-to-ensemble-predictions)\n- [Candidate ReRank Model - [LB 0.575]](https://www.kaggle.com/code/cdeotte/candidate-rerank-model-lb-0-575)\n- [otto-pipeline2 [LB 0.576]](https://www.kaggle.com/code/tuongkhang/otto-pipeline2-lb-0-576)\n- [OTTO: Tuning Candidate ReRank Model[LB 0.577]](https://www.kaggle.com/code/utm529fg/otto-tuning-candidate-rerank-model-lb-0-577)","metadata":{}},{"cell_type":"code","source":"!pip install polars","metadata":{"execution":{"iopub.status.busy":"2023-01-06T20:10:53.316569Z","iopub.execute_input":"2023-01-06T20:10:53.317199Z","iopub.status.idle":"2023-01-06T20:11:09.907913Z","shell.execute_reply.started":"2023-01-06T20:10:53.31708Z","shell.execute_reply":"2023-01-06T20:11:09.906346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\npaths = ['/kaggle/input/0-578-ensemble-of-public-notebooks/submission.csv',  # 0.578\n         #'/kaggle/input/candidate-rerank-model-lb-0-575/submission.csv', # 0.575\n         '/kaggle/input/otto-pipeline2-lb-0-576/submission.csv', # 0.576\n         '/kaggle/input/otto-tuning-candidate-rerank-model-lb-0-577/submission.csv' # 0.577\n        ]","metadata":{"execution":{"iopub.status.busy":"2023-01-06T20:11:09.911042Z","iopub.execute_input":"2023-01-06T20:11:09.911665Z","iopub.status.idle":"2023-01-06T20:11:09.987025Z","shell.execute_reply.started":"2023-01-06T20:11:09.9116Z","shell.execute_reply":"2023-01-06T20:11:09.985865Z"},"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-06T20:11:09.988576Z","iopub.execute_input":"2023-01-06T20:11:09.989846Z","iopub.status.idle":"2023-01-06T20:11:09.998668Z","shell.execute_reply.started":"2023-01-06T20:11:09.989798Z","shell.execute_reply":"2023-01-06T20:11:09.997182Z"},"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-06T20:11:10.001038Z","iopub.execute_input":"2023-01-06T20:11:10.001784Z","iopub.status.idle":"2023-01-06T20:12:27.8318Z","shell.execute_reply.started":"2023-01-06T20:11:10.001741Z","shell.execute_reply":"2023-01-06T20:12:27.83057Z"},"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-06T20:12:27.833454Z","iopub.execute_input":"2023-01-06T20:12:27.833871Z","iopub.status.idle":"2023-01-06T20:17:03.485719Z","shell.execute_reply.started":"2023-01-06T20:12:27.833832Z","shell.execute_reply":"2023-01-06T20:17:03.484354Z"},"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-06T20:17:03.487764Z","iopub.execute_input":"2023-01-06T20:17:03.488915Z","iopub.status.idle":"2023-01-06T20:17:25.167271Z","shell.execute_reply.started":"2023-01-06T20:17:03.488871Z","shell.execute_reply":"2023-01-06T20:17:25.165884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npreds = 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-06T20:17:25.170687Z","iopub.execute_input":"2023-01-06T20:17:25.17164Z","iopub.status.idle":"2023-01-06T20:22:53.842563Z","shell.execute_reply.started":"2023-01-06T20:17:25.171588Z","shell.execute_reply":"2023-01-06T20:22:53.840964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.write_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-06T20:22:53.845134Z","iopub.execute_input":"2023-01-06T20:22:53.845919Z","iopub.status.idle":"2023-01-06T20:22:58.315521Z","shell.execute_reply.started":"2023-01-06T20:22:53.845859Z","shell.execute_reply":"2023-01-06T20:22:58.30927Z"},"trusted":true},"execution_count":null,"outputs":[]}]}