{"nbformat_minor":1,"cells":[{"cell_type":"code","metadata":{"collapsed":true},"execution_count":null,"source":"import datetime\nfrom heapq import nlargest\nfrom operator import itemgetter\nimport math\n","outputs":[]},{"cell_type":"code","metadata":{"collapsed":true},"execution_count":null,"source":"def prepare_arrays_match():\n    f = open(\"../input/train.csv\", \"r\")\n    f.readline()\n    \n    best_hotels_od_ulc = dict()\n    best_hotels_uid_miss = dict()\n    best_hotels_search_dest = dict()\n    best_hotels_country = dict()\n    popular_hotel_cluster = dict()\n    best_s00 = dict()\n    best_s01 = dict()\n    total = 0\n\n    # Calc counts\n    while 1:\n        line = f.readline().strip()\n        total += 1\n\n        if total % 2000000 == 0:\n            print('Read {} lines...'.format(total))\n\n        if line == '':\n            break\n\n        arr = line.split(\",\")\n        \n        if arr[11] != '':\n            book_year = int(arr[11][:4])\n            book_month = int(arr[11][5:7])\n        else:\n            book_year = int(arr[0][:4])\n            book_month = int(arr[0][5:7])\n            \n        if book_month<1 or book_month>12 or book_year<2012 or book_year>2015:\n            #print(book_month)\n            #print(book_year)\n            #print(line)\n            continue\n            \n        user_location_city = arr[5]\n        orig_destination_distance = arr[6]\n        user_id = arr[7]\n        is_package = arr[9]\n        srch_destination_id = arr[16]\n        hotel_country = arr[21]\n        hotel_market = arr[22]\n        is_booking = float(arr[18])\n        hotel_cluster = arr[23]\n\n        append_0 = ((book_year - 2012)*12 + (book_month - 12))\n        if not (append_0>0 and append_0<=36):\n            #print(book_year)\n            #print(book_month)\n            print(line)\n            #print(append_0)\n            continue\n        \n        append_1 = pow(math.log(append_0), 1.36) * (-0.1+0.95*pow(append_0, 1.47)) * (3.5*((book_year - 2012)/2) + 22.56*is_booking)\n        append_2 = 3 + 5.56*is_booking\n\n        if user_location_city != '' and orig_destination_distance != '' and user_id !='' and srch_destination_id != '' and hotel_country != '' and is_booking==1:\n            s00 = (user_id, user_location_city, srch_destination_id, hotel_country, hotel_market)\n            if s00 in best_s00:\n                if hotel_cluster in best_s00[s00]:\n                    best_s00[s00][hotel_cluster] += append_0\n                else:\n                    best_s00[s00][hotel_cluster] = append_0\n            else:\n                best_s00[s00] = dict()\n                best_s00[s00][hotel_cluster] = append_0\n\n        if user_location_city != '' and orig_destination_distance != '' and user_id !='' and srch_destination_id != '' and is_booking==1:\n            s01 = (user_id, srch_destination_id, hotel_country, hotel_market)\n            if s01 in best_s01:\n                if hotel_cluster in best_s01[s01]:\n                    best_s01[s01][hotel_cluster] += append_0\n                else:\n                    best_s01[s01][hotel_cluster] = append_0\n            else:\n                best_s01[s01] = dict()\n                best_s01[s01][hotel_cluster] = append_0\n\n\n        if user_location_city != '' and orig_destination_distance == '' and user_id !='' and srch_destination_id != '' and hotel_country != '' and is_booking==1:\n            s0 = (user_id, user_location_city, srch_destination_id, hotel_country, hotel_market)\n            if s0 in best_hotels_uid_miss:\n                if hotel_cluster in best_hotels_uid_miss[s0]:\n                    best_hotels_uid_miss[s0][hotel_cluster] += append_0\n                else:\n                    best_hotels_uid_miss[s0][hotel_cluster] = append_0\n            else:\n                best_hotels_uid_miss[s0] = dict()\n                best_hotels_uid_miss[s0][hotel_cluster] = append_0\n\n        if user_location_city != '' and orig_destination_distance != '':\n            s1 = (user_location_city, orig_destination_distance)\n\n            if s1 in best_hotels_od_ulc:\n                if hotel_cluster in best_hotels_od_ulc[s1]:\n                    best_hotels_od_ulc[s1][hotel_cluster] += append_0\n                else:\n                    best_hotels_od_ulc[s1][hotel_cluster] = append_0\n            else:\n                best_hotels_od_ulc[s1] = dict()\n                best_hotels_od_ulc[s1][hotel_cluster] = append_0\n\n        if srch_destination_id != '' and hotel_country != '' and hotel_market != '':\n            s2 = (srch_destination_id,hotel_country,hotel_market,is_package)\n            if s2 in best_hotels_search_dest:\n                if hotel_cluster in best_hotels_search_dest[s2]:\n                    best_hotels_search_dest[s2][hotel_cluster] += append_1\n                else:\n                    best_hotels_search_dest[s2][hotel_cluster] = append_1\n            else:\n                best_hotels_search_dest[s2] = dict()\n                best_hotels_search_dest[s2][hotel_cluster] = append_1\n\n        if hotel_market != '':\n            s3 = (hotel_market)\n            if s3 in best_hotels_country:\n                if hotel_cluster in best_hotels_country[s3]:\n                    best_hotels_country[s3][hotel_cluster] += append_2\n                else:\n                    best_hotels_country[s3][hotel_cluster] = append_2\n            else:\n                best_hotels_country[s3] = dict()\n                best_hotels_country[s3][hotel_cluster] = append_2\n\n        if hotel_cluster in popular_hotel_cluster:\n            popular_hotel_cluster[hotel_cluster] += append_0\n        else:\n            popular_hotel_cluster[hotel_cluster] = append_0\n\n    f.close()\n    return best_s00,best_s01, best_hotels_country, best_hotels_od_ulc, best_hotels_uid_miss, best_hotels_search_dest, popular_hotel_cluster\n","outputs":[]},{"cell_type":"code","metadata":{},"execution_count":null,"source":"def gen_submission(best_s00, best_s01,best_hotels_country, best_hotels_search_dest, best_hotels_od_ulc, best_hotels_uid_miss, popular_hotel_cluster):\n    now = datetime.datetime.now()\n    path = 'hottel_' + 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    total0 = 0\n    total00 = 0\n    total1 = 0\n    total2 = 0\n    total3 = 0\n    total4 = 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 % 100000 == 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        user_id = arr[8]\n        is_package = arr[10]\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                total1 += 1\n\n        if orig_destination_distance == '':\n            s0 = (user_id, user_location_city, srch_destination_id, hotel_country, hotel_market)\n            if s0 in best_hotels_uid_miss:\n                d = best_hotels_uid_miss[s0]\n                topitems = nlargest(4, 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                    total0 += 1\n\n        s00 = (user_id, user_location_city, srch_destination_id, hotel_country, hotel_market)\n        s01 = (user_id, srch_destination_id, hotel_country, hotel_market)\n        if s01 in best_s01 and s00 not in best_s00:\n            d = best_s01[s01]\n            topitems = nlargest(4, 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                total00 += 1\n\n\n        s2 = (srch_destination_id,hotel_country,hotel_market,is_package)\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                total2 += 1\n\n        s3 = (hotel_market)\n        if s3 in best_hotels_country:\n            d = best_hotels_country[s3]\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                total3 += 1\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            total4 += 1\n\n        out.write(\"\\n\")\n    out.close()\n    print('Total 1: {} ...'.format(total1))\n    print('Total 0: {} ...'.format(total0))\n    print('Total 00: {} ...'.format(total00))\n    print('Total 2: {} ...'.format(total2))\n    print('Total 3: {} ...'.format(total3))\n    print('Total 4: {} ...'.format(total4))\n\nbest_s00,best_s01,best_hotels_country, best_hotels_od_ulc, best_hotels_uid_miss, best_hotels_search_dest, popular_hotel_cluster = prepare_arrays_match()\ngen_submission(best_s00, best_s01,best_hotels_country, best_hotels_search_dest, best_hotels_od_ulc, best_hotels_uid_miss, popular_hotel_cluster)\n","outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python [Root]","language":"python","name":"Python [Root]"},"language_info":{"file_extension":".py","codemirror_mode":{"version":3,"name":"ipython"},"mimetype":"text/x-python","nbconvert_exporter":"python","version":"3.5.2","name":"python","pygments_lexer":"ipython3"}},"nbformat":4}