{"cells": [{"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", "#Import necessary packages for data analysis\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from sklearn import preprocessing, linear_model, metrics\n", "import gc; gc.enable()\n", "import seaborn as sns\n", "sns.set(style = 'whitegrid', color_codes = True)\n", "%matplotlib inline\n", "#Machine Learning Algorithms\n", "from sklearn.ensemble import RandomForestRegressor\n", "from sklearn.ensemble import GradientBoostingRegressor\n", "from sklearn.linear_model import LinearRegression\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": "ec08dddc-ef40-4b17-a7af-2d164869cf55", "collapsed": true, "_uuid": "0a9971f1993ad246f8f541647415fd8d88c01d20"}, "execution_count": null}, {"source": ["dtypes = {'id':'int64', 'item_nbr':'int32', 'store_nbr':'int8', 'onpromotion':str}\n", "data = {\n", "    'train_1': pd.read_csv('../input/train.csv', dtype=dtypes, parse_dates=['date']),\n", "    'test': pd.read_csv('../input/test.csv', dtype=dtypes, parse_dates=['date']),\n", "    'items': pd.read_csv('../input/items.csv'),\n", "    'stores': pd.read_csv('../input/stores.csv'),\n", "    'trans': pd.read_csv('../input/transactions.csv', parse_dates=['date']),\n", "    'holidays': 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": "beff157f-2518-48a9-a343-1a743a7e9a27", "collapsed": true, "_uuid": "0f23344f1c1dca0275677b0c87e7ed8bbc2cb6a7"}, "execution_count": null}, {"source": ["train = data['train_1'][(data['train_1']['date'].dt.month == 8) & (data['train_1']['date'].dt.day > 15)]\n", "del data['train_1']; gc.collect();\n", "target = train['unit_sales'].values\n", "target[target < 0.] = 0.\n", "train['unit_sales'] = np.log1p(target)\n", "\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", "\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['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\n", "#Items data\n", "data['items'] = df_lbl_enc(data['items'])\n", "train = pd.merge(train, data['items'], how='left', on=['item_nbr'])\n", "test = pd.merge(data['test'], data['items'], how='left', on=['item_nbr'])\n", "del data['test']; gc.collect();\n", "del data['items']; gc.collect();\n", "#Transactions data\n", "train = pd.merge(train, data['trans'], how='left', on=['date','store_nbr'])\n", "test = pd.merge(test, data['trans'], how='left', on=['date','store_nbr'])\n", "del data['trans']; gc.collect();\n", "target = train['transactions'].values\n", "target[target < 0.] = 0.00015\n", "train['transactions'] = np.log1p(target)\n", "#Stores data\n", "data['stores'] = df_lbl_enc(data['stores'])\n", "train = pd.merge(train, data['stores'], how='left', on=['store_nbr'])\n", "test = pd.merge(test, data['stores'], how='left', on=['store_nbr'])\n", "del data['stores']; gc.collect();\n", "#Holidays data\n", "data['holidays'] = data['holidays'][data['holidays']['locale'] == 'National'][['date','transferred']]\n", "data['holidays']['transferred'] = data['holidays']['transferred'].map({'False': 0, 'True': 1})\n", "train = pd.merge(train, data['holidays'], how='left', on=['date'])\n", "test = pd.merge(test, data['holidays'], how='left', on=['date'])\n", "del data['holidays']; gc.collect();\n", "#Oil data\n", "train = pd.merge(train, data['oil'], how='left', on=['date'])\n", "test = pd.merge(test, data['oil'], how='left', on=['date'])\n", "del data['oil']; gc.collect();\n", "#Join data\n", "train = df_transform(train)\n", "test = df_transform(test)\n", "col = [c for c in train if c not in ['id', 'unit_sales','perishable','transactions']]\n", "x1 = train[(train['yea'] != 2016)]\n", "x2 = train[(train['yea'] == 2016)]\n", "del train; gc.collect();\n", "y1 = x1['transactions'].values\n", "y2 = x2['transactions'].values"], "cell_type": "code", "outputs": [], "metadata": {"_cell_guid": "d77b2902-b8ee-4b1b-ae2b-85d20cde473a", "collapsed": true, "_uuid": "995cd5350ef1e49d9c0c3b3fee65f04a9f1bc370"}, "execution_count": null}, {"source": ["def NWRMSLE(y, pred, w):\n", "    return metrics.mean_squared_error(y, pred, sample_weight=w)**0.5\n", "\n", "col = [c for c in x1 if c not in ['id', 'unit_sales','city','cluster','perishable']]\n", "y1 = x1['unit_sales'].values\n", "y2 = x2['unit_sales'].values\n", "\n", "r3 = RandomForestRegressor(n_estimators=79, max_depth=5, n_jobs=-1, \n", "                                    verbose=0, warm_start=True)\n", "\n", "r4 = GradientBoostingRegressor(n_estimators=120, max_depth=3, learning_rate = 0.05, \n", "                                       verbose=0, warm_start=True,\n", "                                       subsample= 0.65, max_features = 0.35)\n", "#Fit a random forest classifier to our training set\n", "r3.fit(x1[col], y1)\n", "r4.fit(x1[col], y1)\n", "a3 = NWRMSLE(y1, r3.predict(x1[col]), x1['perishable'])\n", "a4 = NWRMSLE(y1, r4.predict(x1[col]), x1['perishable'])\n", "print('model fit')"], "cell_type": "code", "outputs": [], "metadata": {"_cell_guid": "f63d6148-2fec-45a5-af93-7ec075b567be", "collapsed": true, "_uuid": "652a409531489960068acd2f9716a2e356f3365d"}, "execution_count": null}, {"source": ["#Output file\n", "N3 = str(a3)\n", "print('Accuracy = ',a3*100)\n", "test['unit_sales'] = r3.predict(test[col])\n", "cut = 0.+1e-12 # 0.+1e-15\n", "test['unit_sales'] = (np.exp(test['unit_sales']) - 1).clip(lower=cut)\n", "\n", "output_file = 'sample_submission_RF1.csv'\n", " \n", "test[['id','unit_sales']].to_csv(output_file, index=False, float_format='%.2f')\n", "print('file created')"], "cell_type": "code", "outputs": [], "metadata": {"_cell_guid": "ab0f86b0-12ed-454e-838d-91bab84e99de", "collapsed": true, "_uuid": "7c3976fce66a18d7f615d3bd9b26ac719724ed4f"}, "execution_count": null}], "metadata": {"kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}, "language_info": {"file_extension": ".py", "name": "python", "version": "3.6.4", "codemirror_mode": {"name": "ipython", "version": 3}, "pygments_lexer": "ipython3", "mimetype": "text/x-python", "nbconvert_exporter": "python"}}, "nbformat": 4, "nbformat_minor": 1}