{"cells":[{"metadata":{"_uuid":"35f6ba33bea4161e30c9904c842f6ecc6ee2ad78"},"cell_type":"markdown","source":"This kernel is based on the approach from https://www.kaggle.com/matthewa313/ensembling-algorithm-for-average-precision-metric\n\nTake predictions from LB 0.771 from https://www.kaggle.com/ateplyuk/ensembling-lb-0-771\n\nTake predictions from LB 0.766 https://www.kaggle.com/hung96ad/ensembling-algorithm-for-average-precision-0-766\n\nPlug your outputs to this kernel and you're good to go."},{"metadata":{"trusted":true,"_uuid":"91805939d0877e59cc3ad04461750c25b506f196"},"cell_type":"code","source":"import os\nos.listdir('../input')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import csv\nimport pandas as pd # not key to functionality of kernel\n\nsub_files = [\n                '../input/publickernel/sub_766.csv',\n                '../input/publickernel/sub_771.csv'\n]\n\n# Weights of the individual subs\nsub_weight = [\n                0.766**2,\n                0.771**2\n            ]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"Hlabel = 'Image' \nHtarget = 'Id'\nnpt = 5 # number of places in target\n\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    ## input 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\n## output file ##\nout = open(\"sub_ns.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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a618597a38a8e3814aecdbb37709ab55af36e21"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}