{"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":"I used @radek1's approach for voting ensemble: https://www.kaggle.com/code/radek1/2-methods-how-to-ensemble-predictions","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","metadata":{"execution":{"iopub.status.busy":"2023-01-24T13:38:00.693555Z","iopub.execute_input":"2023-01-24T13:38:00.694065Z","iopub.status.idle":"2023-01-24T13:38:15.889644Z","shell.execute_reply.started":"2023-01-24T13:38:00.693966Z","shell.execute_reply":"2023-01-24T13:38:15.888421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\npaths = ['/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-24T13:38:15.891634Z","iopub.execute_input":"2023-01-24T13:38:15.891996Z","iopub.status.idle":"2023-01-24T13:38:15.951876Z","shell.execute_reply.started":"2023-01-24T13:38:15.891961Z","shell.execute_reply":"2023-01-24T13:38:15.950498Z"},"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-24T13:38:15.953510Z","iopub.execute_input":"2023-01-24T13:38:15.954231Z","iopub.status.idle":"2023-01-24T13:38:15.963186Z","shell.execute_reply.started":"2023-01-24T13:38:15.954184Z","shell.execute_reply":"2023-01-24T13:38:15.962008Z"},"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-24T13:38:15.965623Z","iopub.execute_input":"2023-01-24T13:38:15.966038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = [1.0, 1.0, 10]\n\nfor i in range(len(paths)) :\n    sub = read_sub(paths[i], weights[i])\n    subs.append(sub)\n\n\nsubs = 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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.write_csv('submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}