{"metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "version": "3.6.3", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "cells": [{"outputs": [], "metadata": {"_cell_guid": "da7f8412-df8a-4ee5-bab3-a237c4493a75", "collapsed": true, "_uuid": "d400179ed61fa9712f22e8d08380fadc3f3e9a37"}, "cell_type": "code", "execution_count": null, "source": ["# To begin the competition, I tried to submit the average per item and per store.\n", "# I was motivated to do so since this is the top basic benchmark which was posted...\n", "# I used some tricks (e.g. if a item/store wasn't in the train - \n", "#         take the average of the store/item which is already known)\n", "\n", "# I get s score of 1.323.\n", "# Anything simpler got a worse score...\n", "\n", "# How come I can't get close to the posted benchmark (0.726)?\n", "# Isn't this weird?"]}, {"outputs": [], "metadata": {"_cell_guid": "703e0796-a9ab-4acd-9661-015954349852", "collapsed": true, "_uuid": "212dbf11c541d5e88e07ff125a112ac0076e3484"}, "cell_type": "code", "execution_count": null, "source": ["import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import datetime"]}, {"outputs": [], "metadata": {"_cell_guid": "5c487ae5-e7c1-479e-a058-08bc58bba97a", "_uuid": "813e4f12e92c356dfb24d5dd4207f074a425a110"}, "cell_type": "code", "execution_count": null, "source": ["# reading the data and turning negative numbers of unit sales to 0\n", "train_df = pd.read_csv('../input/train.csv')\n", "test_df = pd.read_csv('../input/test.csv')\n", "train_df['unit_sales'] = train_df['unit_sales'].apply(lambda x: max(0,x))"]}, {"outputs": [], "metadata": {"_cell_guid": "2882ee38-543f-41e0-b576-f170f1611246", "collapsed": true, "_uuid": "92ff97c735f089a98ac1496be80c29823a5660fc"}, "cell_type": "code", "execution_count": null, "source": ["# creating dictionaries of 3 types:\n", "# 1. mean by store and item\n", "# 2. mean by item only\n", "# 3. mean by store only\n", "\n", "# also calculating average number of sales per item (into mean_all)\n", "\n", "mean_per_both = train_df[['item_nbr', 'store_nbr', 'unit_sales']]\\\n", "    .groupby(['item_nbr', 'store_nbr'], as_index = False)    .agg('mean')\n", "mean_dict_both = dict(zip(zip(mean_per_both['store_nbr'],mean_per_both['item_nbr']), \\\n", "                          mean_per_both['unit_sales']))\n", "\n", "mean_per_item = train_df[['item_nbr', 'unit_sales']].groupby(['item_nbr'], as_index = False)    .agg('mean')\n", "mean_dict_item = dict(zip(mean_per_item['item_nbr'], mean_per_item['unit_sales']))\n", "\n", "mean_per_store = train_df[['store_nbr', 'unit_sales']].groupby(['store_nbr'], as_index = False)    .agg('mean')\n", "mean_dict_store = dict(zip(mean_per_store['store_nbr'], mean_per_item['unit_sales']))\n", "\n", "mean_all = np.mean(list(mean_dict_item.values()))"]}, {"outputs": [], "metadata": {"_cell_guid": "ef59954f-b504-429b-ba3e-ffb67eab6bfc", "collapsed": true, "_uuid": "a2226393da8a39b206e8669059eafcac4f9a9340"}, "cell_type": "code", "execution_count": null, "source": ["# function which receives an instance and returns the most \"specific\" mean \n", "# which can be calculated... (using dictionaries from previous cell)\n", "\n", "def calc_mean(x):\n", "    try:\n", "        return mean_dict_both[(x['store_nbr'],x['item_nbr'])]\n", "    except:\n", "        try:\n", "            return mean_dict_item[x['item_nbr']]\n", "        except:\n", "            try:\n", "                mean_dict_item[x['store_nbr']]\n", "            except:\n", "                return mean_all"]}, {"outputs": [], "metadata": {"_cell_guid": "b0e0b3b8-d7f7-4deb-aab1-979acbc9d514", "collapsed": true, "_uuid": "f66a1e98b95e767411dd23f3fd13c443d6f9aba5"}, "cell_type": "code", "execution_count": null, "source": ["# creating \"predictions\" using the function from the previous cell\n", "test_df['unit_sales'] = test_df[['store_nbr','item_nbr']].apply(calc_mean, axis=1)"]}, {"outputs": [], "metadata": {"_cell_guid": "10eae1f4-52c5-41e2-90fe-286a9f696bf7", "collapsed": true, "_uuid": "32e4ce51430be51323450d11a95e587f874221f6"}, "cell_type": "code", "execution_count": null, "source": ["# saving to file\n", "today = '_' + str(datetime.datetime.now().day) + '_' + str(datetime.datetime.now().month) + \\\n", "    '_' + str(datetime.datetime.now().year)\n", "test_df[['id', 'unit_sales']].to_csv('mean_by_item_and_store' + today + '.csv', index = False)"]}], "nbformat_minor": 1, "nbformat": 4}