{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = open(\"/kaggle/input/expedia-hotel-recommendations/train.csv\", \"r\")\na = f.readline().strip().split(',')\nprint(a[5])\nprint(a[6])\nprint(a[16])\nprint(a[21])\nprint(a[-1])\nprint(a[-6])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict\n\nd_city_distance = defaultdict(lambda: defaultdict(int))\nd_destination = defaultdict(lambda: defaultdict(int))\nd_country = defaultdict(lambda: defaultdict(int))\nd_overall = defaultdict(int)\n\nn = 0\n\nwhile(1):\n    line = f.readline().strip()\n    n += 1\n    \n    if n % 1000000 == 0:\n        print('Read {} lines...'.format(n))\n    \n    if line == '':\n        break\n        \n    arr = line.split(',')\n    \n    city = arr[5]\n    distance = arr[6]\n    destination = arr[16]\n    country = arr[21]\n    score = 20 if arr[-6] == 1 else 1\n    hotel = arr[-1]\n    \n    if city != '' and distance != '':\n        d_city_distance[(city, distance)][hotel] += score\n    \n    if destination != '':\n        d_destination[destination][hotel] += score\n    \n    if country != '':\n        d_country[country][hotel] += score\n    \n    d_overall[hotel] += score","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f.close()\nf2 = open(\"/kaggle/input/expedia-hotel-recommendations/test.csv\",\"r\")\na = f2.readline().split(',')\nprint(a[0])\nprint(a[6])\nprint(a[7])\nprint(a[17])\nprint(a[20])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = open(\"submission.csv\",\"w\")\nout.write(\"id,hotel_cluster\\n\")\n\nfrom heapq import nlargest\nfrom operator import itemgetter\n\ntop_overall = nlargest(5, sorted(d_overall.items()), key=itemgetter(1))\n\nn = 0\nwhile(1):\n    line = f2.readline().strip()\n    n += 1\n    \n    if n % 100000 == 0:\n        print('Read {} lines...'.format(n))\n    \n    if line == '':\n        break\n    \n    arr = line.split(',')\n    \n    idx = arr[0]\n    city = arr[6]\n    distance = arr[7]\n    destination = arr[17]\n    country = arr[20]\n    \n    out.write(str(idx) + ',')\n    recommend = []\n    \n    if (city, distance) in d_city_distance:\n        hotels_1 = d_city_distance[(city, distance)]\n        topitems = nlargest(5, hotels_1.items(), key=itemgetter(1))\n        for i in range(len(topitems)):\n            if len(recommend) == 5:\n                break\n            if topitems[i][0] in recommend:\n                continue\n            out.write(' ' + topitems[i][0])\n            recommend.append(topitems[i][0])\n            \n    if destination in d_destination:\n        hotels_2 = d_destination[destination]\n        topitems = nlargest(5, hotels_2.items(), key=itemgetter(1))\n        for i in range(len(topitems)):\n            if len(recommend) == 5:\n                break\n            if topitems[i][0] in recommend:\n                continue\n            out.write(' ' + topitems[i][0])\n            recommend.append(topitems[i][0])\n            \n    if country in d_country:\n        hotels_3 = d_country[country]\n        topitems = nlargest(5, hotels_3.items(), key=itemgetter(1))\n        for i in range(len(topitems)):\n            if len(recommend) == 5:\n                break\n            if topitems[i][0] in recommend:\n                continue\n            out.write(' ' + topitems[i][0])\n            recommend.append(topitems[i][0])\n            \n    for i in range(len(top_overall)):\n        if len(recommend) == 5:\n            break\n        if top_overall[i][0] in recommend:\n            continue\n        out.write(' ' + top_overall[i][0])\n        recommend.append(top_overall[i][0])\n    \n    out.write('\\n')\nout.close()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}