{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.5.2"}},"nbformat":4,"nbformat_minor":0,"cells":[{"metadata":{"_cell_guid":"a725d3bc-6800-e76c-be43-1d20d443c8db","_active":false,"collapsed":false},"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.\n\n","execution_count":1,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"c1df3b62-039b-e28a-6e6d-41fa6eafc1f3","_active":false,"collapsed":false},"source":"train = pd.read_csv(\"../input/clicks_train.csv\")\n\nids = train.display_id.unique()\nids = np.random.choice(ids, size=len(ids)//10, replace=False)\n\nvalid = train[train.display_id.isin(ids)]\ntrain = train[~train.display_id.isin(ids)]\n\t\nprint (valid.shape, train.shape)","execution_count":2,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"38699265-96cc-b962-4404-22d9fd6094c8","_active":true,"collapsed":false},"source":"cnt = train[train.clicked==1].ad_id.value_counts()\ncntall = train.ad_id.value_counts()\n\ndef get_prob(k):\n    if k not in cnt:\n        return 0\n    return cnt[k]/(float(cntall[k]) + 10)\n","execution_count":3,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"f1e39f46-9eb7-a341-a377-e58e88884e87","_active":false,"collapsed":false},"source":"train.head()\n","execution_count":4,"cell_type":"code","outputs":[],"execution_state":"idle"},{"metadata":{"_cell_guid":"cc185be2-61c1-ba26-9b10-a5c12400b4ad","_active":false,"collapsed":false},"source":"train['base_prob'] = train.apply(lambda row: get_prob(row['ad_id']),axis=1)","execution_count":7,"cell_type":"code","outputs":[],"execution_state":"busy"},{"metadata":{"_cell_guid":"07019daf-42c3-920b-dc1f-2ae9a82591af","_active":false,"collapsed":false},"source":"cnt_set = set(cnt)","execution_count":null,"cell_type":"code","outputs":[],"execution_state":"busy"},{"metadata":{"_cell_guid":"890ea6d0-4862-6634-5292-34ff44804de2","_active":false,"collapsed":false},"source":"valid['base_prob'] = valid.apply(lambda row: get_prob(row['ad_id']),axis=1)","execution_count":null,"cell_type":"code","outputs":[],"execution_state":"busy"},{"metadata":{"_cell_guid":"cfb1dbd6-5f7d-3a94-209d-5229faa2368a","_active":false,"collapsed":false},"source":null,"execution_count":null,"cell_type":"code","outputs":[]}]}