{"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":"# Welcome back !","metadata":{}},{"cell_type":"markdown","source":"Original notebook : https://www.kaggle.com/code/yamsam/simple-ensemble-of-public-best-kernels","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"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":{}},{"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/arcfaceeffb6inferbaseline/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.81**2.4,\n                0.830**1.9,\n                0.650**2.8,\n                0.650**6,\n            ]\n","metadata":{"execution":{"iopub.status.busy":"2022-04-02T14:53:58.855809Z","iopub.execute_input":"2022-04-02T14:53:58.856355Z","iopub.status.idle":"2022-04-02T14:53:58.862196Z","shell.execute_reply.started":"2022-04-02T14:53:58.856313Z","shell.execute_reply":"2022-04-02T14:53:58.861048Z"},"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    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(\"submission.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-04-02T14:54:01.787294Z","iopub.execute_input":"2022-04-02T14:54:01.78778Z","iopub.status.idle":"2022-04-02T14:54:03.571882Z","shell.execute_reply.started":"2022-04-02T14:54:01.787747Z","shell.execute_reply":"2022-04-02T14:54:03.571023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center>\n    <h1 style='colors=#f13658'> Thanks for reading 👍 <h1>","metadata":{}}]}