{"cells": [{"outputs": [], "source": ["import pandas as pd \n", "import numpy as np\n", "import gc\n", "from scipy import sparse"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "e671b6bc-0d77-4c5e-9ada-48400bb3c244", "_uuid": "4ea57ab2ec45635a1f33f3a1802a5bb58dba73c5"}}, {"outputs": [], "source": ["from datetime import datetime as dt\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import matplotlib\n", "#matplotlib.use('Qt5Agg')\n", "sns.set_style('whitegrid')\n", "\n", "%matplotlib inline"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "e45bf31e-82be-4f19-a75c-d8eb90d1e898", "_uuid": "d37d2c27cb015d78d4681f6613cad8207e31e67a"}}, {"outputs": [], "source": ["def add_feature(df):\n", "    df['year'] = pd.DatetimeIndex(df['date']).year\n", "    df['month'] = pd.DatetimeIndex(df['date']).month\n", "#    df['week'] = pd.DatetimeIndex(df['date']).week\n", "    df['day'] = pd.DatetimeIndex(df['date']).day\n", "    \n", "    df['year'] = df['year'].astype(np.uint16)\n", "    df['month'] = df['month'].astype(np.uint8)\n", "#    df['week'] = df['week'].astype(np.uint8)\n", "    df['day'] = df['day'].astype(np.uint8)\n", "    return df"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "7230363f-339f-4d98-8edf-d81432cf54d7", "_uuid": "16f9d34a508d8224cd9c92a1ca082c17a91e2caa"}}, {"outputs": [], "source": ["coltype = {\n", "    'date':object,\n", "    'onpromotion':bool\n", "}\n"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "a04ac749-e673-4c45-a79b-4872a4e338e4", "_uuid": "e8daba6ad148660dea80fe26e602afac77750653"}}, {"outputs": [], "source": ["ttl_chunks = []\n", "for chunk in pd.read_csv('../input/train.csv',dtype=coltype,chunksize=1000000):\n", "    chunk['onpromotion'].fillna(0,inplace=True)\n", "    chunk['onpromotion'] = chunk['onpromotion'].astype(np.uint8)\n", "    chunk = add_feature(chunk)\n", "    ttl_chunks.append(chunk)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "ce06f413-c590-44f6-99ee-59a9182674b7", "_uuid": "6e1a22d4cd7597ba5e4f7075f60ddcd52e8b0fd2"}}, {"outputs": [], "source": ["ttl_sales = pd.DataFrame([],columns=['year','month','unit_sales'])\n", "for chunk in ttl_chunks:\n", "    sales = chunk.groupby(['year','month'])['unit_sales'].sum().reset_index()\n", "    ttl_sales = pd.concat([ttl_sales,sales],axis=0,ignore_index=True)\n", "ttl_sales = ttl_sales.groupby(['year','month'])['unit_sales'].sum().reset_index()\n", "ttl_sales['next_year'] = ttl_sales['unit_sales'].shift(-12)\n", "ttl_sales['yoy'] = ttl_sales['next_year']/ttl_sales['unit_sales']-1\n"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "9658a429-2a0b-4573-8fc0-a50924510b5b", "_uuid": "1160e6bdaf65fc46f91ffbb18a1bc3fffefc48ad"}}, {"outputs": [], "source": ["weight = ttl_sales.groupby('year').mean().reset_index()[['year','yoy']]\n", "weight = weight[weight['year'] != 2017]\n", "weight['year'] = weight['year']+1\n", "weight"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "90856165-678c-4ef6-b4ae-82b47e72f67c", "_uuid": "bb55723711234b8a3c68fa1f8cc836288e187f60"}}, {"outputs": [], "source": ["num_train_chunks = len(ttl_chunks)\n", "chunks=ttl_chunks[76:86]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "1842f403-c9d1-4470-8281-84123ec17163", "_uuid": "b32f9a1610a47a3168ceeb9cf6dc140f86a30559"}}, {"outputs": [], "source": ["#del ttl_chunks;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "54bf2795-d63f-46e6-91af-8b954ca79e79", "_uuid": "b757a1020a096a8cc16982532fa39589ff4eec4c"}}, {"outputs": [], "source": ["print(\"Train dataset chunk numbers is \"+str(len(chunks)))"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "ae4d4691-9008-46d6-9e44-f8b2762d22fb", "_uuid": "3e11cba4c74d66865efe5a0df12cffc0736e04a1"}}, {"outputs": [], "source": ["test_chunks = []\n", "for test_chunk in pd.read_csv(\"../input/test.csv\",dtype=coltype,chunksize=1000000):\n", "    test_chunk = add_feature(test_chunk)\n", "    test_chunk['onpromotion'].fillna(0,inplace=True) \n", "    test_chunk['onpromotion'] = test_chunk['onpromotion'].astype(np.uint8)\n", "    test_chunk['unit_sales'] = np.nan\n", "    test_chunks.append(test_chunk)\n", "\n", "print(\"Testing dataset chunk numbers is \"+str(len(test_chunks)))\n"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "02994dd4-403f-4a60-8c75-e8feb462eca1", "_uuid": "580f27b5192c6dfbdd60029274d703b34b7a6d91"}}, {"outputs": [], "source": ["test = pd.DataFrame([],columns=test_chunks[0].columns)\n", "for df in test_chunks:\n", "    test = pd.concat([test,df],axis=0)\n", "num_test = len([test])\n", "test_id = test.id\n", "len_test = test.shape[0]\n", "print(\"Test dataset info: \" + str(test.shape))"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "37a53f66-e71b-4169-b907-d03aef860f18", "_uuid": "d96cbf5647a471fcade8483aabcdc82f177cf2a1"}}, {"outputs": [], "source": ["# Make test shorter record\n", "#test = test.iloc[:10000]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "39d98a5b-f864-4873-875d-3d1b5316dd7b", "_uuid": "294ec6a4afc52890bc83e9b7b180d6c4cf5f0a05"}}, {"outputs": [], "source": ["all_chunks = chunks+[test]\n", "del chunk; gc.collect()"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "57086e7e-2da0-4808-8ad8-7fe7a7dc79e7", "_uuid": "563698592d8ba75731b09bdce900bac7c946791d"}}, {"outputs": [], "source": ["rows_test = test.shape[0]\n", "rows_test"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "4592de67-5d3f-4c60-8bab-0a8a7b1ed186", "_uuid": "3da8d35706f14af486fa7ce416fad32f58491521"}}, {"outputs": [], "source": ["def add_more_features(chunks,df_2_add,add_key=None):\n", "    new_chunks = []\n", "    for chunk in chunks:\n", "        new_chunk = chunk.merge(df_2_add,how='left',on=add_key)\n", "        new_chunks.append(new_chunk)\n", "    del new_chunk,df_2_add;gc.collect()\n", "    return new_chunks"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "0c0bb13e-6398-4689-863e-3a9c0989da95", "_uuid": "a292138fb1200f0e6bb3745d5295bda15183a9cf"}}, {"outputs": [], "source": ["stores = pd.read_csv('../input/stores.csv',usecols=[0,4])\n", "all_chunks = add_more_features(all_chunks,stores,'store_nbr')\n", "del stores;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "968fce91-4d77-43f0-90bf-2c313bd5af60", "_uuid": "af74513c28b0793172c1566cf3b7e3cb9d367deb"}}, {"outputs": [], "source": ["items = pd.read_csv('../input/items.csv',usecols=[0,3])\n", "all_chunks = add_more_features(all_chunks,items,'item_nbr')\n", "del items;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "a586e0cb-3c4c-47ca-a8ef-9f380d0c6e52", "_uuid": "ecc5c9387d3ad170a6f775caafae3c8b4fa0e1ef"}}, {"outputs": [], "source": ["oil = pd.read_csv('../input/oil.csv',usecols=[0,1])\n", "all_chunks = add_more_features(all_chunks,oil,'date')\n", "del oil;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "8985c97c-9b9e-4f4c-9001-54726efe6aeb", "_uuid": "b6e242b2dc3d183d623a235deccb138c7d8252e8"}}, {"outputs": [], "source": ["for chunk in all_chunks:\n", "    chunk['dcoilwtico'][0] = np.mean(chunk['dcoilwtico'])\n", "    chunk['dcoilwtico'].fillna(method='ffill',inplace=True) \n", "    chunk['dcoilwtico'] = chunk['dcoilwtico'].astype(np.float32)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "d6c1cebb-3b82-4172-b7b9-2bb98fa912e5", "_uuid": "331b3eb5a68848fdecec2e7ad59e86bffd6426aa"}}, {"outputs": [], "source": ["holidays = pd.read_csv('../input/holidays_events.csv',usecols=[0,1,5])\n", "holidays['type'] = holidays['type'].apply(lambda x:0 if x=='Work Day' else 1)\n", "holidays['transferred'] = holidays['transferred'].apply(lambda x:1 if x=='True' else 0)\n", "all_chunks = add_more_features(all_chunks,holidays,'date')\n", "\n", "del holidays;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "9bcbef11-809a-426b-a829-0367de2e2c65", "_uuid": "9cb29b05ce85347f7b03825fea88f587437ab7cc"}}, {"outputs": [], "source": ["for chunk in all_chunks:\n", "    chunk['type'].fillna(0,inplace=True) \n", "    chunk['type'] = chunk['type'].astype(np.uint8)\n", "    chunk['transferred'].fillna(0,inplace=True) \n", "    chunk['transferred'] = chunk['transferred'].astype(np.uint8)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "f331c0e2-7d63-43c6-bb96-caf0896a5f6a", "_uuid": "135d15ccacd74f33ce15b5ff326a989915b194cb"}}, {"outputs": [], "source": ["from sklearn.preprocessing import OneHotEncoder\n", "from scipy import sparse"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "31e44d2a-3b40-49cc-8c45-262c33f2bcee", "_uuid": "94ca17a29f27dc99f7762797448f971f7bd870cd"}}, {"outputs": [], "source": ["from sklearn.linear_model import LinearRegression\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import LabelBinarizer"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "66f5a0c5-1730-4516-92fc-45e6af266f94", "_uuid": "810748c100613c31c0b121d83d8a19279ccc152a"}}, {"outputs": [], "source": ["chunks = all_chunks[:-1]\n", "test = all_chunks[-1]\n", "comb = pd.DataFrame([],columns=all_chunks[0].columns)\n", "for i in range(len(chunks)):\n", "    if i == 0:\n", "        comb = chunks[i]\n", "    else:\n", "        comb = pd.concat([comb,chunks[i]],axis=0)\n", "#del all_chunks\n", "#gc.collect()"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "963b4979-6d64-401f-a1ce-05a19d1ba4f8", "_uuid": "fc637bcdb42cf84df943040c2a36ec1ec09e9275"}}, {"outputs": [], "source": ["# train dataset unique value in very column combine with test dataset unique value of very column\n", "# Append all combination of above combined column unique values difference before label endcorder\n", "# Choose chunks 0 for now for quick work\n", "\n", "comb_fornow = comb[['year','month','day','store_nbr','item_nbr','cluster','onpromotion','perishable','transferred','type','dcoilwtico']]\n", "y = comb.unit_sales.values\n", "cat_cols=['year','month','day','store_nbr','item_nbr','cluster']\n", "num_cols=['onpromotion','perishable','type','transferred','dcoilwtico']\n", "del comb;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "a4dc8ea9-36bc-4a50-978b-1f9ac1d85ded", "_uuid": "0e9aeab64dc84d8f9ce90ece953b902acf90a807"}}, {"outputs": [], "source": ["cat_train = comb_fornow[cat_cols]\n", "cat_test = test[cat_cols]\n", "num_train = comb_fornow[num_cols]\n", "num_test = test[num_cols]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "f94e2bb5-5e09-4ca3-bf1c-188d9fdf156d", "_uuid": "429c8360f95a85f59e3b3e1772564538fccbaff8"}}, {"outputs": [], "source": ["\n", "def TrainTestSameDimension(cat_train,cat_test):\n", "    # train_fill_values is the value fillna in created dataset. train_fill_values IS THE last row of \n", "    # Categroy data set\n", "    train_fill_values = dict(zip(cat_train.columns,cat_train.iloc[-1].values))\n", "    test_fill_values = dict(zip(cat_test.columns,cat_test.iloc[-1].values))\n", "    temp_train = []\n", "    temp_test = []\n", "    if cat_train.shape[1] != cat_test.shape[1]:\n", "        print(\"Please remove target variable!\")\n", "    else:\n", "        for col in cat_train.columns:\n", "            globals() [col+'_train'] = set(cat_test[col])-set(cat_train[col])\n", "            globals() [col+'_test'] = set(cat_train[col])-set(cat_test[col])\n", "            temp_train.append(pd.Series(list(globals() [col+'_train']),name=col))\n", "            temp_test.append(pd.Series(list(globals() [col+'_test']),name=col))\n", "        train_df = pd.DataFrame(temp_train).T\n", "        train_df.fillna(value=train_fill_values,inplace=True)\n", "        test_df = pd.DataFrame(temp_test).T\n", "        test_df.fillna(value=test_fill_values,inplace=True)\n", "    return {'train':train_df,'test':test_df}"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "12a47716-e3de-4e45-be09-eabefff3642c", "_uuid": "566b738e98fe0c8eec4ea0bbad9d5caaec20dfef"}}, {"outputs": [], "source": ["#del comb_fornow ;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "5dfe9824-a687-426d-8c82-09ab1f83826a", "_uuid": "ec99ed5395b6c451d3dccf49a1cf190f10128f31"}}, {"outputs": [], "source": ["train_added2cat_train=TrainTestSameDimension(cat_train[cat_cols],cat_test[cat_cols])['train']\n", "test_added2cat_test=TrainTestSameDimension(cat_train[cat_cols],cat_test[cat_cols])['test']"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "9075d5ce-82a3-4185-b7dd-37601f3131b4", "_uuid": "2ee82d8cac1287b2a1549f599f32503031d44f1a"}}, {"outputs": [], "source": ["train_final = pd.concat([comb_fornow,train_added2cat_train],axis=0)\n", "train_final.fillna(0,inplace=True)\n", "cat_test_concat = pd.concat([test,test_added2cat_test],axis=0)\n", "#del train_added2cat_train ;gc.collect();\n", "#del test_added2cat_test ;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "dcdf6182-5cfb-4ed1-97f3-35f80950372e", "_uuid": "ac183a0f00ea94a8f204e111b38e11e5e258f8cd"}}, {"outputs": [], "source": ["# fill number columns' na with 0\n", "#train_final = cat_train_concat.merge(num_train,how='left',left_index=True,right_index=True)\n", "#train_final.fillna(0,inplace=True)\n", "#del cat_train_concat ;gc.collect();\n", "del num_train ;gc.collect();\n", "print(\"Wait for above step finished!\")"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "b41c643c-abc8-45ae-8500-4b3058125475", "_uuid": "fcedcc2da14698ea3c00b3bb204c655a566c1cd5"}}, {"outputs": [], "source": ["test_final = pd.concat([comb_fornow,train_added2cat_train],axis=0)\n", "test_final.fillna(0,inplace=True)\n", "#del cat_test_concat ;gc.collect();\n", "del num_test ;gc.collect();"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "5a51e451-798b-4620-87e2-4f585600e1e4", "_uuid": "55e05ff974e705501ed4dcabe7a2c2b5035d0cec"}}, {"outputs": [], "source": ["X_cat = train_final[cat_cols]\n", "X_num = train_final[num_cols]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "b29120f9-5e86-456f-8adf-0b2250990c24", "_uuid": "7f78b9e0abc86ab977dc2d446175febededb01f3"}}, {"outputs": [], "source": ["test_cat = test_final[cat_cols]\n", "test_num = test_final[num_cols]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "d4176bb1-1cdd-481f-b684-7db27679d699", "_uuid": "f29079fc95099edab001032e665f0fd41bfc1a18"}}, {"outputs": [], "source": ["def get_sparse(X_cat,X_num=None):\n", "    ohe_list = []\n", "    for cat in X_cat.columns:\n", "        ohe = OneHotEncoder(sparse=True)\n", "        ohe_list.append(ohe.fit_transform(X_cat[cat].values.reshape(len(X_cat),1)))\n", "    \n", "    for i in range(len(ohe_list)):\n", "        if i ==0:\n", "            temp = ohe_list[0]\n", "        else:\n", "            X_cat_new = sparse.hstack([temp,ohe_list[i]])\n", "    if sparse.issparse(X_num):\n", "        dct = X_cat_new\n", "    else:\n", "        dct = sparse.hstack([X_cat_new.astype(float),X_num])\n", "    return dct"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "484c3e24-0794-4e39-9643-686539b27ebd", "_uuid": "e4eab3551cc465f458f92955df277a683c588b08"}}, {"outputs": [], "source": ["#def generate_datasets(dataset,cat_cols,num_cols):\n", "##    train_chunk = chunk[:(len(chunk)-num_test)]\n", "##    comb = pd.concat([train_chunk,test],axis=0)\n", "#    comb = dataset\n", "#    comb.fillna(0,inplace=True)\n", "#    y=comb.unit_sales.values\n", "#    X_cat=comb[cat_cols]\n", "#    X_num=comb[num_cols]\n", "#    \n", "#    from sklearn.preprocessing import OneHotEncoder\n", "#    from scipy import sparse\n", "#    \n", "#    X_sparse = get_sparse(X_cat,X_num)['sparse']\n", "##    row_train = train_chunks.shape[0]\n", "##    row_test = test.shape[0]\n", "#    #return {'sparse':X_sparse,'y':y,'id':id,'row_train':row_train,'row_test':row_test}\n", "#    return {'sparse':X_sparse}"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "e278eb8b-1377-4935-b66a-8e5a723238d3", "_uuid": "45a1f206b53bd3d023ea55b409873a16cec3ccad"}}, {"outputs": [], "source": ["X_sparse = get_sparse(X_cat,X_num)\n", "test = get_sparse(test_cat,test_num)\n", "y=np.array(y.tolist()+[0]*(X_sparse.shape[0]-len(y)))\n", "print(X_sparse.shape)\n", "print(len(y))"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "f45238f4-ae66-45d9-bb4e-695f1c618fc2", "_uuid": "c147a10dbb5d3a4dee29eac2cce6916c2fb63fad"}}, {"outputs": [], "source": ["#import pickle\n", "#outfile_name = \"pickle_66-75.pkl\"\n", "#d = {'train':X_sparse,'test':test,'y':y}\n", "#with open(outfile_name, 'wb')  as fn:\n", "#    pickle.dump(d,fn,protocol=pickle.HIGHEST_PROTOCOL)\n", "#\n", "#with open(\"pickle1.pkl\", 'rb')  as fn:\n", "#    d = pickle.load(fn)\n", "#\n", "#X=d['train']\n", "#test=d['test']\n", "#y=d['y']"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "fd73454c-13c9-49b5-a87e-6f77c4721002", "_uuid": "36d50fb6c6d563e9fb42461cef9058465b9029af"}}, {"outputs": [], "source": ["X=X_sparse"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "7e0b201d-4505-4c76-9a7f-b7f1442fb593", "_uuid": "5d213ce05a905f103022302216a826a67de50f1e"}}, {"outputs": [], "source": ["X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)\n", "lg = LinearRegression()\n", "pred = lg.fit(X_train,y_train).predict(X_test)\n", "r2 = lg.score(X_test,y_test)\n", "intercept = lg.intercept_\n", "coef_list = lg.coef_\n", "print(len(coef_list))"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "c2a8a1d8-6b48-422d-8986-f52c59fce46e", "_uuid": "a664412b5db9c10c66ec015410803b18631cc093"}}, {"outputs": [], "source": ["r2"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "0c1e8483-e2af-4941-ae44-1bd78221023c", "_uuid": "e104410e6ad7bb0fcd6a0d9b4c87b59a09c0ac1f"}}, {"outputs": [], "source": ["#from sklearn.tree import DecisionTreeRegressor\n", "#dt = DecisionTreeRegressor()\n", "#pred = dt.fit(X_train,y_train).predict(X_test)\n", "#dt_r2 = dt.score(X_test,y_test)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "969503fc-2942-463c-92cf-a278b30be3b2", "_uuid": "0e00c9fc3fe05e99e81b3442379bca31a4653e3c"}}, {"outputs": [], "source": ["import xgboost as xgb"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "e637ae1c-f178-4bb5-bd8e-75e9e1039312", "_uuid": "ad73a56c3a8126828a7e2a829aac7da65a4403ed"}}, {"outputs": [], "source": ["dtrain = xgb.DMatrix(X_train, y_train)\n", "dvalid = xgb.DMatrix(X_test, y_test)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "bb98fa9e-9fa1-4930-aa8e-0f52efc69286", "_uuid": "20755a67c4dcddbdd5c826f4570edbb999a65aa6"}}, {"outputs": [], "source": ["params = {\"objective\": \"reg:linear\",\n", "          \"booster\" : \"gbtree\",\n", "          \"eta\": 0.3,\n", "          \"max_depth\": 10,\n", "          \"subsample\": 0.9,\n", "          \"colsample_bytree\": 0.7,\n", "          \"silent\": 1,\n", "          \"seed\": 1301\n", "          }\n", "num_boost_round = 30\n", "watchlist = [(dtrain, 'train'), (dvalid, 'eval')]"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "b84d5695-babb-4b1e-8d5e-14452883e2ef", "_uuid": "737d0ff44465de74071d84ff6f0183cd4fb652da"}}, {"outputs": [], "source": ["def rmspe(y, yhat):\n", "    return np.sqrt(np.mean((yhat/y-1) ** 2))\n", "\n", "def rmspe_xg(yhat, y):\n", "    y = np.expm1(y.get_label())\n", "    yhat = np.expm1(yhat)\n", "    return \"rmspe\", rmspe(y,yhat)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "449ec533-2a59-4c4b-a0f4-a5be13808270", "_uuid": "00522ded962a33048147d862e3239d1977debe83"}}, {"outputs": [], "source": ["#gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist,early_stopping_rounds=20, feval=rmspe_xg, verbose_eval=True)\n", "gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist,early_stopping_rounds=20, verbose_eval=True)\n"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "b1c1230d-2c96-40af-93df-378284d9ca59", "_uuid": "69899ecb95d337bcc494ef3c2b498534d0a55c47"}}, {"outputs": [], "source": ["print(\"Validating\")\n", "yhat = gbm.predict(xgb.DMatrix(X_test))\n", "error = rmspe(y_test, yhat)\n", "print('RMSPE: {:.6f}'.format(error))\n"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "bc340fd5-cc3f-443a-aa28-0a59d8324c80", "_uuid": "7b34ea35d7d3408967fe1b8f44bce4239c8ee231"}}, {"outputs": [], "source": ["from sklearn.metrics import r2_score\n", "r2=r2_score(y_test, yhat)\n", "r2"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "10239e5a-e4f1-4885-923d-0bbfdfb74b30", "_uuid": "3b2d0b54b09ae9bd4c1f6f52ad4dc2c3b687ccc7"}}, {"outputs": [], "source": ["dtest = xgb.DMatrix(test)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "066b3fbb-bed9-4a29-86e5-cc852d798042", "_uuid": "b35a0c60f9787b0d73c3f568ebfed0e4eee9d256"}}, {"outputs": [], "source": ["pred_xgb = gbm.predict(dtest)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "212af1b9-3ee3-49d0-b2a1-50cefa255422", "_uuid": "28403708a2bd34a8ec88ae680c411276123d245d"}}, {"outputs": [], "source": ["\n", "\n", "#result3 = pd.DataFrame({\"id\": test_id, 'unit_sales': pred_xgb[:len_test]})"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "3fd93651-0e38-45ad-a11e-e94f56e6a2ed", "_uuid": "b8c8e5fdcd71f411ab037ddc1850e2080c7d4a50"}}, {"outputs": [], "source": ["result1 = pd.DataFrame({\"id\": test_id, 'unit_sales': pred_xgb[:len_test]})"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "c3d40b68-1ad5-4acc-9977-5be24d060452", "_uuid": "9680660050f570448131962b395779024704ae9b"}}, {"outputs": [], "source": ["#result2 = pd.DataFrame({\"id\": test_id, 'unit_sales': pred_xgb[:len_test]})"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "27038693-80a5-4c28-8861-4d0d8dab4b09", "_uuid": "4cf18c505a5f5311782f0d64f6b938988ac7310e"}}, {"outputs": [], "source": ["result1.to_csv('submittion_1.csv',index=False)"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_kg_hide-output": true, "_cell_guid": "fbde5539-2530-4c14-8f30-d00d4a0fc468", "_uuid": "112a53407b43da7688bde9574b18241f5077216b"}}, {"outputs": [], "source": ["result"], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "50f54fb6-b3e5-46a5-9cd3-a2b6e98634fb", "_uuid": "4218f9eb2c7dbf389decc7af56d4737b365c3cec"}}, {"outputs": [], "source": [], "cell_type": "code", "execution_count": null, "metadata": {"collapsed": true, "_cell_guid": "696a2800-d328-43c0-b2bb-1d8b66f1d7a0", "_uuid": "887cc632fb8bfa51762ab16a38c9b1e8cd90500c"}}], "nbformat": 4, "metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "name": "python", "file_extension": ".py", "version": "3.6.4", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "nbconvert_exporter": "python"}}, "nbformat_minor": 1}