{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7c7d3d54-0d66-6376-ba54-883572900f9f"
      },
      "outputs": [],
      "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",
        "\n",
        "import numpy as np # linear algebra\n",
        "import 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",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../working\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "291cd346-4d25-7a50-4897-c229c19d1279"
      },
      "outputs": [],
      "source": [
        "doc_ad_df = pd.read_csv('../input/clicks_train.csv', nrows=5)\n",
        "doc_ad_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a2b2daa-444e-cfe0-6a2d-1aa2289fec4a"
      },
      "outputs": [],
      "source": [
        "doc_en_df = pd.read_csv('../input/documents_entities.csv', nrows=5)\n",
        "doc_en_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ae14a48a-563f-bd0a-5d49-cf148208da05"
      },
      "outputs": [],
      "source": [
        "doc_meta_df = pd.read_csv('../input/documents_meta.csv', nrows=5)\n",
        "doc_meta_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "312d07e4-723f-2bd1-0d72-e0ebaec3f8b2"
      },
      "outputs": [],
      "source": [
        "doc_cat_df = pd.read_csv('../input/documents_categories.csv', nrows=5)\n",
        "doc_cat_df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "55ef3df7-85fc-efb8-8872-2fff22975503"
      },
      "outputs": [],
      "source": [
        "xtrain = pd.read_csv('../input/clicks_train.csv')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0fbf3d2e-d500-192c-4799-3a6db6771b7c"
      },
      "outputs": [],
      "source": [
        "xtrain = xtrain.ix[xtrain.clicked == 1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9ae95527-25e8-73dc-da79-353a9d3f37b7"
      },
      "outputs": [],
      "source": [
        "xtrain.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e468d556-5f27-5580-e344-a46d76396dfd"
      },
      "outputs": [],
      "source": [
        "freq_table = xtrain.ad_id.value_counts()\n",
        "print(freq_table.head())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d585afe9-2bfe-e822-2900-af7e4a31ea76"
      },
      "outputs": [],
      "source": [
        "xtest = pd.read_csv('../input/clicks_test.csv')\n",
        "xtest['count'] = xtest['ad_id'].map(freq_table)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7ddc8daf-2523-cd4d-70ad-7c13d206c834"
      },
      "outputs": [],
      "source": [
        "xtest.sort_values(by=['display_id', 'count'], inplace = True, ascending = False) "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fa1d69ee-b5f6-f283-5e10-5fa6d49f40ca"
      },
      "outputs": [],
      "source": [
        "xsub = xtest.groupby('display_id').aggregate(lambda x: ' '.join([str(ff) for ff in x]))\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7009db64-d968-4333-df03-92b23c7eed63"
      },
      "outputs": [],
      "source": [
        "xsub.to_csv('../input/mbc_sub01.csv', index = True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a8323571-c3f3-ea57-e37b-8117f7298942"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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
}