{"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":"Here is a simple ensemble method for submissions.\nThis idea can be easily referenced from previous competitions\n\nIf you think this kernel is good, please upvote the following people who provided the original kernel instead.\n\n*  https://www.kaggle.com/aikhmelnytskyy/happywhale-arcface-baseline-eff7-tpu-768-inference\n\n* https://www.kaggle.com/nghiahoangtrung/0-720-eff-b5-640-rotate\n\n* https://www.kaggle.com/aikhmelnytskyy/happywhale-effnet-b7-fork-with-detic-training\n\n* https://www.kaggle.com/andrej0marinchenko/happywhale-0-679","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import csv\nimport pandas as pd \n\nsub_files = [\n                 '../input/happywhale-arcface-baseline-eff7-tpu-768-inference/submission.csv',\n                 '../input/0-720-eff-b5-640-rotate/submission.csv',\n                 '../input/happywhale-arcface-baseline-eff7-tpu-768-inference/submission.csv',\n                 '../input/happywhale-effnet-b7-fork-with-detic-crop/submission.csv',\n]\n\n# Weights of the individual subs\nsub_weight = [\n                0.768**2,\n                0.720**2,\n                0.699**2,\n                0.674**2,\n            ]\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T01:37:36.251904Z","iopub.execute_input":"2022-03-16T01:37:36.252239Z","iopub.status.idle":"2022-03-16T01:37:36.278241Z","shell.execute_reply.started":"2022-03-16T01:37:36.252145Z","shell.execute_reply":"2022-03-16T01:37:36.277591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Hlabel = 'image' \nHtarget = 'predictions'\nnpt = 6\nplace_weights = {}\nfor i in range(npt):\n    place_weights[i] = ( 1 / (i + 1) )\n    \nprint(place_weights)\n\nlg = len(sub_files)\nsub = [None]*lg\nfor i, file in enumerate( sub_files ):\n   \n    print(\"Reading {}: w={} - {}\". format(i, sub_weight[i], file))\n    reader = csv.DictReader(open(file,\"r\"))\n    sub[i] = sorted(reader, key=lambda d: str(d[Hlabel]))\n\nout = open(\"sub_ens.csv\", \"w\", newline='')\nwriter = csv.writer(out)\nwriter.writerow([Hlabel,Htarget])\n\nfor p, row in enumerate(sub[0]):\n    target_weight = {}\n    for s in range(lg):\n        row1 = sub[s][p]\n        for ind, trgt in enumerate(row1[Htarget].split(' ')):\n            target_weight[trgt] = target_weight.get(trgt,0) + (place_weights[ind]*sub_weight[s])\n    tops_trgt = sorted(target_weight, key=target_weight.get, reverse=True)[:npt]\n    writer.writerow([row1[Hlabel], \" \".join(tops_trgt)])\nout.close()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T01:37:36.279379Z","iopub.execute_input":"2022-03-16T01:37:36.279671Z","iopub.status.idle":"2022-03-16T01:37:38.027368Z","shell.execute_reply.started":"2022-03-16T01:37:36.279646Z","shell.execute_reply":"2022-03-16T01:37:38.02667Z"},"trusted":true},"execution_count":null,"outputs":[]}]}