{"nbformat_minor": 1, "nbformat": 4, "metadata": {"language_info": {"version": "3.6.3", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "cells": [{"metadata": {"_uuid": "f32d3855f0cd27f5f00490aae699eb94ca49c368", "collapsed": true, "_cell_guid": "6d7b371a-0516-4732-83a2-001dc0243925"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["import numpy as np\n", "import pandas as pd\n", "import random\n", "from sklearn import preprocessing, metrics\n", "import gc; gc.enable()\n", "import random\n", "\n", "from sklearn.neural_network import MLPRegressor\n", "\n", "from sklearn.ensemble import BaggingRegressor\n", "from sklearn.tree import DecisionTreeRegressor\n", "from sklearn.ensemble import RandomForestRegressor\n", "\n", "np.random.seed(100)"]}, {"metadata": {"_uuid": "13eeca70e61261bb31bf1b0de09f6788fe7bd9ef", "_cell_guid": "d395289b-9a18-483c-80e9-616998f5b79b"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["print('Three models: \\n1. Neural network \\n2. Bagging\\n3. Random Forest')"]}, {"metadata": {"_uuid": "5e665efd3bd37c4aab945b7f045f91928881c961", "collapsed": true, "_cell_guid": "692e9f32-328f-4cf4-a6c6-16499c7dd1a1"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["# read datasets\n", "datatypes = {'id':'int64', 'item_nbr':'int32', 'store_nbr':'int8', 'onpromotion':str}\n", "data = {\n", "    'traind': pd.read_csv('../input/train.csv', dtype=datatypes, parse_dates=['date']),\n", "    'testd': pd.read_csv('../input/test.csv', dtype=datatypes, parse_dates=['date']),\n", "    'itemsd': pd.read_csv('../input/items.csv'),\n", "    'storesd': pd.read_csv('../input/stores.csv'),\n", "    'transactionsd': pd.read_csv('../input/transactions.csv', parse_dates=['date']),\n", "    'holidaysd': pd.read_csv('../input/holidays_events.csv', dtype={'transferred':str}, parse_dates=['date']),\n", "    'oild': pd.read_csv('../input/oil.csv', parse_dates=['date']),\n", "    }\n"]}, {"metadata": {"_uuid": "ba15cccd9309a9c53a13ad852da7a4676ce09dff", "_cell_guid": "a7c61000-dcc7-45ac-8193-6a6034ea2d04"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["# dataset preprocessing\n", "print('Datasets pre-processing')\n", "\n", "train = data['traind'][(data['traind']['date'].dt.month == 8) & (data['traind']['date'].dt.day > 15)]\n", "del data['traind']; gc.collect();\n", "target = train['unit_sales'].values\n", "target[target < 0.] = 0.\n", "train['unit_sales'] = np.log1p(target)\n", "\n", "def label_encoding(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"]}, {"metadata": {"_uuid": "3715ef22b610e43c6eff60b1829b92ae89063040", "collapsed": true, "_cell_guid": "93573f4f-d9e4-48d4-9cfd-9384555d921c"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["def transform_dataframe(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['date'] = df['date'].dt.dayofweek\n", "    df['onpromotion'] = df['onpromotion'].map({'False': 0, 'True': 1})\n", "    df['perishable'] = df['perishable'].map({0:1.0, 1:1.25})\n", "    df = df.fillna(-1)\n", "    return df"]}, {"metadata": {"_uuid": "64021bc85f381b6cdd5af84224c2189575ec71db", "_cell_guid": "960dadf8-a32f-4ad2-ace3-d68c226ac286"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["data['itemsd'] = label_encoding(data['itemsd'])\n", "train = pd.merge(train, data['itemsd'], how='left', on=['item_nbr'])\n", "test = pd.merge(data['testd'], data['itemsd'], how='left', on=['item_nbr'])\n", "del data['testd']; gc.collect();\n", "del data['itemsd']; gc.collect();"]}, {"metadata": {"_uuid": "1153f41a9c09815222f041f422797bf9e6d7cc61", "collapsed": true, "_cell_guid": "79ef89db-b483-4063-a227-1f8013ea691d"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["train = pd.merge(train, data['transactionsd'], how='left', on=['date','store_nbr'])\n", "test = pd.merge(test, data['transactionsd'], how='left', on=['date','store_nbr'])\n", "del data['transactionsd']; gc.collect();\n", "target = train['transactions'].values\n", "target[target < 0.] = 0.\n", "train['transactions'] = np.log1p(target)"]}, {"metadata": {"_uuid": "a09cdf2007548b1378b2e4045c77342cf4727bf3", "_cell_guid": "a2ece6d6-c0e9-40ee-8ac5-36138b107650"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["data['storesd'] = label_encoding(data['storesd'])\n", "train = pd.merge(train, data['storesd'], how='left', on=['store_nbr'])\n", "test = pd.merge(test, data['storesd'], how='left', on=['store_nbr'])\n", "del data['storesd']; gc.collect();\n", "\n", "data['holidaysd'] = data['holidaysd'][data['holidaysd']['locale'] == 'National'][['date','transferred']]\n", "data['holidaysd']['transferred'] = data['holidaysd']['transferred'].map({'False': 0, 'True': 1})\n", "train = pd.merge(train, data['holidaysd'], how='left', on=['date'])\n", "test = pd.merge(test, data['holidaysd'], how='left', on=['date'])\n", "del data['holidaysd']; gc.collect();"]}, {"metadata": {"_uuid": "42265456a48b7cf6dddf6108ebe46542d775ade0", "collapsed": true, "_cell_guid": "ee361c29-a74f-45c6-a5c8-1f5882af5c0b"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["train = pd.merge(train, data['oild'], how='left', on=['date'])\n", "test = pd.merge(test, data['oild'], how='left', on=['date'])\n", "del data['oild']; gc.collect();\n", "\n", "train = transform_dataframe(train)\n", "test = transform_dataframe(test)\n", "col = [c for c in train if c not in ['id', 'unit_sales','perishable','transactions']]"]}, {"metadata": {"_uuid": "11d4c20cb7b7d88abefce28547fd281c9f581ab2", "collapsed": true, "_cell_guid": "6f7c554d-e48d-43b1-86b4-7804c01bee5e"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["x1 = train[(train['yea'] != 2016)]\n", "x2 = train[(train['yea'] == 2016)]\n", "del train; gc.collect();\n", "\n", "y1 = x1['transactions'].values\n", "y2 = x2['transactions'].values"]}, {"metadata": {"_uuid": "5d91444e6599e17cfa93deab07e7369395d5100c", "collapsed": true, "_cell_guid": "8cd30d6f-733e-4026-88ec-2bf8cbb6ca97"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["def NWRMSLE(y, pred, w):\n", "    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5"]}, {"metadata": {"_uuid": "dfe453601d3bf2986a47d986dea1a00eb7d44786", "_cell_guid": "3bd4bb26-4221-41bf-b59b-eece87e127c3"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": ["\n", "print('\\nRunning the models')    \n", "\n", "no_of_models = 3\n", "\n", "for model in range(1, no_of_models + 1):\n", "    \n", "    \n", "    random_state1 = round(model + 515 * model + 56 * model) \n", "    np.random.seed(random_state1)\n", "    \n", "    print('\\nmodel = ', model)\n", "    \n", "    if (model == 1):\n", "        print('Neural Network')\n", "        model_name = 'Neural Network'    \n", "        clf = MLPRegressor(hidden_layer_sizes=(4,), max_iter=30) \n", "    \n", "    if (model == 2):\n", "        print('Bagging Regressor')\n", "        model_name = 'BaggingRegressor'\n", "        clf = BaggingRegressor(\n", "                DecisionTreeRegressor(\n", "                        max_depth=6,\n", "                        max_features=0.85))  \n", "        \n", "    if (model == 3):\n", "        print('Random forest')\n", "        model_name = 'RandomForest'\n", "        clf = RandomForestRegressor(n_estimators=70, max_depth = 3, n_jobs = -1, \n", "                                   random_state=random_state1, verbose=0, warm_start=True)\n", "                                          \n", "    clf.fit(x1[col], y1)\n", "\n", "\n", "    random1 = NWRMSLE(y2, clf.predict(x2[col]), x2['perishable'])\n", "    \n", "    \n", "    test['transactions'] = clf.predict(test[col])\n", "    test['transactions'] = test['transactions'].clip(lower = 0.+1e-12)\n", "\n", "    col = [c for c in x1 if c not in ['id', 'unit_sales','perishable']]\n", "    y1 = x1['unit_sales'].values\n", "    y2 = x2['unit_sales'].values\n", "    \n", "    random_state2 = round(model + 331 * model + 561 * model) \n", "    np.random.seed(random_state2)\n", "\n", "    if (model == 1):\n", "        clf = MLPRegressor(hidden_layer_sizes = (4,), max_iter = 30)\n", "\n", "    if (model == 2):\n", "        clf = BaggingRegressor(DecisionTreeRegressor(max_depth = 5,max_features = 0.85))            \n", "\n", "    if (model == 3):\n", "        clf = RandomForestRegressor(n_estimators = 70, max_depth = 3, n_jobs = -1, \n", "                                   random_state=random_state2, verbose=0, warm_start=True)\n", "        \n", "    clf.fit(x1[col], y1)\n", "    \n", "    random2 = NWRMSLE(y2, clf.predict(x2[col]), x2['perishable'])\n", "   \n", "    print('Performance: NWRMSLE(1) = ',random1,'NWRMSLE(2) = ',random2)\n", "\n", "    test['unit_sales'] = clf.predict(test[col])\n", "    cut = 0. + 1e-12 \n", "    \n", "    test['unit_sales'] = (np.exp(test['unit_sales']) - 1).clip(lower = cut)\n", "\n", "\n", "    output_file = 'submission ' + str(model_name) + '.csv'\n", " \n", "    test[['id','unit_sales']].to_csv(output_file, index=False, float_format='%.2f')\n", "\n", "        \n", "        \n", "        "]}, {"metadata": {"_uuid": "ca4075f4907768bb0c808c350c35828c96cb329b", "collapsed": true, "_cell_guid": "23e1d6b4-b541-4727-bec8-1d9ef90c1260"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": []}, {"metadata": {"_uuid": "0f0b0fd77d487580987d59723a2982b71ad8a29a", "collapsed": true, "_cell_guid": "b2d793e4-f839-44f6-b307-b3eb1cc780e3"}, "execution_count": null, "outputs": [], "cell_type": "code", "source": []}]}