{"nbformat_minor":0,"cells":[{"execution_count":null,"source":"learning \ndune_dwellerPredict hotel type with pandas\n","outputs":[],"cell_type":"markdown","metadata":{"_uuid":"9a78eaee0c992ca20a34870e2e470619f1fa4215","_execution_state":"idle","collapsed":false}},{"execution_count":null,"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","outputs":[],"cell_type":"code","metadata":{"_uuid":"966020dc4c50519c341be41d22f8c5e26ea2c6ba","_execution_state":"idle"}},{"execution_count":null,"source":"train = pd.read_csv('../input/train.csv',\n                    dtype={'is_booking':bool,'srch_destination_id':np.int32, 'hotel_cluster':np.int32},\n                    usecols=['srch_destination_id','is_booking','hotel_cluster'],\n                    chunksize=1000000)\naggs = []\nprint('-'*38)\nfor chunk in train:\n    agg = chunk.groupby(['srch_destination_id',\n                         'hotel_cluster'])['is_booking'].agg(['sum','count'])\n    agg.reset_index(inplace=True)\n    aggs.append(agg)\n    print('.',end='')\nprint('')\naggs = pd.concat(aggs, axis=0)\naggs.head(10)","outputs":[],"cell_type":"code","metadata":{"_uuid":"1bbf174bf3c16b3b7c92ad985ab0a96cd9b0ff27","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"aggs.shape","outputs":[],"cell_type":"code","metadata":{"_uuid":"321408813fd3ee1da3ac57a166ae496d9b7b52ec","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"agg = aggs.groupby(['srch_destination_id','hotel_cluster']).sum().reset_index()\nagg.head()","outputs":[],"cell_type":"code","metadata":{"_uuid":"100fb362744b94d81f9140709f86780130bc02d6","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"CLICK_WEIGHT = 0.05\nagg = aggs.groupby(['srch_destination_id','hotel_cluster']).sum().reset_index()\nagg['count'] -= agg['sum']\nagg = agg.rename(columns={'sum':'bookings','count':'clicks'})\nagg['relevance'] = agg['bookings'] + CLICK_WEIGHT * agg['clicks']\nagg.head()","outputs":[],"cell_type":"code","metadata":{"_uuid":"96968206a62e734761a29bccd5d9f71fa78870a9","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"def most_popular(group, n_max=5):\n    relevance = group['relevance'].values\n    hotel_cluster = group['hotel_cluster'].values\n    most_popular = hotel_cluster[np.argsort(relevance)[::-1]][:n_max]\n    return np.array_str(most_popular)[1:-1] # remove square brackets","outputs":[],"cell_type":"code","metadata":{"_uuid":"c1670517d9dd460d7d4486da31e2e91cfd16b23a","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"relevance = agg['relevance'].values\nhotel_cluster = agg['hotel_cluster'].values\nindx = np.argsort(relevance)[::-1]\nhotel_cluster[indx][:5]\n","outputs":[],"cell_type":"code","metadata":{"_uuid":"2e2f750781e974812674683280542ebbdbb03010","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"most_pop = agg.groupby(['srch_destination_id']).apply(most_popular)\nmost_pop = pd.DataFrame(most_pop).rename(columns={0:'hotel_cluster'})\nmost_pop.head()","outputs":[],"cell_type":"code","metadata":{"_uuid":"4d6110599b59e413d33af3a27580b1121636fef7","_execution_state":"idle","collapsed":false}},{"execution_count":null,"source":"","outputs":[],"cell_type":"code","metadata":{"_uuid":"77bc1795860b8c6f2bb189155470a9223407fe6b","_execution_state":"idle","collapsed":false}}],"nbformat":4,"metadata":{"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","mimetype":"text/x-python","file_extension":".py","name":"python","version":"3.6.0","codemirror_mode":{"version":3,"name":"ipython"}},"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"}}}