{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "052c5e16-a99c-4f2f-b446-d3320aa2ae70"
      },
      "outputs": [],
      "source": [
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "train_data=pd.read_csv(\"../input/train.csv\",usecols=['Producto_ID','Demanda_uni_equil'])\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b2596823-8fce-43f0-b39c-aaa938421345"
      },
      "outputs": [],
      "source": [
        "train_data['log_Dem']=np.log(np.array(train_data['Demanda_uni_equil'].tolist())+1)\n",
        "#print(train_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "af745793-0efe-49d6-8a52-cbabb4ce4914"
      },
      "outputs": [],
      "source": [
        "mean_data=train_data.groupby(train_data['Producto_ID']).mean()\n",
        "print(mean_data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fedba4ed-161e-4298-8609-9dedf72c77d4"
      },
      "outputs": [],
      "source": [
        "test_data=pd.read_csv(\"../input/test.csv\",usecols=['id','Producto_ID'])\n",
        "target=np.zeros(test_data.shape[0])\n",
        "log_target=np.zeros(test_data.shape[0])\n",
        "for pid in mean_data.index:\n",
        "    target[test_data[test_data['Producto_ID']==pid]['id'].values]=mean_data.ix[pid]['Demanda_uni_equil']\n",
        "    log_target[test_data[test_data['Producto_ID']==pid]['id'].values]=mean_data.ix[pid]['log_Dem']\n",
        "#print (log_target)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ef4ba418-0f3e-4b49-82d9-5060cf0c9eb2"
      },
      "outputs": [],
      "source": [
        "test_data['Demanda_uni_equil']=np.exp(log_target)-1\n",
        "print(test_data)\n",
        "test_data.to_csv('result_groupmean_log.csv',index=False,columns=['id','Demanda_uni_equil'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3db17ac5-0869-4461-a923-af9844a4510a"
      },
      "outputs": [],
      "source": [
        "test_data[test_data['Producto_ID']==41]['id']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a2f98d9e-250a-487d-9843-c856f907972f"
      },
      "outputs": [],
      "source": [
        "#mean_data.index\n",
        "#mean_data.ix[41]\n",
        "test_data.shape"
      ]
    }
  ],
  "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
}