{"nbformat": 4, "nbformat_minor": 1, "metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"pygments_lexer": "ipython3", "file_extension": ".py", "mimetype": "text/x-python", "version": "3.6.3", "codemirror_mode": {"version": 3, "name": "ipython"}, "name": "python", "nbconvert_exporter": "python"}}, "cells": [{"source": ["**This notebook is based on the LGBM Starter by Lee's and it seems that Linear regression model could edge up the decision-tree based models by 0.01\n", "\n", "Intuition is that models with different structure can learn from each other and might improve overall accuracy.**"], "metadata": {}, "cell_type": "markdown"}, {"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\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "execution_count": null, "metadata": {"_uuid": "6d4ce2a88955e1ba5a32546a9bccd3ddf3b2d3dd", "_cell_guid": "b319e009-d390-4920-8609-617acf0d8b7f", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["#OKAY! Best score is 0.600748907457\n", "#OKAY! Best weights are 0.45   0.45   0.1  \n", "\n", "lrSubmitY= pd.read_csv('../input/aggr-dataset/lrSubmitY.csv',index_col=False)\n", "lrSubmitY.drop(['Unnamed: 0'], inplace=True,axis=1)"], "execution_count": null, "metadata": {"_uuid": "790b3628349a7c7cda17d40cd5744689b838b41f", "_cell_guid": "e69dba28-b8cb-4b05-970e-f3a8cbf14ce0", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["lgbSubmitY= pd.read_csv('../input/dataset2/lgbSubmitY.csv',index_col=False)\n", "lgbSubmitY.drop(['Unnamed: 0'], inplace=True,axis=1)"], "execution_count": null, "metadata": {"_uuid": "a451721b10e6eeb178e3f07b899288f1e71dfa29", "_cell_guid": "71bd1efb-8c31-4826-9eda-f1bf38ff3e0b", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["xgbSubmitY= pd.read_csv('../input/aggr-dataset/xgbSubmitY.csv',index_col=False)\n", "xgbSubmitY.drop(['Unnamed: 0'], inplace=True,axis=1)"], "execution_count": null, "metadata": {"_uuid": "adf02f96147df8cf1e9aee66a556cfd37a572b2c", "_cell_guid": "8b0ebba8-589a-41e3-83ef-debab0278dd8", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["combinedPredY = (xgbSubmitY*0.45)+(lgbSubmitY*0.45)+(lrSubmitY*0.10)"], "execution_count": null, "metadata": {"_uuid": "afe1a97e87f8493b9c463f7a6916bfa4763d13d8", "_cell_guid": "a3e28f5a-49d4-40b1-baab-b6f136154498", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["combinedPredY.values"], "execution_count": null, "metadata": {"_uuid": "88f418c093d4d82f759a1e9e65a401a026a0059a", "scrolled": true, "_cell_guid": "8c8f13bb-7667-409a-87b5-eb8607c4a1c9", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["#to reproduce the testing IDs\n", "df_train = pd.read_csv(\n", "    '../input/favorita-grocery-sales-forecasting/train.csv', usecols=[1, 2, 3, 4, 5],\n", "    dtype={'onpromotion': bool},\n", "    converters={'unit_sales': lambda u: np.log1p(\n", "        float(u)) if float(u) > 0 else 0},\n", "    parse_dates=[\"date\"],\n", "    skiprows=range(1, 66458909)  # 2016-01-01\n", ")\n", "\n", "df_2017 = df_train.loc[df_train.date>=pd.datetime(2017,1,1)]\n", "del df_train\n", "\n", "df_2017 = df_2017.set_index(\n", "    [\"store_nbr\", \"item_nbr\", \"date\"])[[\"unit_sales\"]].unstack(\n", "        level=-1).fillna(0)\n", "df_2017.columns = df_2017.columns.get_level_values(1)"], "execution_count": null, "metadata": {"_uuid": "a580a47f1ed32671d0d43a5d2fd4bfbd969e793f", "_cell_guid": "dfb3476d-53de-4406-bdcb-9cec37c73748", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["combinedPredY.shape"], "execution_count": null, "metadata": {"_uuid": "cb90b6a7a065b92655994361d3cb11afbd73ed22", "_cell_guid": "70744c39-9105-456a-a55f-98b037ad1d41", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["print(\"Making submission...\")\n", "#y_test = np.array(test_pred).transpose()\n", "#pd.DataFrame(y_test).to_csv('predY_before_stack.csv')\n", "df_preds = pd.DataFrame(\n", "    combinedPredY.values, index=df_2017.index,\n", "    columns=pd.date_range(\"2017-08-16\", periods=16)\n", ").stack().to_frame(\"unit_sales\")\n", "df_preds.index.set_names([\"store_nbr\", \"item_nbr\", \"date\"], inplace=True)"], "execution_count": null, "metadata": {"_uuid": "a680d683fff99183c3c4ed81c886fe6053c10192", "_cell_guid": "ea5ac5e3-0135-4f31-8336-7a9943b8922c", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["import gc\n", "gc.collect()"], "execution_count": null, "metadata": {"_uuid": "675dbfd6598ab64c3647e8202ab89d9b19452da1", "_cell_guid": "1b0756f1-306b-4e5a-a8d5-2e88999f9005", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["df_test = pd.read_csv(\n", "    \"../input/favorita-grocery-sales-forecasting/test.csv\", usecols=[0, 1, 2, 3, 4],\n", "    dtype={'onpromotion': bool},\n", "    parse_dates=[\"date\"]  # , date_parser=parser\n", ").set_index(\n", "    ['store_nbr', 'item_nbr', 'date']\n", ")"], "execution_count": null, "metadata": {"_uuid": "f87eed108eb2973e883d2d27d2a416cfdc059d19", "_cell_guid": "e1f2b15b-e6ac-4509-a845-1e5b363b79fc", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": ["submission = df_test[[\"id\"]].join(df_preds, how=\"left\").fillna(0)\n", "submission[\"unit_sales\"] = np.clip(np.expm1(submission[\"unit_sales\"]), 0, 1000)\n", "submission.to_csv('aggr.csv', float_format='%.4f', index=None)"], "execution_count": null, "metadata": {"_uuid": "26e916fbad517f74285c09427ff40b6d3bf825d2", "_cell_guid": "bc3c7e4a-18d2-4e7e-a137-67a56a140a8d", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": [], "execution_count": null, "metadata": {"_uuid": "3e77a0b456789e1181a204903bb6281b77515a5e", "_cell_guid": "67cda40d-e6ce-4a2f-881a-5de99c6efb30", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": [], "execution_count": null, "metadata": {"_uuid": "ec6528e7fde1761964837d6ab4c8ab9eee3ad3c3", "_cell_guid": "29abde52-6010-4d71-823b-50ae8d053a77", "collapsed": true}, "cell_type": "code", "outputs": []}, {"source": [], "execution_count": null, "metadata": {"_uuid": "c7a08784ca14811547f69d323fff4248e9df8981", "_cell_guid": "f827a25b-018d-44dc-a559-37dc579f91d6", "collapsed": true}, "cell_type": "code", "outputs": []}]}