{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn import model_selection, preprocessing, metrics\nimport lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"343b5c440fe82801e016a4b1abbea6478df7951f"},"cell_type":"markdown","source":"#### If you find this kernel usefull , pls do upvote. It really motivates"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"## Dataset exploration"},{"metadata":{"trusted":true,"_uuid":"c5ffb0e7f325554e61cbb25c2eb727de21e15865"},"cell_type":"code","source":"!ls ../input/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"29201d878568a6948da5524dfb08fccb9c0a3418"},"cell_type":"markdown","source":"## Lets peek into each of files"},{"metadata":{"trusted":true,"_uuid":"05ba08f2d3242a273e5c39c48c296bda0fd55d19"},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b15eb7e01d750418ff845d264f86e42a34fd78b"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6d2caa245ec603968f13078c4dd0b4190eaeab5c"},"cell_type":"markdown","source":"Lets look at each of the columns to understand more \n\nStarting with card_id"},{"metadata":{"trusted":true,"_uuid":"26b6321d909a1fdd14c6ffa798098278e17d4fd9"},"cell_type":"code","source":"train[\"card_id\"].head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2cab19f5c3e83bcd863ff13b06db8290226ebfea"},"cell_type":"markdown","source":"## ^^Card Id is the unique id representing each user \n## Lets check the sanity of this data"},{"metadata":{"trusted":true,"_uuid":"b08f431ecaa4cf76e541b940d0abe8976e66ad98"},"cell_type":"code","source":"card_id_groupby_count = train.groupby(\"card_id\")[\"target\",\"first_active_month\"].agg([\"count\",\"size\"]).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f59781baaf39b3c6514f102a6c2bee0bb8f63e8f"},"cell_type":"code","source":"card_id_groupby_count.columns = pd.Index([\"_\".join(col) for col in card_id_groupby_count.columns.tolist()])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12915726241ac6747d9b6ed5c27dbdca2f861259"},"cell_type":"code","source":"card_id_groupby_count.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d3f8966998cb2fd8b549a83b85cfb95858c79eeb"},"cell_type":"code","source":"card_id_groupby_count[card_id_groupby_count[\"target_count\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3aa1ee7f47481e4a377acd35d2223281854cb154"},"cell_type":"code","source":"card_id_groupby_count[card_id_groupby_count[\"target_size\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c15f9bbd6a78956fe5dade5eb1d6c97798ab60f"},"cell_type":"code","source":"card_id_groupby_count[card_id_groupby_count[\"first_active_month_count\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7944c0490c2158c62959a424e151d62a0deb66cc"},"cell_type":"code","source":"card_id_groupby_count[card_id_groupby_count[\"first_active_month_size\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a4114655e97ce363fccb18180fca2b7f1eca0f39"},"cell_type":"markdown","source":"### ^^ Looks good"},{"metadata":{"_uuid":"843ab6e776af6838210fc436e5fab797edb9767f"},"cell_type":"markdown","source":"### Lets look into test and do similar sanity check"},{"metadata":{"trusted":true,"_uuid":"a2a5afa59c5e5976343f8205066a0b4ab5d072c4"},"cell_type":"code","source":"test = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a0fa250463e4a30a9e2cb414950d8689aa1c91d"},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"618c8cb8f6ab55901134646fc011963c06ddd117"},"cell_type":"code","source":"card_id_test_groupby_count = test.groupby(\"card_id\")[\"first_active_month\"].agg([\"count\",\"size\"]).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"890429fb671b0fe9fae723df97a5a9a599073209"},"cell_type":"code","source":"card_id_test_groupby_count[card_id_test_groupby_count[\"count\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"490aa975e647c3871fc3f61675168b50b76f0b17"},"cell_type":"code","source":"card_id_test_groupby_count[card_id_test_groupby_count[\"size\"] > 1].count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d2dc911b007d0c79d12dfad64848f46966fa281b"},"cell_type":"markdown","source":"### ^^ All looks good here too\n\n####  Lests look at the distribution of first active month"},{"metadata":{"trusted":true,"_uuid":"08b4fd670e8e6fc59e24b45068d8588138f2b2dd"},"cell_type":"code","source":"train[\"first_active_month\"] = pd.to_datetime(train[\"first_active_month\"])\ntest[\"first_active_month\"] = pd.to_datetime(test[\"first_active_month\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce9ac25d54e35f44a8cb81d9a20f8631b16f1fb9"},"cell_type":"code","source":"cnt_srs = train['first_active_month'].dt.date.value_counts()\ncnt_srs = cnt_srs.sort_index()\nplt.figure(figsize=(14,6))\nsns.barplot(cnt_srs.index, cnt_srs.values, alpha=0.8, color='green')\nplt.xticks(rotation='vertical')\nplt.xlabel('First active month', fontsize=12)\nplt.ylabel('Number of cards', fontsize=12)\nplt.title(\"First active month count in train set\")\nplt.show()\n\ncnt_srs = test['first_active_month'].dt.date.value_counts()\ncnt_srs = cnt_srs.sort_index()\nplt.figure(figsize=(14,6))\nsns.barplot(cnt_srs.index, cnt_srs.values, alpha=0.8, color='green')\nplt.xticks(rotation='vertical')\nplt.xlabel('First active month', fontsize=12)\nplt.ylabel('Number of cards', fontsize=12)\nplt.title(\"First active month count in test set\")\nplt.show()\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e7d00ea1263aaf81a7c69d1a9ef6ea930289f851"},"cell_type":"markdown","source":"### ^^ The distribution of test and train is similar\n\n### Lets look at the target variable distribution"},{"metadata":{"trusted":true,"_uuid":"e205478875068a075956e3b50a21fb23ef446bd3"},"cell_type":"code","source":"train[\"target\"].describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d67df636634d0fec4d6f53aff012e0233221e0d7"},"cell_type":"code","source":"plt.figure(figsize=(8,6))\nplt.scatter(range(train.shape[0]), np.sort(train[\"target\"].values))\nplt.xlabel('index', fontsize=12)\nplt.ylabel('Loyalty Score', fontsize=12)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0d5597f9ce529afb9a272b2b7bec5ed84fbe942"},"cell_type":"code","source":"plt.figure(figsize=(12,8))\nsns.distplot(train[\"target\"],bins=50, kde=False, color=\"red\")\nplt.title(\"Histogram of Loyalty score\")\nplt.xlabel('Loyalty score', fontsize=12)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b09799cba0a535eb4e45f8ce0f0c453450d844d"},"cell_type":"markdown","source":"## ^^ There is small outlier values , but rest looks good\n\n### Lets look into three other features"},{"metadata":{"trusted":true,"_uuid":"ac232aeaa44b8731b89934e9ca305cba4f895d57"},"cell_type":"code","source":"def plotViolin(feature):\n    plt.figure(figsize=(8,4))\n    sns.violinplot(x=feature, y=\"target\", data=train)\n    plt.xticks(rotation='vertical')\n    plt.xlabel('Feature 1', fontsize=12)\n    plt.ylabel('Loyalty score', fontsize=12)\n    plt.title(\"Feature 1 distribution\")\n    plt.show()\n\nplotViolin(\"feature_1\")\nplotViolin(\"feature_2\")\nplotViolin(\"feature_3\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b15ec2ccf5a455bb3ea77c6e648613cb2c59a747"},"cell_type":"code","source":"sns.distplot(train[\"feature_1\"],kde=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc79aa5aa196d5fc9aee0d2dbd499fa844f8a601"},"cell_type":"code","source":"sns.distplot(train[\"feature_2\"],kde=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a75f297b26ffffaac509931c451e280eae615f56"},"cell_type":"code","source":"sns.distplot(train[\"feature_3\"],kde=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"286cf6161e73034838847f7e39a6e24b675cd4f1"},"cell_type":"markdown","source":"### ^^ The data looks more of discreate than continuous , And the distribution of target variable seems to same for different values which means we dont see yet if these features are significant or not ,  we need see how models see it "},{"metadata":{"trusted":true,"_uuid":"d2de28b32c9025ac47cd3d75748750516d7a02a9"},"cell_type":"markdown","source":"### Lets look at the historical transaction data"},{"metadata":{"trusted":true,"_uuid":"078389a948ef1d08b6fd7b0796ae00e0da1a33a5"},"cell_type":"code","source":"historical_transaction = pd.read_csv(\"../input/historical_transactions.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f73f56a2abf79e72ff8871b8dd4e81282b7bbeb"},"cell_type":"code","source":"historical_transaction.describe(include=\"all\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d55de34af244ad671ef3973e44c4e02cdb87342"},"cell_type":"code","source":"historical_transaction.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8664ff5ff0e0ce6af6441630b881310236ee919c"},"cell_type":"code","source":"historical_transaction.count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f1edffd8e38f472a5079d5ab0fa9ca19871c8e8e"},"cell_type":"markdown","source":"### Lets look at each of the above columns \n\nLets start to look at authorized_flag"},{"metadata":{"trusted":true,"_uuid":"89ef97e7fb5634252db6945787c98ea0c7a96a2a"},"cell_type":"code","source":"columns_check = ['authorized_flag', 'category_1', 'installments',\\\n                 'category_3','month_lag','category_2','state_id','subsector_id']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0c1c70ae835a21d9e9e33dc0190a07fc84d3c620"},"cell_type":"code","source":"def plot_distribution(column,df):\n    distribution  = df[column].value_counts()\n    plt.figure(figsize=(10,5))\n    sns.barplot(distribution.index, distribution.values, alpha=0.8)\n    plt.title('Distribution of ' + column)\n    plt.ylabel(\"distribution\", fontsize=12)\n    plt.xlabel(column, fontsize=12)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e764a6648510e1d51475985aa1eabc1304b41204"},"cell_type":"markdown","source":"Lets look at the distribution of city_id flag"},{"metadata":{"trusted":true,"_uuid":"baf7ee860d520e511de69b188478deec48ca8db1","scrolled":false},"cell_type":"code","source":"for col in columns_check:\n    plot_distribution(col,historical_transaction)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"669af2f7dc5f5c22d1c9438a57d0f8625255b06a"},"cell_type":"markdown","source":"## The summary of the data present in historical transaction file is as follows , more digging needed\n###### authorized_flag               -  Looks like if the transaction was authorized or not , \n###### card_id                             - This is the unique card id  \n###### city_id                               - city in which the transaction was done\n###### category_1                        - some masked category , has only two values and one of them is dominating\n###### category_2                       - Some masked category, Take 5 values.\n###### category_3                       - Some masked category, Take 3 values\n###### merchant_category_id    - The category the merchant belongs to \n###### installments                    - My guess is that it is the installments at which the purchase was completed , most of installments were completed in 0 , some at 1 . Not quite sure what 0 means ....\n###### merchant_id                    - Unique Merchant Id \n###### month_lag                       - Month lag to reference date\n###### purchase_amount          - Amount of transaction \n###### purchase_date               - purchase date\n###### state_id                           - state in which the transaction was done\n###### subsector_id                   - something similar to merchant_category_id .\n\n"},{"metadata":{"trusted":true,"_uuid":"845b54c975951152f401b1c7e8437fd73b8587ab"},"cell_type":"markdown","source":"## Lets now shift our focus to merchants.csv file"},{"metadata":{"trusted":true,"_uuid":"33840de777d973e8422222f8c77a3c0faf9f19e7"},"cell_type":"code","source":"merchants = pd.read_csv(\"../input/merchants.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6473d9dde7c508bc797b73c0e86c623f07c1f70b"},"cell_type":"code","source":"merchants.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de7082124bd8a8b5844ac974e40e4002ffa56d4d"},"cell_type":"code","source":"merchants.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"483634e584868ea2d5a21273c79210dd528dd47f"},"cell_type":"code","source":"columns_to_check_merchant = [\n       'subsector_id', 'category_1',\n       'most_recent_sales_range', 'most_recent_purchases_range',\n       'category_4', 'city_id', 'state_id', 'category_2']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6e92698ac68dee7609b0e504acdaa851635258cc"},"cell_type":"code","source":"['merchant_group_id', 'merchant_category_id',\n       'numerical_1', 'numerical_2', 'category_1',\n       'most_recent_sales_range', 'most_recent_purchases_range',       \n       'category_4', 'city_id', 'state_id', 'category_2']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"569b19af4a2e98459446d3893a981af551cb8672","scrolled":false},"cell_type":"code","source":"for col in columns_to_check_merchant:\n    plot_distribution(col,merchants)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3732651b593f1d97fdfe4972fcd9bb8c4c291df6"},"cell_type":"code","source":"merchants[\"numerical_1\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"10d9abbd8022b67cd1f7e99177f9c4b12baa45e3"},"cell_type":"markdown","source":"## ^^ most of values in column numerical_1 is less"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"72509f3b922e16d7d422b1ba028bb3e1bd57f93c"},"cell_type":"code","source":"merchants[\"numerical_2\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cdd6f7901077c1d87b1c596beabf7b6c7888501c"},"cell_type":"markdown","source":"#### ^^ The distribution and range of both columns numerical_1 and numerical_2 very similar , are these two related ?"},{"metadata":{"trusted":true,"_uuid":"445661c6139d26eca55f0f798ca6fcf7b4feac31"},"cell_type":"code","source":"merchants[[\"numerical_1\",\"numerical_2\"]].corr()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d737905b72cb7ac12bcc9bf91f10efbddd8d9ddc"},"cell_type":"markdown","source":"#### ^^ Yes these two variables are highly co related"},{"metadata":{"trusted":true,"_uuid":"27044acac53c43a50ea1461ee7bd22cbb9fb9a05"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e0eef6e13a48e16098a72a62693d2db705bf295"},"cell_type":"markdown","source":"## The summary of the data present in historical transaction file is as follows , more digging needed\n#### merchant_id                                        : Unique Merchant Id \n#### merchant_group_id                          : Yet another group Id , needs to be investigated   \n#### merchant_category_id                     : This field looks similar to the one we say in historical transaction file   , we have to find how these two are related\n#### subsector_id                                       :  This field looks similar to the one we say in historical transaction file   , we have to find how these two are related\n#### numerical_1                                         : Highly correlated with numerical_2, \n#### numerical_2                                        :  \n#### category_1                                           :  This field looks similar to the one we say in historical transaction file   , we have to find how these two are related\n#### most_recent_sales_range              :  Yet another categorical data.       \n#### most_recent_purchases_range   :  Yet another categorical data.                       \n#### avg_sales_lag3                                  :  To look deeper\n#### avg_purchases_lag3                             :  To look deeper\n#### active_months_lag3                             :  To look deeper\n#### avg_sales_lag6                                 :  To look deeper\n#### avg_purchases_lag6                             :  To look deeper\n#### active_months_lag6                             :  To look deeper\n#### avg_sales_lag12                                :  To look deeper\n#### avg_purchases_lag12                            :  To look deeper\n#### active_months_lag12                            :  To look deeper\n#### category_4                                        : Yet another categorical data\n#### city_id                                                : city id of the merchant\n#### state_id                                             : state of the merchant \n#### category_2                                      : This field looks similar to the one we say in historical transaction file   , we have to find how these two are related"},{"metadata":{"trusted":true,"_uuid":"c4632b96e8ac5c8a6310f29184d929f17aa0b766"},"cell_type":"markdown","source":"## Lets look at the last file New Merchant Transactions"},{"metadata":{"trusted":true,"_uuid":"1638d9fb3f884a120c9b5dc71af1180ba47a7bf4"},"cell_type":"code","source":"new_trans_df = pd.read_csv(\"../input/new_merchant_transactions.csv\")\nnew_trans_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f53003b813ee424caec1bb94dd804eca70fd3118"},"cell_type":"markdown","source":"#### ^^ these columns looks similar to ones present in historical transactions file , let verify it "},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"08b09e14f7060afda78bfaa484d60143b6206ec8"},"cell_type":"code","source":"new_trans_df.columns == historical_transaction.columns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"011b12920dea1282013c5374f75789c62bb85927"},"cell_type":"markdown","source":"## Lets Quickly build a baseline model "},{"metadata":{"_uuid":"59b1bff100871e60abe5e9aa775b3695b38f25c1"},"cell_type":"markdown","source":"### Adding few Simple features"},{"metadata":{"trusted":true,"_uuid":"fac1a279702f49501746d6e5309215e14f864e8f"},"cell_type":"code","source":"gdf = historical_transaction.groupby(\"card_id\")\ngdf = gdf[\"purchase_amount\"].size().reset_index()\ngdf.columns = [\"card_id\", \"num_hist_transactions\"]\ntrain = pd.merge(train, gdf, on=\"card_id\", how=\"left\")\ntest = pd.merge(test, gdf, on=\"card_id\", how=\"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f35a207c8a10d515df9a5ecf7dc3e7f5101a1f4"},"cell_type":"code","source":"gdf = historical_transaction.groupby(\"card_id\")\ngdf = gdf[\"purchase_amount\"].agg(['sum', 'mean', 'std', 'min', 'max']).reset_index()\ngdf.columns = [\"card_id\", \"sum_hist_trans\", \"mean_hist_trans\", \"std_hist_trans\", \"min_hist_trans\", \"max_hist_trans\"]\ntrain = pd.merge(train, gdf, on=\"card_id\", how=\"left\")\ntest = pd.merge(test, gdf, on=\"card_id\", how=\"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e22f74efdf5a96c6add766a3dc0df181560aa3d"},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1e7387aa493343d1ad4c1ef361be6c070f44e34"},"cell_type":"code","source":"gdf = new_trans_df.groupby(\"card_id\")\ngdf = gdf[\"purchase_amount\"].agg(['sum', 'mean', 'std', 'min', 'max']).reset_index()\ngdf.columns = [\"card_id\", \"sum_merch_trans\", \"mean_merch_trans\", \"std_merch_trans\", \"min_merch_trans\", \"max_merch_trans\"]\ntrain = pd.merge(train, gdf, on=\"card_id\", how=\"left\")\ntest = pd.merge(test, gdf, on=\"card_id\", how=\"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f04b88495b8a71805a70522a6b53a9247e3c4630"},"cell_type":"code","source":"gdf = new_trans_df.groupby(\"card_id\")\ngdf = gdf[\"purchase_amount\"].size().reset_index()\ngdf.columns = [\"card_id\", \"num_merch_transactions\"]\ntrain = pd.merge(train, gdf, on=\"card_id\", how=\"left\")\ntest = pd.merge(test, gdf, on=\"card_id\", how=\"left\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94e7a299c2a52799d946a4281d90621c7a2de19e"},"cell_type":"code","source":"target_col = \"target\"\ntrain[\"year\"] = train[\"first_active_month\"].dt.year\ntest[\"year\"] = test[\"first_active_month\"].dt.year\ntrain[\"month\"] = train[\"first_active_month\"].dt.month\ntest[\"month\"] = test[\"first_active_month\"].dt.month\n\ncols_to_use = [\"feature_1\", \"feature_2\", \"feature_3\", \"year\", \"month\", \n               \"num_hist_transactions\", \"sum_hist_trans\", \"mean_hist_trans\", \"std_hist_trans\", \n               \"min_hist_trans\", \"max_hist_trans\",\n               \"num_merch_transactions\", \"sum_merch_trans\", \"mean_merch_trans\", \"std_merch_trans\",\n               \"min_merch_trans\", \"max_merch_trans\",\n              ]\n\ndef run_lgb(train_X, train_y, val_X, val_y, test_X):\n    params = {\n        \"objective\" : \"regression\",\n        \"metric\" : \"rmse\",\n        \"num_leaves\" : 30,\n        \"min_child_weight\" : 50,\n        \"learning_rate\" : 0.05,\n        \"bagging_fraction\" : 0.7,\n        \"feature_fraction\" : 0.7,\n        \"bagging_frequency\" : 5,\n        \"bagging_seed\" : 2018,\n        \"verbosity\" : -1\n    }\n    \n    lgtrain = lgb.Dataset(train_X, label=train_y)\n    lgval = lgb.Dataset(val_X, label=val_y)\n    evals_result = {}\n    model = lgb.train(params, lgtrain, 1000, valid_sets=[lgval], early_stopping_rounds=100, verbose_eval=100, evals_result=evals_result)\n    \n    pred_test_y = model.predict(test_X, num_iteration=model.best_iteration)\n    return pred_test_y, model, evals_result\n\ntrain_X = train[cols_to_use]\ntest_X = test[cols_to_use]\ntrain_y = train[target_col].values\n\npred_test = 0\nkf = model_selection.KFold(n_splits=5, random_state=2018, shuffle=True)\nfor dev_index, val_index in kf.split(train):\n    dev_X, val_X = train_X.loc[dev_index,:], train_X.loc[val_index,:]\n    dev_y, val_y = train_y[dev_index], train_y[val_index]\n    \n    pred_test_tmp, model, evals_result = run_lgb(dev_X, dev_y, val_X, val_y, test_X)\n    pred_test += pred_test_tmp\npred_test /= 5.\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38e13ab2185d71e6e37a88d9682f5fd7d99f7dc2"},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12,10))\nlgb.plot_importance(model, max_num_features=50, height=0.8, ax=ax)\nax.grid(False)\nplt.title(\"LightGBM - Feature Importance\", fontsize=15)\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"590ea7607424e1bc8e966b7d0b9d03c24102f86c"},"cell_type":"code","source":"sub_df = pd.DataFrame({\"card_id\":test[\"card_id\"].values})\nsub_df[\"target\"] = pred_test\nsub_df.to_csv(\"baseline_lgb.csv\", index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76d8b44f6ed86eeb14f4d2bad8d148c18a6684f8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}