{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# coding: utf-8\n__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'\n\nimport datetime\nfrom heapq import nlargest\nfrom operator import itemgetter\nfrom collections import defaultdict\n\n\ndef run_solution():\n    print('Preparing arrays...')\n    f = open(\"../input/train.csv\", \"r\")\n    f.readline()\n    best_hotels_od_ulc = defaultdict(lambda: defaultdict(int))\n    best_hotels_search_dest = defaultdict(lambda: defaultdict(int))\n    best_hotels_search_dest1 = defaultdict(lambda: defaultdict(int))\n    best_hotel_country = defaultdict(lambda: defaultdict(int))\n    popular_hotel_cluster = defaultdict(int)\n    total = 0\n\n    # Calc counts\n    while 1:\n        line = f.readline().strip()\n        total += 1\n\n        if total % 10000000 == 0:\n            print('Read {} lines...'.format(total))\n\n        if line == '':\n            break\n\n        arr = line.split(\",\")\n        book_year = int(arr[0][:4])\n        user_location_city = arr[5]\n        orig_destination_distance = arr[6]\n        srch_destination_id = arr[16]\n        is_booking = int(arr[18])\n        hotel_country = arr[21]\n        hotel_market = arr[22]\n        hotel_cluster = arr[23]\n\n        append_1 = 3 + 17*is_booking\n        append_2 = 1 + 5*is_booking\n\n        if user_location_city != '' and orig_destination_distance != '':\n            best_hotels_od_ulc[(user_location_city, orig_destination_distance)][hotel_cluster] += 1\n\n        if srch_destination_id != '' and hotel_country != '' and hotel_market != '' and book_year == 2014:\n            best_hotels_search_dest[(srch_destination_id, hotel_country, hotel_market)][hotel_cluster] += append_1\n        \n        if srch_destination_id != '':\n            best_hotels_search_dest1[srch_destination_id][hotel_cluster] += append_1\n        \n        if hotel_country != '':\n            best_hotel_country[hotel_country][hotel_cluster] += append_2\n        \n        popular_hotel_cluster[hotel_cluster] += 1\n    \n    f.close()\n\n    print('Generate submission...')\n    now = datetime.datetime.now()\n    path = 'submission_' + str(now.strftime(\"%Y-%m-%d-%H-%M\")) + '.csv'\n    out = open(path, \"w\")\n    f = open(\"../input/test.csv\", \"r\")\n    f.readline()\n    total = 0\n    out.write(\"id,hotel_cluster\\n\")\n    topclasters = nlargest(5, sorted(popular_hotel_cluster.items()), key=itemgetter(1))\n\n    while 1:\n        line = f.readline().strip()\n        total += 1\n\n        if total % 1000000 == 0:\n            print('Write {} lines...'.format(total))\n\n        if line == '':\n            break\n\n        arr = line.split(\",\")\n        id = arr[0]\n        user_location_city = arr[6]\n        orig_destination_distance = arr[7]\n        srch_destination_id = arr[17]\n        hotel_country = arr[20]\n        hotel_market = arr[21]\n\n        out.write(str(id) + ',')\n        filled = []\n\n        s1 = (user_location_city, orig_destination_distance)\n        if s1 in best_hotels_od_ulc:\n            d = best_hotels_od_ulc[s1]\n            topitems = nlargest(5, sorted(d.items()), key=itemgetter(1))\n            for i in range(len(topitems)):\n                if topitems[i][0] in filled:\n                    continue\n                if len(filled) == 5:\n                    break\n                out.write(' ' + topitems[i][0])\n                filled.append(topitems[i][0])\n\n        s2 = (srch_destination_id, hotel_country, hotel_market)\n        if s2 in best_hotels_search_dest:\n            d = best_hotels_search_dest[s2]\n            topitems = nlargest(5, d.items(), key=itemgetter(1))\n            for i in range(len(topitems)):\n                if topitems[i][0] in filled:\n                    continue\n                if len(filled) == 5:\n                    break\n                out.write(' ' + topitems[i][0])\n                filled.append(topitems[i][0])\n        elif srch_destination_id in best_hotels_search_dest1:\n            d = best_hotels_search_dest1[srch_destination_id]\n            topitems = nlargest(5, d.items(), key=itemgetter(1))\n            for i in range(len(topitems)):\n                if topitems[i][0] in filled:\n                    continue\n                if len(filled) == 5:\n                    break\n                out.write(' ' + topitems[i][0])\n                filled.append(topitems[i][0])\n\n        if hotel_country in best_hotel_country:\n            d = best_hotel_country[hotel_country]\n            topitems = nlargest(5, d.items(), key=itemgetter(1))\n            for i in range(len(topitems)):\n                if topitems[i][0] in filled:\n                    continue\n                if len(filled) == 5:\n                    break\n                out.write(' ' + topitems[i][0])\n                filled.append(topitems[i][0])\n\n        for i in range(len(topclasters)):\n            if topclasters[i][0] in filled:\n                continue\n            if len(filled) == 5:\n                break\n            out.write(' ' + topclasters[i][0])\n            filled.append(topclasters[i][0])\n\n        out.write(\"\\n\")\n    out.close()\n    print('Completed!')\n\nrun_solution()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "import pandas as pd\n\ndestinations = pd.read_csv(\"../input/destinations.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\ntrain = pd.read_csv(\"../input/train.csv\")"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "a =4\n"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}