{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c3cb0844-f171-8246-81f9-8623a9b25263"
      },
      "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",
        "import gc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "16860506-6b3d-26eb-8828-be600aa05365"
      },
      "outputs": [],
      "source": [
        "print ('Reading train!')\n",
        "train = pd.read_csv('../input/train.csv',\n",
        "                    usecols=['Agencia_ID',\n",
        "                                  'Ruta_SAK',\n",
        "                                  'Cliente_ID',\n",
        "                                  'Producto_ID',\n",
        "                                  'Demanda_uni_equil'],\n",
        "                    dtype={'Agencia_ID': 'uint16',\n",
        "                                      'Ruta_SAK': 'uint16',\n",
        "                                      'Cliente_ID': 'int32',\n",
        "                                      'Producto_ID': 'uint16',\n",
        "                                      'Demanda_uni_equil': 'float32'})\n",
        "print ('Train read!')\n",
        "print ('Estimating means features')\n",
        "train['Demanda_uni_equil'] = train['Demanda_uni_equil'].apply(lambda x: 1.005*np.log1p(x + 0.01) - 0.005)\n",
        "train['MeanP'] = train.groupby('Producto_ID')['Demanda_uni_equil'].transform(np.mean).astype('float32')\n",
        "MP = train.loc[:, ['Producto_ID','MeanP']].drop_duplicates(subset=['Producto_ID'])\n",
        "test = pd.read_csv('../input/test.csv',\n",
        "                   usecols=['Agencia_ID',\n",
        "                              'Ruta_SAK',\n",
        "                              'Cliente_ID',\n",
        "                              'Producto_ID',\n",
        "                            'id'],\n",
        "                   dtype={'Agencia_ID': 'uint16',\n",
        "                                      'Ruta_SAK': 'uint16',\n",
        "                                      'Cliente_ID': 'int32',\n",
        "                                      'Producto_ID': 'uint16',\n",
        "                                      'id': 'int32'})\n",
        "print ('Test read!')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a912f088-208f-f2d2-d0ec-ac4efbfc3f75"
      },
      "outputs": [],
      "source": [
        "test = test.merge(MP,\n",
        "                  how='left',\n",
        "                  on=['Producto_ID'],\n",
        "                  copy=False)\n",
        "print ('P merged!')\n",
        "test.loc[:, ['id', 'Producto_ID']].to_csv('submission.csv', index=False)"
      ]
    }
  ],
  "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
}