{"metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"name": "python", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "nbconvert_exporter": "python", "version": "3.6.3", "mimetype": "text/x-python", "file_extension": ".py"}}, "nbformat": 4, "cells": [{"metadata": {"_cell_guid": "1d879c04-699a-40d1-b44e-748b6cbf48ff", "_uuid": "3929889ab49200259badfd41c38138dcfeaea97d"}, "execution_count": null, "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; gc.enable()\n", "from sklearn import preprocessing, linear_model, metrics\n", "import matplotlib.pyplot as plt\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."], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "a7ff9ed9-66ce-4c36-9f5d-f0906ad44cfb", "collapsed": true, "_uuid": "879f2c2b08cdb8cc7c03efc3809f5eea58ebf436"}, "execution_count": null, "source": ["dtypes = {'id':'int64', 'item_nbr':'int32', 'store_nbr':'int8', 'onpromotion':str}\n", "data = {\n", "    'tra': pd.read_csv('../input/train.csv', dtype=dtypes, parse_dates=['date']),\n", "    'tes': pd.read_csv('../input/test.csv', dtype=dtypes, parse_dates=['date']),\n", "    'ite': pd.read_csv('../input/items.csv'),\n", "    'sto': pd.read_csv('../input/stores.csv'),\n", "    'trn': pd.read_csv('../input/transactions.csv', parse_dates=['date']),\n", "    'hol': pd.read_csv('../input/holidays_events.csv', dtype={'transferred':str}, parse_dates=['date']),\n", "    'oil': pd.read_csv('../input/oil.csv', parse_dates=['date']),\n", "    }"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "bf3abe9e-11c4-4b39-9f17-43c83f7ec890", "collapsed": true, "_uuid": "c07f5ca970cd28bce57584ed9e7d477ba99c90a7"}, "execution_count": null, "source": ["train = data['tra'][(data['tra']['date'].dt.month == 8) & (data['tra']['date'].dt.day > 15)]\n", "test = data['tes'][(data['tes']['date'].dt.month == 8) & (data['tes']['date'].dt.day > 15)]"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "1035be43-5d1d-44fc-b50e-b8400375d3ea", "collapsed": true, "_uuid": "95108c6a59bc03545f1fa8545d150e620c4093d5"}, "execution_count": null, "source": ["#train[\"item_nbr\"].value_counts(sort = True,ascending=False).nlargest(5)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "71f2d78f-fc33-411c-a8e4-fdcf5fcdd03a", "collapsed": true, "_uuid": "81b968807cbef7162be8c6cb2d94c8d0ba4bf33b"}, "execution_count": null, "source": ["#strain_502331= train[(train[\"item_nbr\"] == 502331)]\n", "#= test[(test[\"item_nbr\"] == 502331)]"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "36a19453-9a7d-45d0-9fd3-7d5e25b40c7d", "_uuid": "d04c56ba7749ccbbefb32bdd3be78be93e7026f3"}, "execution_count": null, "source": ["strain = train #strain_502331\n", "stest = test #stest_502331\n", "print(strain.shape,stest.shape)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "24340f8a-f944-4556-a612-36b58ed91170", "_uuid": "d639c151a598662fe4afee39ebbdb0c42a3bf01f"}, "execution_count": null, "source": ["target = strain['unit_sales'].values\n", "target[target < 0.] = 0.\n", "strain['unit_sales'] = target"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "70194033-db7e-480c-9f1a-c50d2c04c037", "collapsed": true, "_uuid": "28e7a2b0842f6905b336d6865c6b301b63fe3292"}, "execution_count": null, "source": ["strain = pd.merge(strain, data['ite'], how='left', on=['item_nbr'])\n", "strain = pd.merge(strain, data['sto'], how='left', on=['store_nbr'])\n", "data_h_1 = data['hol'][data['hol']['locale'] == 'National'][['date','transferred']]\n", "data_h_1['transferred'] = data_h_1['transferred'].map({'False': 0, 'True': 1})\n", "strain = pd.merge(strain, data_h_1, how='left', on=['date'])\n", "strain = pd.merge(strain, data['oil'], how='left', on=['date'])\n", "\n", "stest = pd.merge(stest, data['ite'], how='left', on=['item_nbr'])\n", "stest = pd.merge(stest, data['sto'], how='left', on=['store_nbr'])\n", "data_h_t = data['hol'][data['hol']['locale'] == 'National'][['date','transferred']]\n", "data_h_t['transferred'] = data_h_t['transferred'].map({'False': 0, 'True': 1})\n", "stest = pd.merge(stest, data_h_t, how='left', on=['date'])\n", "stest = pd.merge(stest, data['oil'], how='left', on=['date'])"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "eccded42-c066-4bcf-9e5e-d34fb433bf86", "collapsed": true, "_uuid": "56d01a48be91333df680018acf4ce9828210295f"}, "execution_count": null, "source": ["from sklearn import preprocessing\n", "def df_transform(df):\n", "    df['date'] = pd.to_datetime(df['date'])\n", "    df['yea'] = df['date'].dt.year\n", "    df['mon'] = df['date'].dt.month\n", "    df['day'] = df['date'].dt.day\n", "    df['dayofweek'] = df['date'].dt.dayofweek\n", "    df['onpromotion'] = df['onpromotion'].map({'False': 1, 'True': 2})\n", "    df['perishable'] = df['perishable'].map({0:1.0, 1:1.25})\n", "    df = df.fillna(0)\n", "    return df\n", "def df_lbl_enc(df):\n", "    for c in df.columns:\n", "        if df[c].dtype == 'object':\n", "            lbl = preprocessing.LabelEncoder()\n", "            df[c] = lbl.fit_transform(df[c])\n", "            print(c)\n", "    return df\n"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "99a9eed1-522a-4458-bf9e-433d2790cc97", "_uuid": "acd2b002b4bb5a6b0ccd5eb0bb21db4658ba2262"}, "execution_count": null, "source": ["strain_t = df_transform(strain)\n", "strain_t_e = df_lbl_enc(strain_t)\n", "\n", "stest_t = df_transform(stest)\n", "stest_t_e = df_lbl_enc(stest_t)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "e89df832-8bbf-4f5c-b4b2-1c699827b537", "collapsed": true, "_uuid": "43b17eecd32ae0a349db9aa571de01b791dbf480"}, "execution_count": null, "source": ["strain_t_e_dateIndex = strain_t_e.set_index('date')\n", "stest_t_e_dateIndex = stest_t_e.set_index('date')\n", "\n", "col =[c for c in strain_t_e_dateIndex if c not in ['id','item_nbr','mon','class','city','cluster','unit_sales']]\n", "train_features = strain_t_e_dateIndex[col]\n", "target = np.log1p(strain_t_e_dateIndex[['unit_sales']])\n", "\n", "col =[c for c in stest_t_e_dateIndex if c not in ['id','item_nbr','mon','class','city','cluster']]\n", "features = stest_t_e_dateIndex[col]"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "a2044fb0-1e3d-4a39-a1b4-1c0dc83ca138", "collapsed": true, "_uuid": "e384e0b1ad15a1355033885228daed7eea60fbbd"}, "execution_count": null, "source": ["from sklearn.model_selection import train_test_split\n", "\n", "\n", "#X_train = train_features['2015':'2016']\n", "#X_test = train_features['2013']\n", "#y_train = target['2014':'2017']\n", "#y_test = target['2013']\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(train_features, target, test_size=0.20, random_state=42)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "d5f8dd20-9f37-4923-9e20-950f881f7d5e", "collapsed": true, "_uuid": "015032ab61bbd6680f33f1572bdf4b6e6da61821"}, "execution_count": null, "source": ["W_train = X_train['perishable']#.map({0:1.0, 1:1.25})\n", "W_test = X_test['perishable']#.map({0:1.0, 1:1.25})\n", "#W_train = W_train.fillna(0)\n", "#W_test = W_test.fillna(0)"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "c2f01f06-e767-49fc-be61-780b00a2fa19", "collapsed": true, "_uuid": "5132c016fe2c71a9230d72ffc6f3c88b46793e34"}, "execution_count": null, "source": ["from sklearn import metrics\n", "def NWRMSLE(y, pred, w):\n", "    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "a102393f-02d2-4c0c-acd3-f81346388c3c", "_uuid": "9a5acd15fa32e249490f47c842cf1909960d448e"}, "execution_count": null, "source": ["from sklearn.tree import DecisionTreeRegressor\n", "from sklearn.ensemble import RandomForestRegressor\n", "from sklearn import cross_validation\n", "\n", "rf = RandomForestRegressor(max_features = \"auto\", random_state =50 )\n", "\n", "rf.fit(X_train, y_train)\n", "print ('RF accuracy: TRAINING', rf.score(X_train,y_train,W_train))\n", "print ('RF accuracy: TESTING', rf.score(X_test,y_test,W_test))\n", "print(\"feature Importance\",rf.feature_importances_)\n", "yhat1 = rf.predict(X_test)\n", "print('NWRMSLE RF',NWRMSLE((y_test),(yhat1),W_test.values ))"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "29bc3aa1-9578-48c7-9b94-3ef97f935b5c", "collapsed": true, "_uuid": "ef658d7bb222ec525e2ede9d2fdd3c5454f14693"}, "execution_count": null, "source": ["test.head()"], "cell_type": "code", "outputs": []}, {"metadata": {"_cell_guid": "44fc63b5-1139-4c0e-a1c6-9162dda9fa1b", "collapsed": true, "_uuid": "bb0072c4f683b32a205f6e9ed175df58559bc648"}, "execution_count": null, "source": ["#y_test['pred']=yhat1\n", "#plt.plot(y_test['unit_sales'])\n", "#plt.plot(y_test['pred'])\n", "#plt.show()\n", "#Submission\n", "test['unit_sales'] = rf.predict(features[col])\n", "test[['id','unit_sales']].to_csv(\"submission_rf.csv\", index=False, float_format='%.2f')"], "cell_type": "code", "outputs": []}], "nbformat_minor": 1}