{"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":"# <div style=\"color:white;display:fill;border-radius:5px;background-color:#75B7BF;letter-spacing:0.1px;overflow:hidden\"><p style=\"padding:20px;color:white;overflow:hidden;margin:0;font-size:100%;text-align:center\">Import</p></div>","metadata":{}},{"cell_type":"code","source":"import csv\nimport pandas as pd ","metadata":{"execution":{"iopub.status.busy":"2022-04-01T01:53:56.902194Z","iopub.execute_input":"2022-04-01T01:53:56.902612Z","iopub.status.idle":"2022-04-01T01:53:56.912721Z","shell.execute_reply.started":"2022-04-01T01:53:56.902508Z","shell.execute_reply":"2022-04-01T01:53:56.912104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_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.768**2.4,\n                0.830**1.9,\n                0.699**2.8,\n                0.674**2.7,\n            ]","metadata":{"execution":{"iopub.status.busy":"2022-04-01T01:53:59.432270Z","iopub.execute_input":"2022-04-01T01:53:59.432707Z","iopub.status.idle":"2022-04-01T01:53:59.437385Z","shell.execute_reply.started":"2022-04-01T01:53:59.432677Z","shell.execute_reply":"2022-04-01T01:53:59.436512Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-04-01T01:54:04.275863Z","iopub.execute_input":"2022-04-01T01:54:04.276155Z","iopub.status.idle":"2022-04-01T01:54:04.282328Z","shell.execute_reply.started":"2022-04-01T01:54:04.276112Z","shell.execute_reply":"2022-04-01T01:54:04.281328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lg = 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(\"submission.csv\", \"w\", newline='')\nwriter = csv.writer(out)\nwriter.writerow([Hlabel,Htarget])","metadata":{"execution":{"iopub.status.busy":"2022-04-01T01:40:13.922102Z","iopub.execute_input":"2022-04-01T01:40:13.922545Z","iopub.status.idle":"2022-04-01T01:40:14.952868Z","shell.execute_reply.started":"2022-04-01T01:40:13.922515Z","shell.execute_reply":"2022-04-01T01:40:14.951919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for 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-01T01:40:14.955585Z","iopub.execute_input":"2022-04-01T01:40:14.95589Z","iopub.status.idle":"2022-04-01T01:40:15.745555Z","shell.execute_reply.started":"2022-04-01T01:40:14.95585Z","shell.execute_reply":"2022-04-01T01:40:15.744564Z"},"trusted":true},"execution_count":null,"outputs":[]}]}