{"cells":[{"metadata":{"_uuid":"7cb82a6d1f008fcc568ee87245b751c2a4167f1c"},"cell_type":"markdown","source":"**Elo Merchant Category Recommendation**"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from sklearn import model_selection, preprocessing, metrics\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.model_selection import train_test_split\nfrom pandas import get_dummies\nimport lightgbm as lgb\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport xgboost as xgb\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport matplotlib\nimport warnings\nimport sklearn\nimport scipy\nimport numpy\nimport json\nimport sys\nimport csv\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7ed73971418d48d08bd579b0978c0053a926c82"},"cell_type":"code","source":"#Loading Train and Test Data\ndf_train = pd.read_csv('../input/train.csv', parse_dates=[\"first_active_month\"] )\ndf_test = pd.read_csv('../input/test.csv' ,parse_dates=[\"first_active_month\"] )\ndf_merchants=pd.read_csv('../input/merchants.csv')\ndf_new_merchant_transactions=pd.read_csv('../input/new_merchant_transactions.csv')\ndf_historical_transactions = pd.read_csv(\"../input/historical_transactions.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a43817cd33d20571e01653e7d717a7a49d452425"},"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dad1ee03d61a146113fdf28df75a8cbe41572b71"},"cell_type":"code","source":"sample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"88d2678a86ab4131d0a7a4489044934eaf0b759a"},"cell_type":"markdown","source":"* The final recommendation is the above target value or the loyalty score for the Elo Platform."},{"metadata":{"trusted":true,"_uuid":"8ef5f5e4eaeffeb448aa740d60e2d2d25aebfa70"},"cell_type":"code","source":"print(df_train.info())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64c2fb983fbc86f4285df600d210a0012c6355bb"},"cell_type":"code","source":"print(\"Shape of train set                 : \",df_train.shape)\nprint(\"Shape of test set                  : \",df_test.shape)\nprint(\"Shape of historical_transactions   : \",df_historical_transactions.shape)\nprint(\"Shape of merchants                 : \",df_merchants.shape)\nprint(\"Shape of new_merchant_transactions : \",df_new_merchant_transactions.shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d01d3ed4725d4a790848cbdc392534c80c6df7b"},"cell_type":"code","source":"#Missing Data Analysis on train set\ntotal = df_train.isnull().sum().sort_values(ascending = False)\npercent = (df_train.isnull().sum()/df_train.isnull().count()*100).sort_values(ascending = False)\nmissing_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percentage'])\nmissing_data.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4f88bf4134e9085e37f15eaa52b672fc22f9cca"},"cell_type":"code","source":"#Missing Data Analysis on test set\ntotal = df_test.isnull().sum().sort_values(ascending = False)\npercent = (df_test.isnull().sum()/df_test.isnull().count()*100).sort_values(ascending = False)\nmissing_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percentage'])\nmissing_data.head(20)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"_uuid":"6e9ee9d6034be9c058211d339b33f216913a6c36"},"cell_type":"markdown","source":"The above analysis signifies that there are no missing values in the train and test set"},{"metadata":{"trusted":true,"_uuid":"afc321625d177a9f79ff84b0c043ec20b7f35804"},"cell_type":"code","source":"df_train.target.plot.hist()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7a0a9553685bb80890e3b069d548ba710bf8f638"},"cell_type":"markdown","source":"The above plot signifies that most of the values of the target variable are between -9 to +9 .\n\nThere is an outlier value around -30"},{"metadata":{"trusted":true,"_uuid":"cd90adeb38b5891a53f22307fffcb04d56ca07d9"},"cell_type":"code","source":"# Code reference from https://www.kaggle.com/sudalairajkumar/simple-exploration-notebook-elo\n# SRK - Simple Exploration Notebook\n\nimport seaborn as sns\n\ncnt_srs = df_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 = df_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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10359e43998e7362ff1fb80feecdd31297a00592","scrolled":false},"cell_type":"code","source":"df_train.corr()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e34a8586a40fa03eb39747f9a8c84e48099e33ec"},"cell_type":"markdown","source":"*** From the above correlation matrix , it is apparent that the features have very less correlation with target value.\nFeature 3 has the least correlation as it is closest to 0 and the Feature_1 and Feature_2 have a little negative correlation**"},{"metadata":{"_uuid":"48463435548385016be6bfcf6fc8ec525086c6c6"},"cell_type":"markdown","source":"Only Feature 2 and Feature 1 are being converted to numerical values as Feature 3 will not impact the target score much because of negative correlation"},{"metadata":{"trusted":true,"_uuid":"a7f9b90bf2f408bf4cc2c5084a648e9a6058aa68","scrolled":true},"cell_type":"code","source":"df_train = pd.get_dummies(df_train, columns=['feature_1', 'feature_2'])\ndf_test = pd.get_dummies(df_test, columns=['feature_1', 'feature_2'])\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cbdc95185fb3cd61291d858f7a275f4cf9a94848"},"cell_type":"markdown","source":"**Basic EDA on Historical and New_merchant files**"},{"metadata":{"_uuid":"28a57c429e34dacb7a2b99649968e05f826774fa"},"cell_type":"markdown","source":"Top 10 cards with highest number of transactions in historical dataset"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"88a0288bfdc63d17ca5ea51f77b2a37f0390e36e"},"cell_type":"code","source":"df_card_history  = df_historical_transactions['card_id'].value_counts().head(10)\ndf_card_history","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f3550c0b20767486bc2399a576483a677a3a722"},"cell_type":"code","source":"df_card_history_authorized  = df_historical_transactions['authorized_flag'].value_counts()\ndf_card_history_authorized","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df255fd667dbf66a2b7dcea7dc19657c9e643291"},"cell_type":"code","source":"df_non_authorized = df_historical_transactions[df_historical_transactions['authorized_flag'] == \"N\"]\ndf_non_authorized","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a5059e68a4fd1725939b60d824f77c0e6800754e","scrolled":false},"cell_type":"code","source":"df_card_unauth  = df_non_authorized['card_id'].value_counts()\ndf_card_unauth.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b16c6f883ab76605e9598a1d189b9d8415262d13"},"cell_type":"markdown","source":"The below 2 card_ids have the most unauthorized transactions\n\n\n**C_ID_5ea401d358 ** --------- \n** C_ID_3d3dfdc692  **"},{"metadata":{"trusted":true,"_uuid":"e10d068796dc97522f3faf3fdeb683ee5c3d2c18","scrolled":true},"cell_type":"code","source":"df_card_1_unauth= df_non_authorized[df_non_authorized['card_id']==\"C_ID_5ea401d358\"]\ndf_card_2_unauth = df_non_authorized[df_non_authorized['card_id']==\"C_ID_3d3dfdc692\"]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"caf8fee38b71c1ae0b23c373e960bcae98c92344"},"cell_type":"markdown","source":"Below are the findings for the merchants where these cards have done the  most unauthorized transactions"},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"b3d83752cf92bdb4b6bba467ced4c03dc843679b"},"cell_type":"code","source":"df_card_1_merch = df_card_1_unauth['merchant_id'].value_counts()\ndf_card_1_merch.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"66d50e03c1275d271274f4afcecf51bc01ce6a97"},"cell_type":"markdown","source":"\"C_ID_5ea401d358\" has the most unauthorized transactions at \" M_ID_9fa00da7b2 \"    "},{"metadata":{"trusted":true,"_uuid":"26a1cd69b75cffefa067bb353a26adb5a5a5030b"},"cell_type":"code","source":"df_card_1_merch = df_card_2_unauth['merchant_id'].value_counts()\ndf_card_1_merch.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc617abf2dae42cfc1f85524feafafa3492b9261"},"cell_type":"markdown","source":"\"C_ID_3d3dfdc692\" has the most unauthorized transactions at  \"M_ID_79692349d6\"    "},{"metadata":{"_uuid":"ebd748fb800b8c6f8ad49ee6889637fbb14720d2"},"cell_type":"markdown","source":"**Top 10 cards with highest number of transactions in new merchant transactions dataseta**"},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"69b3c02ebe3bb7f16ec4f45e2f3af43103cd5cfb"},"cell_type":"code","source":"df_card_new  = df_new_merchant_transactions['card_id'].value_counts().head(10)\ndf_card_new","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"7b89dc7af427c26477489a72cc8bd9e9baccadcb"},"cell_type":"code","source":"df_non_authorized = df_new_merchant_transactions[df_new_merchant_transactions['authorized_flag'] == \"N\"]\ndf_non_authorized","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"460494a5ea80befefae0b8080acc260b5f292d44"},"cell_type":"markdown","source":"***All the transactions in the New Merchant transactions are authorized transactions. **"},{"metadata":{"_uuid":"23c9cd8238de0493c5dd5e1a73349d87238db816"},"cell_type":"markdown","source":"Moving on to the Historical and new merchant transactions table\nThe most interesting feature in transactions table seems to be the `purchase_amount`for a particular card_id \n\nIdeally the values should have been positive , but because the values have been normalized the data is different from expectation . Also,there should have been some integers in it's distribution which is obviously not the case here!"},{"metadata":{"_uuid":"46bf9db8eed4725aff831c940d813ae4681dc27c"},"cell_type":"markdown","source":""},{"metadata":{"trusted":true,"_uuid":"b8bbcce2a5cc81839d16c171534a2d0cb1ba03c0"},"cell_type":"code","source":"df_new_merchant_transactions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"52993cecb1adde9334a1d9bd5da803d739aacfb4"},"cell_type":"code","source":"df_new_merchant_transactions['purchase_amount_integer'] = df_new_merchant_transactions.purchase_amount.apply(lambda x: x == np.round(x))\nprint(df_new_merchant_transactions.groupby('purchase_amount_integer')['card_id'].count())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"15935db8f1c77542146cb81fb785d56e62dc00fc"},"cell_type":"markdown","source":"The above code depicts that none of the purchase amount values are integers. \n\nSome modification is required to make the data consistent which can then be consumed in the model for better predictions.\n\nThe below code snippet has been referenced from :\nhttps://www.kaggle.com/raddar/towards-de-anonymizing-the-data-some-insights"},{"metadata":{"trusted":true,"_uuid":"a3305fb81efc8b38a945ddbda2f931db93032bef"},"cell_type":"code","source":"df_new_merchant_transactions['purchase_amount_new'] = np.round(df_new_merchant_transactions['purchase_amount'] / 0.00150265118 + 497.06,8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c514d9aa3ae37daa3e9f3722a68ea937f41a5370"},"cell_type":"code","source":"#rounding off two decimal places\ndf_new_merchant_transactions['purchase_amount_new'] = np.round(df_new_merchant_transactions['purchase_amount'] / 0.00150265118 + 497.06,2)\n\ndf_new_merchant_transactions['purchase_amount_integer'] = df_new_merchant_transactions.purchase_amount_new.apply(lambda x: x == np.round(x))\nprint(df_new_merchant_transactions.groupby('purchase_amount_integer')['card_id'].count())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"ef11fded0b94f1d3f435d685e33c313929a1d7b6"},"cell_type":"code","source":"df_new_merchant_transactions.groupby('purchase_amount_new')['card_id'].count().reset_index(name='count').sort_values('count',ascending=False).head(100)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bb325a16227758333880b2d76497217c4a24147b"},"cell_type":"markdown","source":"Since the above data has some **integer values** and some  with \"** 0.55 and 0.99 **\" after the decimal places, henceforth the \"**purchase amount**\" can be considered as the money spent on the transaction\n"},{"metadata":{"_uuid":"6c5747bd4133420ec02a10268ef611ac903cb06d"},"cell_type":"markdown","source":"* Converting the categorical data to numerical values with the help of **get_dummies** function from the pandas library\n\n* Since there are only 2 levels in **authorized flag** and **category_1** , henceforth they have been mapped to binary values"},{"metadata":{"trusted":true,"_uuid":"47c7d52a6dc9fddf32f571e74916da801e72f295"},"cell_type":"code","source":"df_historical_transactions = pd.get_dummies(df_historical_transactions, columns=['category_2', 'category_3'])\ndf_historical_transactions['authorized_flag'] = df_historical_transactions['authorized_flag'].map({'Y': 1, 'N': 0})\ndf_historical_transactions['category_1'] = df_historical_transactions['category_1'].map({'Y': 1, 'N': 0})\n\ndf_historical_transactions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e06100c8b56a7d83ad83136239c70b7d93993ef1"},"cell_type":"code","source":"df_historical_transactions['purchase_amount_integer'] = df_historical_transactions.purchase_amount.apply(lambda x: x == np.round(x))\nprint(df_historical_transactions.groupby('purchase_amount_integer')['card_id'].count())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e16058769f38ff549ee72f1c3a62fa036e339837"},"cell_type":"code","source":"#df_historical_transactions['purchase_amount_new'] = np.round(df_historical_transactions['purchase_amount'] / 0.00150265118 + 497.06,8)\ndf_historical_transactions['purchase_amount_new'] = np.round(df_historical_transactions['purchase_amount'] / 0.00150265118 + 497.06,2)\ndf_historical_transactions['purchase_amount_integer'] = df_historical_transactions.purchase_amount_new.apply(lambda x: x == np.round(x))\nprint(df_historical_transactions.groupby('purchase_amount_integer')['card_id'].count())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"040c44cc7b7be5861f797dbb79a45b8766991406"},"cell_type":"code","source":"df_historical_transactions.groupby('purchase_amount_new')['card_id'].count().reset_index(name='count').sort_values('count',ascending=False).head(100)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b527389430b026a30604600f17935a4284975df2"},"cell_type":"markdown","source":"Since the above data too has some **integer values** and some  with \"**.50 and 0.99**\" after the decimal places, henceforth the \"**purchase amount**\" can be confirmed as the money spent on the transaction\n"},{"metadata":{"trusted":true,"_uuid":"b29ea76b6062fbf4340cc6f84b4b2ab236164b4b"},"cell_type":"code","source":"del df_historical_transactions['purchase_amount_integer']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef01c6a5d5873ef7814d0ed5803d3ed0e9e8e801"},"cell_type":"code","source":"df_historical_transactions.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d33ce41a7e790077747815cebec06dd545bed10"},"cell_type":"code","source":"####\n\ndef aggregate_transactions(trans, prefix):  \n    trans.loc[:, 'purchase_date'] = pd.DatetimeIndex(trans['purchase_date']).\\\n                                      astype(np.int64) * 1e-9\n    \n    agg_func = {\n        'authorized_flag': ['sum', 'mean'],\n        'category_1': ['mean'],\n        'category_2_1.0': ['mean'],\n        'category_2_2.0': ['mean'],\n        'category_2_3.0': ['mean'],\n        'category_2_4.0': ['mean'],\n        'category_2_5.0': ['mean'],\n        'category_3_A': ['mean'],\n        'category_3_B': ['mean'],\n        'category_3_C': ['mean'],\n        'merchant_id': ['nunique'],\n        'purchase_amount_new': ['sum', 'mean', 'max', 'min', 'std'],\n        'installments': ['sum', 'mean', 'max', 'min', 'std'],\n        'purchase_date': [np.ptp],\n        'month_lag': ['min', 'max']\n    }\n    agg_trans = trans.groupby(['card_id']).agg(agg_func)\n    agg_trans.columns = [prefix + '_'.join(col).strip() \n                           for col in agg_trans.columns.values]\n    agg_trans.reset_index(inplace=True)\n    \n    df = (trans.groupby('card_id')\n          .size()\n          .reset_index(name='{}transactions_count'.format(prefix)))\n    \n    agg_trans = pd.merge(df, agg_trans, on='card_id', how='left')\n    \n    return agg_trans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"709b97b711cd68221946fad436f5181bac62b13b"},"cell_type":"code","source":"###### Aggregate transaction function on the historical transaction ######\n\nimport gc\nmerch_hist = aggregate_transactions(df_historical_transactions, prefix='hist_')\n\ndel df_historical_transactions\ngc.collect()\ndf_train = pd.merge(df_train, merch_hist, on='card_id',how='left')\ndf_test = pd.merge(df_test, merch_hist, on='card_id',how='left')\n\ndel merch_hist\ngc.collect()\ndf_train.head()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e1eaf20a6401181371aa37c00bf2ef851b101439"},"cell_type":"markdown","source":"* Similar to the historical transaction dataset, the categorical variables from the new merchant dataset are also converted with the below code snippet with the help of **get_dummies** function from the pandas library\n\n* Since there are only 2 levels in **authorized flag** and **category_1** , henceforth they have been mapped to binary values"},{"metadata":{"trusted":true,"_uuid":"91fc0bc9bb78c59d4b67072273c58c80a9efbfdd"},"cell_type":"code","source":"df_new_merchant_transactions = pd.get_dummies(df_new_merchant_transactions, columns=['category_2', 'category_3'])\ndf_new_merchant_transactions['authorized_flag'] = df_new_merchant_transactions['authorized_flag'].map({'Y': 1, 'N': 0})\ndf_new_merchant_transactions['category_1'] = df_new_merchant_transactions['category_1'].map({'Y': 1, 'N': 0})\n\ndf_new_merchant_transactions.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"61c3dd35607a7c47c41afa61cb4d9769dd964aa2"},"cell_type":"code","source":"######Aggregate transaction function on the new merchant transaction ######\n\nmerch_new = aggregate_transactions(df_new_merchant_transactions, prefix='new_')\ndel df_new_merchant_transactions\ngc.collect()\ndf_train = pd.merge(df_train, merch_new, on='card_id',how='left')\ndf_test = pd.merge(df_test, merch_new, on='card_id',how='left')\n\ndel merch_new\ngc.collect()\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c21cb1f6e6bb7b63d5b56a42f7d389d99871af19"},"cell_type":"code","source":"###### Dropping 3 columns ######\n\ntarget = df_train['target']\ndrops = ['card_id', 'first_active_month', 'target']\nuse_cols = [c for c in df_train.columns if c not in drops]\nfeatures = list(df_train[use_cols].columns)\ndf_train[features].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"282f55c18c6addab664eaa3e084ed451a5ca6f8e"},"cell_type":"code","source":"\n\nprint(df_train[features].shape)\nprint(df_test[features].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b345f504bc78590d5f9b744cdb5c660ff312ded4"},"cell_type":"code","source":"df_train.corr()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"859bbca03cef0ad4929ee7c7ed055e4cff1f66eb"},"cell_type":"code","source":"#### Utilizing KFold Cross Validator ####\n\nfrom sklearn.model_selection import KFold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c390df670bdf85404511e24c21bc93dce03aa43c"},"cell_type":"code","source":"###LGB model detals for different parameters referenced from  the below notebook\n####https://www.kaggle.com/peterhurford/you-re-going-to-want-more-categories-lb-3-737\n\nparam = {'num_leaves': 50,\n         'min_data_in_leaf': 30, \n         'objective':'regression',\n         'max_depth': -1,\n         'learning_rate': 0.005,\n         \"boosting\": \"gbdt\",\n         \"feature_fraction\": 0.9,\n         \"bagging_freq\": 1,\n         \"bagging_fraction\": 0.9,\n         \"bagging_seed\": 11,\n         \"metric\": 'rmse',\n         \"lambda_l1\": 0.1,\n         \"verbosity\": -1}\n\n################# 5  fold cross validation #################\n\n\n### Splitting the dataset into 5 folds for validation\nfolds = KFold(n_splits=5, shuffle=True, random_state=15)\noof_5 = np.zeros(len(df_train))\npredictions = np.zeros(len(df_test))\n\n\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(df_train.values, target.values)):\n    print('-')\n    print(\"Fold {}\".format(fold_ + 1))\n    trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx])\n    val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx])\n\n    num_round = 10000\n    clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds=100)\n    oof_5[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)\n    predictions += clf.predict(df_test[features], num_iteration=clf.best_iteration) / folds.n_splits","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"09fe8137e8a5bceca746525c3b0373e850acc3d4"},"cell_type":"markdown","source":"**Calculating the mean squared error with the help light gradient booster model**"},{"metadata":{"trusted":true,"_uuid":"8fc4415470849eec828438b238243eee377e8459"},"cell_type":"code","source":"print('5 Fold Cross Validation - lgb - ', np.sqrt(mean_squared_error(oof_5, target)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"d7d9b4649fdd56d88d4735f9207753b8b3c15f50"},"cell_type":"code","source":"###LGB model detals for different parameters referenced from  the below notebook\n####https://www.kaggle.com/peterhurford/you-re-going-to-want-more-categories-lb-3-737\n\nparam = {'num_leaves': 50,\n         'min_data_in_leaf': 30, \n         'objective':'regression',\n         'max_depth': -1,\n         'learning_rate': 0.005,\n         \"boosting\": \"gbdt\",\n         \"feature_fraction\": 0.9,\n         \"bagging_freq\": 1,\n         \"bagging_fraction\": 0.9,\n         \"bagging_seed\": 11,\n         \"metric\": 'rmse',\n         \"lambda_l1\": 0.1,\n         \"verbosity\": -1}\n\n\n\n################# 10  fold cross validation #################\n\n\n\n### Splitting the dataset into 10 folds for validation\nfolds = KFold(n_splits=10, shuffle=True, random_state=15)\noof_10 = np.zeros(len(df_train))\npredictions = np.zeros(len(df_test))\n\n\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(df_train.values, target.values)):\n    print('-')\n    print(\"Fold {}\".format(fold_ + 1))\n    trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx])\n    val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx])\n\n    num_round = 10000\n    clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds=100)\n    oof_10[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)\n    predictions += clf.predict(df_test[features], num_iteration=clf.best_iteration) / folds.n_splits","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54899ee27ef2807d4111c11b9290c888ed6df041"},"cell_type":"code","source":"print('10 Fold Cross Validation - lgb - ', np.sqrt(mean_squared_error(oof_10, target)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c1bd3d0600a0a1c3a8501ae7a0b633c5d9ca8a9"},"cell_type":"code","source":"sub_df = pd.read_csv('../input/sample_submission.csv')\nsub_df[\"target\"] =  predictions #0.5 * predictions_lgb + 0.5 * predictions_xgb\nsub_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"425ce733fad63b005bbef571fbb7b5ae8a7e47bd"},"cell_type":"code","source":"sub_df.tail(5)","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}