{"cells":[{"metadata":{"_uuid":"1dee50e02ba20b8aabe9775e0f8b19c40e1bcff8"},"cell_type":"markdown","source":"This is my revised kernel inspired by Chau Ngoc Huynh's kernel (3.699).\n\n**New Update : Application of Interaction on Categorical Variables followed by Stacking using Bayesian Ridge on Stratified K Folds**"},{"metadata":{"_uuid":"a594abe9f66c5a090177dff966d6bedf63718a9c","trusted":false},"cell_type":"code","source":"# Import the necessary libraries\nimport numpy as np\nimport pandas as pd\nimport os\nimport time\nimport warnings\nimport gc\ngc.collect()\nimport os\nfrom six.moves import urllib\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport datetime\nwarnings.filterwarnings('ignore')\n%matplotlib inline\nplt.style.use('seaborn')\nfrom scipy.stats import norm, skew\nfrom sklearn.preprocessing import StandardScaler","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bfbbf30dcb4c354827aae6fff1aeaa632a9e874e","trusted":false},"cell_type":"code","source":"#Add All the Models Libraries\n\n# Scalers\nfrom sklearn.utils import shuffle\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.pipeline import FeatureUnion\n\n# Models\n\nfrom sklearn.linear_model import Lasso\nfrom sklearn.metrics import mean_squared_log_error,mean_squared_error, r2_score,mean_absolute_error\n\nfrom sklearn.model_selection import train_test_split #training and testing data split\nfrom sklearn import metrics #accuracy measure\nfrom sklearn.metrics import confusion_matrix #for confusion matrix\nfrom scipy.stats import reciprocal, uniform\n\nfrom sklearn.model_selection import StratifiedKFold, RepeatedKFold\n\n# Cross-validation\nfrom sklearn.model_selection import KFold #for K-fold cross validation\nfrom sklearn.model_selection import cross_val_score #score evaluation\nfrom sklearn.model_selection import cross_val_predict #prediction\nfrom sklearn.model_selection import cross_validate\n\n# GridSearchCV\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import RandomizedSearchCV\n\n#Common data processors\nfrom sklearn.preprocessing import OneHotEncoder, LabelEncoder\nfrom sklearn import feature_selection\nfrom sklearn import model_selection\nfrom sklearn import metrics\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.utils import check_array\nfrom scipy import sparse","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"798008f46ddb84ca5ec248990d56b64e5ec84e5a","trusted":false},"cell_type":"code","source":"# to make this notebook's output stable across runs\nnp.random.seed(123)\ngc.collect()\n# To plot pretty figures\n%matplotlib inline\nplt.rcParams['axes.labelsize'] = 14\nplt.rcParams['xtick.labelsize'] = 12\nplt.rcParams['ytick.labelsize'] = 12","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"47d5d544fa7e4bf9305814409c3cabc148b0169a","trusted":false},"cell_type":"code","source":"#Reduce the memory usage - Inspired by Panchajanya Banerjee\ndef reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2    \n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)    \n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"56152464b49828f018e84cf114f520239342270e","trusted":false},"cell_type":"code","source":"def one_hot_encoder(df, nan_as_category=True):\n    original_columns = df.columns.tolist()\n\n    categorical_columns = list(filter(lambda c: c in ['object'], df.dtypes))\n    df = pd.get_dummies(df, columns=categorical_columns, dummy_na=nan_as_category)\n\n    new_columns = list(filter(lambda c: c not in original_columns, df.columns))\n    return df, new_columns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"450f05f1bb8bd470972ec8a8f80a96f4b56402a7","scrolled":true,"trusted":false},"cell_type":"code","source":"train = reduce_mem_usage(pd.read_csv('../input/train.csv',parse_dates=[\"first_active_month\"]))\ntest = reduce_mem_usage(pd.read_csv('../input/test.csv', parse_dates=[\"first_active_month\"]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2352513f1d53487e789e41fbfa050199154f8916","trusted":false},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"78407176c3c40b9d15227165028d6b61f94f3ba3","trusted":false},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1858899e8d6a7a6808204567ded690e4a10810d7","trusted":false},"cell_type":"code","source":"plt.subplot(1, 2, 2)\nsns.distplot(train.target, kde=True, fit = norm)\nplt.xlabel('Customer Loyality (Skewed)')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f1dc9399de447ff96d064b085e6152a543ece278","trusted":false},"cell_type":"code","source":"# Remove the Outliers if any \ntrain['outliers'] = 0\ntrain.loc[train['target'] < -30, 'outliers'] = 1\ntrain['outliers'].value_counts()\n\nfor features in ['feature_1','feature_2','feature_3']:\n    order_label = train.groupby([features])['outliers'].mean()\n    train[features] = train[features].map(order_label)\n    test[features] =  test[features].map(order_label)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ce244a5a05abaa8c91ff3c73d8c5f86ba592c9a1","trusted":false},"cell_type":"code","source":"# Now extract the days and Qtr\ntrain['days'] = (datetime.date(2018, 2, 1) - train['first_active_month'].dt.date).dt.days\ntrain['quarter'] = train['first_active_month'].dt.quarter\n\nfeature_cols = ['feature_1', 'feature_2', 'feature_3']\nfor f in feature_cols:\n    train['days_' + f] = train['days'] * train[f]\n    train['days_' + f + '_ratio'] = train[f] / train['days']\n\ntest['days'] = (datetime.date(2018, 2, 1) - test['first_active_month'].dt.date).dt.days\ntest['quarter'] = test['first_active_month'].dt.quarter\n\nfeature_cols = ['feature_1', 'feature_2', 'feature_3']\nfor f in feature_cols:\n    test['days_' + f] = test['days'] * test[f]\n    test['days_' + f + '_ratio'] = test[f] / test['days']\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b54460353af274062f649afe26f034c4126a6a84"},"cell_type":"markdown","source":"Now we will try to extract more features from Transactions Data"},{"metadata":{"_uuid":"f9835dd6582d3e9c36954b498810929c8438d2d2","trusted":false},"cell_type":"code","source":"def aggregate_transaction_hist(trans, prefix):  \n        \n    agg_func = {\n        'purchase_amount' : ['sum','max','min','mean','var','skew'],\n        'installments' : ['sum','max','mean','var','skew'],\n        'purchase_date' : ['max','min'],\n        'month_lag' : ['max','min','mean','var','skew'],\n        'month_diff' : ['max','min','mean','var','skew'],\n        'weekend' : ['sum', 'mean'],\n        'weekday' : ['sum', 'mean'],\n        'authorized_flag': ['sum', 'mean'],\n        'category_1': ['sum','mean', 'max','min'],\n        'card_id' : ['size','count'],\n        'month': ['nunique', 'mean', 'min', 'max'],\n        'hour': ['nunique', 'mean', 'min', 'max'],\n        'weekofyear': ['nunique', 'mean', 'min', 'max'],\n        'day': ['nunique', 'mean', 'min', 'max'],\n        'subsector_id': ['nunique'],\n        'merchant_category_id' : ['nunique'],\n        'price' :['sum','mean','max','min','var'],\n        'duration' : ['mean','min','max','var','skew'],\n        'amount_month_ratio':['mean','min','max','var','skew']\n        \n    }\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":{"_uuid":"06a04f3393a83cb2687b76deba417012e18ef606","scrolled":true,"trusted":false},"cell_type":"code","source":"transactions = reduce_mem_usage(pd.read_csv('../input/historical_transactions.csv'))\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1decf075c9422fe20823691972f055b8a1c84f50","scrolled":true,"trusted":false},"cell_type":"code","source":"#impute missing values - This is now excluded.\ntransactions['category_2'] = transactions['category_2'].fillna(1.0,inplace=True)\ntransactions['category_3'] = transactions['category_3'].fillna('A',inplace=True)\ntransactions['merchant_id'] = transactions['merchant_id'].fillna('M_ID_00a6ca8a8a',inplace=True)\ntransactions['installments'].replace(-1, np.nan,inplace=True)\ntransactions['installments'].replace(999, np.nan,inplace=True)\ntransactions['purchase_amount'] = transactions['purchase_amount'].apply(lambda x: min(x, 0.8))\n\n#Feature Engineering - Adding new features inspired by Chau's first kernel\ntransactions['authorized_flag'] = transactions['authorized_flag'].map({'Y': 1, 'N': 0})\ntransactions['category_1'] = transactions['category_1'].map({'Y': 1, 'N': 0})\ntransactions['category_3'] = transactions['category_3'].map({'A':0, 'B':1, 'C':2})\n\ntransactions['purchase_date'] = pd.to_datetime(transactions['purchase_date'])\ntransactions['weekofyear'] = transactions['purchase_date'].dt.weekofyear\ntransactions['month'] = transactions['purchase_date'].dt.month\ntransactions['day'] = transactions['purchase_date'].dt.day\ntransactions['weekday'] = transactions.purchase_date.dt.weekday\ntransactions['weekend'] = (transactions.purchase_date.dt.weekday >=5).astype(int)\ntransactions['hour'] = transactions['purchase_date'].dt.hour \ntransactions['month_diff'] = ((datetime.datetime.today() - transactions['purchase_date']).dt.days)//30\ntransactions['month_diff'] += transactions['month_lag']\n\n# additional features\ntransactions['duration'] = transactions['purchase_amount']*transactions['month_diff']\ntransactions['amount_month_ratio'] = transactions['purchase_amount']/transactions['month_diff']\ntransactions['price'] = transactions['purchase_amount'] / transactions['installments']\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"93a165f209a8853e4c8e1f624becd0506e6fb0f1","trusted":false},"cell_type":"code","source":"agg_func = {\n        'mean': ['mean'],\n    }\nfor col in ['category_2','category_3']:\n    transactions[col+'_mean'] = transactions['purchase_amount'].groupby(transactions[col]).agg('mean')\n    transactions[col+'_max'] = transactions['purchase_amount'].groupby(transactions[col]).agg('max')\n    transactions[col+'_min'] = transactions['purchase_amount'].groupby(transactions[col]).agg('min')\n    transactions[col+'_sum'] = transactions['purchase_amount'].groupby(transactions[col]).agg('sum')\n    agg_func[col+'_mean'] = ['mean']\n    \ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95128e635d5c0ad26b170652971aca4fc9c41ad1","scrolled":false,"trusted":false},"cell_type":"code","source":"merge_trans = aggregate_transaction_hist(transactions, prefix='hist_')\ndel transactions\ngc.collect()\ntrain = pd.merge(train, merge_trans, on='card_id',how='left')\ntest = pd.merge(test, merge_trans, on='card_id',how='left')\ndel merge_trans\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a3e6c6e3dec6f2153d02a684fb7ddbbace4c5550","trusted":false},"cell_type":"code","source":"#Feature Engineering - Adding new features inspired by Chau's first kernel\ntrain['hist_purchase_date_max'] = pd.to_datetime(train['hist_purchase_date_max'])\ntrain['hist_purchase_date_min'] = pd.to_datetime(train['hist_purchase_date_min'])\ntrain['hist_purchase_date_diff'] = (train['hist_purchase_date_max'] - train['hist_purchase_date_min']).dt.days\ntrain['hist_purchase_date_average'] = train['hist_purchase_date_diff']/train['hist_card_id_size']\ntrain['hist_purchase_date_uptonow'] = (datetime.datetime.today() - train['hist_purchase_date_max']).dt.days\ntrain['hist_purchase_date_uptomin'] = (datetime.datetime.today() - train['hist_purchase_date_min']).dt.days\ntrain['hist_first_buy'] = (train['hist_purchase_date_min'] - train['first_active_month']).dt.days\ntrain['hist_last_buy'] = (train['hist_purchase_date_max'] - train['first_active_month']).dt.days\n\nfor feature in ['hist_purchase_date_max','hist_purchase_date_min']:\n    train[feature] = train[feature].astype(np.int64) * 1e-9\ngc.collect()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"370757bfba37b643f463f385d9e878ff27e320bc","trusted":false},"cell_type":"code","source":"#Feature Engineering - Adding new features inspired by Chau's first kernel\ntest['hist_purchase_date_max'] = pd.to_datetime(test['hist_purchase_date_max'])\ntest['hist_purchase_date_min'] = pd.to_datetime(test['hist_purchase_date_min'])\ntest['hist_purchase_date_diff'] = (test['hist_purchase_date_max'] - test['hist_purchase_date_min']).dt.days\ntest['hist_purchase_date_average'] = test['hist_purchase_date_diff']/test['hist_card_id_size']\ntest['hist_purchase_date_uptonow'] = (datetime.datetime.today() - test['hist_purchase_date_max']).dt.days\ntest['hist_purchase_date_uptomin'] = (datetime.datetime.today() - test['hist_purchase_date_min']).dt.days\n\ntest['hist_first_buy'] = (test['hist_purchase_date_min'] - test['first_active_month']).dt.days\ntest['hist_last_buy'] = (test['hist_purchase_date_max'] - test['first_active_month']).dt.days\n\nfor feature in ['hist_purchase_date_max','hist_purchase_date_min']:\n    test[feature] = test[feature].astype(np.int64) * 1e-9\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0c374b5e1327cd726ddfb85f7d1afceb2fd9c5df","scrolled":true,"trusted":false},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"88381a0c6e50924860a133e7d61838c86ee1eab1","trusted":false},"cell_type":"code","source":"# Taking Reference from Other Kernels\ndef aggregate_transaction_new(trans, prefix):  \n        \n    agg_func = {\n        'purchase_amount' : ['sum','max','min','mean','var','skew'],\n        'installments' : ['sum','max','mean','var','skew'],\n        'purchase_date' : ['max','min'],\n        'month_lag' : ['max','min','mean','var','skew'],\n        'month_diff' : ['max','min','mean','var','skew'],\n        'weekend' : ['sum', 'mean'],\n        'weekday' : ['sum', 'mean'],\n        'authorized_flag': ['sum', 'mean'],\n        'category_1': ['sum','mean', 'max','min'],\n        'card_id' : ['size','count'],\n        'month': ['nunique', 'mean', 'min', 'max'],\n        'hour': ['nunique', 'mean', 'min', 'max'],\n        'weekofyear': ['nunique', 'mean', 'min', 'max'],\n        'day': ['nunique', 'mean', 'min', 'max'],\n        'subsector_id': ['nunique'],\n        'merchant_category_id' : ['nunique'],\n        'price' :['sum','mean','max','min','var'],\n        'duration' : ['mean','min','max','var','skew'],\n        'amount_month_ratio':['mean','min','max','var','skew']\n    }\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":{"_uuid":"9c653335214ef763a6d9e13de46db5800a75c5a3","trusted":false},"cell_type":"code","source":"# Now extract the data from the new transactions\nnew_transactions = reduce_mem_usage(pd.read_csv('../input/new_merchant_transactions.csv'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fd7fc65f351e45fdd1e8a43b730b8ed50b2e4456","scrolled":false,"trusted":false},"cell_type":"code","source":"#impute missing values\nnew_transactions['category_2'] = new_transactions['category_2'].fillna(1.0,inplace=True)\nnew_transactions['category_3'] = new_transactions['category_3'].fillna('A',inplace=True)\nnew_transactions['merchant_id'] = new_transactions['merchant_id'].fillna('M_ID_00a6ca8a8a',inplace=True)\nnew_transactions['installments'].replace(-1, np.nan,inplace=True)\nnew_transactions['installments'].replace(999, np.nan,inplace=True)\nnew_transactions['purchase_amount'] = new_transactions['purchase_amount'].apply(lambda x: min(x, 0.8))\n\n#Feature Engineering - Adding new features inspired by Chau's first kernel\nnew_transactions['authorized_flag'] = new_transactions['authorized_flag'].map({'Y': 1, 'N': 0})\nnew_transactions['category_1'] = new_transactions['category_1'].map({'Y': 1, 'N': 0})\nnew_transactions['category_3'] = new_transactions['category_3'].map({'A':0, 'B':1, 'C':2}) \n\nnew_transactions['purchase_date'] = pd.to_datetime(new_transactions['purchase_date'])\nnew_transactions['month'] = new_transactions['purchase_date'].dt.month\nnew_transactions['weekofyear'] = new_transactions['purchase_date'].dt.weekofyear\nnew_transactions['day'] = new_transactions['purchase_date'].dt.day\nnew_transactions['weekday'] = new_transactions.purchase_date.dt.weekday\nnew_transactions['weekend'] = (new_transactions.purchase_date.dt.weekday >=5).astype(int)\nnew_transactions['hour'] = new_transactions['purchase_date'].dt.hour \nnew_transactions['month_diff'] = ((datetime.datetime.today() - new_transactions['purchase_date']).dt.days)//30\nnew_transactions['month_diff'] += new_transactions['month_lag']\n\ngc.collect()\n\n# additional features\nnew_transactions['duration'] = new_transactions['purchase_amount']*new_transactions['month_diff']\nnew_transactions['amount_month_ratio'] = new_transactions['purchase_amount']/new_transactions['month_diff']\nnew_transactions['price'] = new_transactions['purchase_amount'] / new_transactions['installments']\n\naggs = {\n        'mean': ['mean'],\n    }\n\nfor col in ['category_2','category_3']:\n    new_transactions[col+'_mean'] = new_transactions['purchase_amount'].groupby(new_transactions[col]).agg('mean')\n    new_transactions[col+'_max'] = new_transactions['purchase_amount'].groupby(new_transactions[col]).agg('max')\n    new_transactions[col+'_min'] = new_transactions['purchase_amount'].groupby(new_transactions[col]).agg('min')\n    new_transactions[col+'_var'] = new_transactions['purchase_amount'].groupby(new_transactions[col]).agg('var')\n    aggs[col+'_mean'] = ['mean']\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5ebf2d2ac4064c8e9a008f555c7f902ca0fdf853","scrolled":false,"trusted":false},"cell_type":"code","source":"merge_new = aggregate_transaction_new(new_transactions, prefix='new_')\ndel new_transactions\ngc.collect()\n\ntrain = pd.merge(train, merge_new, on='card_id',how='left')\ntest = pd.merge(test, merge_new, on='card_id',how='left')\ndel merge_new\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4741d24d2c897ebd286807d0d3bd658800e316ca","trusted":false},"cell_type":"code","source":"#Feature Engineering - Adding new features inspired by Chau's first kernel\ntrain['new_purchase_date_max'] = pd.to_datetime(train['new_purchase_date_max'])\ntrain['new_purchase_date_min'] = pd.to_datetime(train['new_purchase_date_min'])\ntrain['new_purchase_date_diff'] = (train['new_purchase_date_max'] - train['new_purchase_date_min']).dt.days\ntrain['new_purchase_date_average'] = train['new_purchase_date_diff']/train['new_card_id_size']\ntrain['new_purchase_date_uptonow'] = (datetime.datetime.today() - train['new_purchase_date_max']).dt.days\ntrain['new_purchase_date_uptomin'] = (datetime.datetime.today() - train['new_purchase_date_min']).dt.days\ntrain['new_first_buy'] = (train['new_purchase_date_min'] - train['first_active_month']).dt.days\ntrain['new_last_buy'] = (train['new_purchase_date_max'] - train['first_active_month']).dt.days\nfor feature in ['new_purchase_date_max','new_purchase_date_min']:\n    train[feature] = train[feature].astype(np.int64) * 1e-9\n\n#Feature Engineering - Adding new features inspired by Chau's first kernel\ntest['new_purchase_date_max'] = pd.to_datetime(test['new_purchase_date_max'])\ntest['new_purchase_date_min'] = pd.to_datetime(test['new_purchase_date_min'])\ntest['new_purchase_date_diff'] = (test['new_purchase_date_max'] - test['new_purchase_date_min']).dt.days\ntest['new_purchase_date_average'] = test['new_purchase_date_diff']/test['new_card_id_size']\ntest['new_purchase_date_uptonow'] = (datetime.datetime.today() - test['new_purchase_date_max']).dt.days\ntest['new_purchase_date_uptomin'] = (datetime.datetime.today() - test['new_purchase_date_min']).dt.days\ntest['new_first_buy'] = (test['new_purchase_date_min'] - test['first_active_month']).dt.days\ntest['new_last_buy'] = (test['new_purchase_date_max'] - test['first_active_month']).dt.days\n\nfor feature in ['new_purchase_date_max','new_purchase_date_min']:\n    test[feature] = test[feature].astype(np.int64) * 1e-9\n    \ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"12b0f46661a53ead1c430acc0ec0a83eee4d3b0f","trusted":false},"cell_type":"code","source":"#NEW Features referred from https://www.kaggle.com/mfjwr1/simple-lightgbm-without-blending\ntrain['card_id_total'] = train['new_card_id_size']+train['hist_card_id_size']\ntrain['card_id_cnt_total'] = train['new_card_id_count']+train['hist_card_id_count']\ntrain['card_id_cnt_ratio'] = train['new_card_id_count']/train['hist_card_id_count']\ntrain['purchase_amount_total'] = train['new_purchase_amount_sum']+train['hist_purchase_amount_sum']\ntrain['purchase_amount_mean'] = train['new_purchase_amount_mean']+train['hist_purchase_amount_mean']\ntrain['purchase_amount_max'] = train['new_purchase_amount_max']+train['hist_purchase_amount_max']\ntrain['purchase_amount_min'] = train['new_purchase_amount_min']+train['hist_purchase_amount_min']\ntrain['purchase_amount_ratio'] = train['new_purchase_amount_sum']/train['hist_purchase_amount_sum']\ntrain['month_diff_mean'] = train['new_month_diff_mean']+train['hist_month_diff_mean']\ntrain['month_diff_ratio'] = train['new_month_diff_mean']/train['hist_month_diff_mean']\ntrain['month_lag_mean'] = train['new_month_lag_mean']+train['hist_month_lag_mean']\ntrain['month_lag_max'] = train['new_month_lag_max']+train['hist_month_lag_max']\ntrain['month_lag_min'] = train['new_month_lag_min']+train['hist_month_lag_min']\ntrain['category_1_mean'] = train['new_category_1_mean']+train['hist_category_1_mean']\ntrain['installments_total'] = train['new_installments_sum']+train['hist_installments_sum']\ntrain['installments_mean'] = train['new_installments_mean']+train['hist_installments_mean']\ntrain['installments_max'] = train['new_installments_max']+train['hist_installments_max']\ntrain['installments_ratio'] = train['new_installments_sum']/train['hist_installments_sum']\ntrain['price_total'] = train['purchase_amount_total'] / train['installments_total']\ntrain['price_mean'] = train['purchase_amount_mean'] / train['installments_mean']\ntrain['price_max'] = train['purchase_amount_max'] / train['installments_max']\ntrain['duration_mean'] = train['new_duration_mean']+train['hist_duration_mean']\ntrain['duration_min'] = train['new_duration_min']+train['hist_duration_min']\ntrain['duration_max'] = train['new_duration_max']+train['hist_duration_max']\ntrain['amount_month_ratio_mean']=train['new_amount_month_ratio_mean']+train['hist_amount_month_ratio_mean']\ntrain['amount_month_ratio_min']=train['new_amount_month_ratio_min']+train['hist_amount_month_ratio_min']\ntrain['amount_month_ratio_max']=train['new_amount_month_ratio_max']+train['hist_amount_month_ratio_max']\ntrain['new_CLV'] = train['new_card_id_count'] * train['new_purchase_amount_sum'] / train['new_month_diff_mean']\ntrain['hist_CLV'] = train['hist_card_id_count'] * train['hist_purchase_amount_sum'] / train['hist_month_diff_mean']\ntrain['CLV_ratio'] = train['new_CLV'] / train['hist_CLV']\n\ntest['card_id_total'] = test['new_card_id_size']+test['hist_card_id_size']\ntest['card_id_cnt_total'] = test['new_card_id_count']+test['hist_card_id_count']\ntest['card_id_cnt_ratio'] = test['new_card_id_count']/test['hist_card_id_count']\ntest['purchase_amount_total'] = test['new_purchase_amount_sum']+test['hist_purchase_amount_sum']\ntest['purchase_amount_mean'] = test['new_purchase_amount_mean']+test['hist_purchase_amount_mean']\ntest['purchase_amount_max'] = test['new_purchase_amount_max']+test['hist_purchase_amount_max']\ntest['purchase_amount_min'] = test['new_purchase_amount_min']+test['hist_purchase_amount_min']\ntest['purchase_amount_ratio'] = test['new_purchase_amount_sum']/test['hist_purchase_amount_sum']\ntest['month_diff_mean'] = test['new_month_diff_mean']+test['hist_month_diff_mean']\ntest['month_diff_ratio'] = test['new_month_diff_mean']/test['hist_month_diff_mean']\ntest['month_lag_mean'] = test['new_month_lag_mean']+test['hist_month_lag_mean']\ntest['month_lag_max'] = test['new_month_lag_max']+test['hist_month_lag_max']\ntest['month_lag_min'] = test['new_month_lag_min']+test['hist_month_lag_min']\ntest['category_1_mean'] = test['new_category_1_mean']+test['hist_category_1_mean']\ntest['installments_total'] = test['new_installments_sum']+test['hist_installments_sum']\ntest['installments_mean'] = test['new_installments_mean']+test['hist_installments_mean']\ntest['installments_max'] = test['new_installments_max']+test['hist_installments_max']\ntest['installments_ratio'] = test['new_installments_sum']/test['hist_installments_sum']\ntest['price_total'] = test['purchase_amount_total'] / test['installments_total']\ntest['price_mean'] = test['purchase_amount_mean'] / test['installments_mean']\ntest['price_max'] = test['purchase_amount_max'] / test['installments_max']\ntest['duration_mean'] = test['new_duration_mean']+test['hist_duration_mean']\ntest['duration_min'] = test['new_duration_min']+test['hist_duration_min']\ntest['duration_max'] = test['new_duration_max']+test['hist_duration_max']\ntest['amount_month_ratio_mean']=test['new_amount_month_ratio_mean']+test['hist_amount_month_ratio_mean']\ntest['amount_month_ratio_min']=test['new_amount_month_ratio_min']+test['hist_amount_month_ratio_min']\ntest['amount_month_ratio_max']=test['new_amount_month_ratio_max']+test['hist_amount_month_ratio_max']\ntest['new_CLV'] = test['new_card_id_count'] * test['new_purchase_amount_sum'] / test['new_month_diff_mean']\ntest['hist_CLV'] = test['hist_card_id_count'] * test['hist_purchase_amount_sum'] / test['hist_month_diff_mean']\ntest['CLV_ratio'] = test['new_CLV'] / test['hist_CLV']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e977529f1ee19cf2b79c1a08fbf65a10bc047f14","trusted":false},"cell_type":"code","source":"train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b9aa541a016e5f0c3c66d9eebd30e6c713711d6a","trusted":false},"cell_type":"code","source":"train = train.drop(['card_id', 'first_active_month'], axis = 1)\ntest = test.drop(['card_id', 'first_active_month'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8323aef4eb43a9821d808578fa00514fc1ea063","trusted":false},"cell_type":"code","source":"# Get the X and Y\ndf_train_columns = [c for c in train.columns if c not in ['first_active_month', 'target', 'card_id', 'outliers',\n                  'hist_purchase_date_max', 'hist_purchase_date_min', 'hist_card_id_size',\n                  'new_purchase_date_max', 'new_purchase_date_min', 'new_card_id_size']] \ntarget = train['target']\ndel train['target']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1067b18790d511ed78b7ad1896bba120dcdf2990","trusted":false},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dac5e3fdfb1731d5b883ab2268f386cf61a869f7"},"cell_type":"markdown","source":"Apply Light GBM Modelling Technique with Stratified K Folds enumerated on training set and outliers "},{"metadata":{"_uuid":"29d413b0cf270375808c3424410e71e07f71e15e","scrolled":true,"trusted":false},"cell_type":"code","source":"# Change in Hyper Parameters using kernel : https://www.kaggle.com/mfjwr1/simple-lightgbm-without-blending/output\nimport lightgbm as lgb\n\nfolds = StratifiedKFold(n_splits=5, shuffle = True, random_state=4950)\noof = np.zeros(len(train))\npredictions = np.zeros(len(test))\nfeature_importance_df = pd.DataFrame()\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train.values, train['outliers'].values)):\n    \n    print(\"fold {}\".format(fold_))\n    trn_data = lgb.Dataset(train.iloc[trn_idx][df_train_columns], label=target.iloc[trn_idx])\n    val_data = lgb.Dataset(train.iloc[val_idx][df_train_columns], label=target.iloc[val_idx])\n\n    param ={\n                'task': 'train',\n                'boosting': 'goss',\n                'objective': 'regression',\n                'metric': 'rmse',\n                'learning_rate': 0.01,\n                'subsample': 0.9855232997390695,\n                'max_depth': 7,\n                'top_rate': 0.9064148448434349,\n                'num_leaves': 63,\n                'min_child_weight': 41.9612869171337,\n                'other_rate': 0.0721768246018207,\n                'reg_alpha': 9.677537745007898,\n                'colsample_bytree': 0.5665320670155495,\n                'min_split_gain': 9.820197773625843,\n                'reg_lambda': 8.2532317400459,\n                'min_data_in_leaf': 21,\n                'verbose': -1,\n                'seed':int(2**fold_),\n                'bagging_seed':int(2**fold_),\n                'drop_seed':int(2**fold_)\n                }\n    \n    num_round = 10000\n    clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=-1, early_stopping_rounds = 200)\n    oof[val_idx] = clf.predict(train.iloc[val_idx][df_train_columns], num_iteration=clf.best_iteration)\n    \n    fold_importance_df = pd.DataFrame()\n    fold_importance_df[\"Feature\"] = df_train_columns\n    fold_importance_df[\"importance\"] = clf.feature_importance()\n    fold_importance_df[\"fold\"] = fold_ + 1\n    feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)\n    \n    predictions += clf.predict(test[df_train_columns], num_iteration=clf.best_iteration) / folds.n_splits\n\nnp.sqrt(mean_squared_error(oof, target))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"04426bf9e197bbef4aa7e13fa4dfeef14fad7fdc"},"cell_type":"markdown","source":"Feature Importance - Stratified K Folds Technique"},{"metadata":{"_uuid":"f594383f5fb83246203ce54b4892592673e699ce","scrolled":true,"trusted":false},"cell_type":"code","source":"cols = (feature_importance_df[[\"Feature\", \"importance\"]]\n        .groupby(\"Feature\")\n        .mean()\n        .sort_values(by=\"importance\", ascending=False)[:1000].index)\n\nbest_features = feature_importance_df.loc[feature_importance_df.Feature.isin(cols)]\n\nplt.figure(figsize=(14,25))\nsns.barplot(x=\"importance\",\n            y=\"Feature\",\n            data=best_features.sort_values(by=\"importance\",\n                                           ascending=False))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()\nplt.savefig('lgbm_importances.png')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4e4259f0bc335d4060f6d28ddc044617c9c7f94d","trusted":false},"cell_type":"code","source":"features = [c for c in train.columns if c not in ['first_active_month', 'target', 'card_id', 'outliers',\n                  'hist_purchase_date_max', 'hist_purchase_date_min', 'hist_card_id_size',\n                  'new_purchase_date_max', 'new_purchase_date_min', 'new_card_id_size']]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4b3d96a1354b5f37e7ba1a845428948cf7f8d1f9"},"cell_type":"markdown","source":"Apply Light GBM Modelling Technique with Repeated K Folds enumerated on training set and Target "},{"metadata":{"_uuid":"85f3d1c010115cd099839c8434fe37a9429d8696","scrolled":false,"trusted":false},"cell_type":"code","source":"folds = RepeatedKFold(n_splits=5, n_repeats=2, random_state=4950)\noof_2 = np.zeros(len(train))\npredictions_2 = np.zeros(len(test))\nfeature_importance_df_2 = pd.DataFrame()\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train.values, target.values)):\n    \n    print(\"fold {}\".format(fold_))\n    trn_data = lgb.Dataset(train.iloc[trn_idx][features], label=target.iloc[trn_idx])\n    val_data = lgb.Dataset(train.iloc[val_idx][features], label=target.iloc[val_idx])\n    \n    param ={\n                'task': 'train',\n                'boosting': 'goss',\n                'objective': 'regression',\n                'metric': 'rmse',\n                'learning_rate': 0.01,\n                'subsample': 0.9855232997390695,\n                'max_depth': 7,\n                'top_rate': 0.9064148448434349,\n                'num_leaves': 63,\n                'min_child_weight': 41.9612869171337,\n                'other_rate': 0.0721768246018207,\n                'reg_alpha': 9.677537745007898,\n                'colsample_bytree': 0.5665320670155495,\n                'min_split_gain': 9.820197773625843,\n                'reg_lambda': 8.2532317400459,\n                'min_data_in_leaf': 21,\n                'verbose': -1,\n                'seed':int(2**fold_),\n                'bagging_seed':int(2**fold_),\n                'drop_seed':int(2**fold_)\n                }\n        \n    num_round = 10000\n    clf_r = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=-1, early_stopping_rounds = 200)\n    oof_2[val_idx] = clf_r.predict(train.iloc[val_idx][features], num_iteration=clf_r.best_iteration)\n    \n    fold_importance_df = pd.DataFrame()\n    fold_importance_df[\"Feature\"] = features\n    fold_importance_df[\"importance\"] = clf_r.feature_importance()\n    fold_importance_df[\"fold\"] = fold_ + 1\n    feature_importance_df_2 = pd.concat([feature_importance_df_2, fold_importance_df], axis=0)\n    \n    predictions_2 += clf_r.predict(test[features], num_iteration=clf_r.best_iteration) / (5 * 2)\n\nprint(\"CV score: {:<8.5f}\".format(mean_squared_error(oof_2, target)**0.5))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e68611a4c9f1f1a042ddc5eedcfad33d89575b13"},"cell_type":"markdown","source":"Feature Importance - Repeated K Folds"},{"metadata":{"_uuid":"5e3ecdfbeb0d615801156a32ff14eade7e994950","scrolled":true,"trusted":false},"cell_type":"code","source":"cols = (feature_importance_df_2[[\"Feature\", \"importance\"]]\n        .groupby(\"Feature\")\n        .mean()\n        .sort_values(by=\"importance\", ascending=False)[:1000].index)\n\nbest_features = feature_importance_df_2.loc[feature_importance_df_2.Feature.isin(cols)]\n\nplt.figure(figsize=(14,25))\nsns.barplot(x=\"importance\",\n            y=\"Feature\",\n            data=best_features.sort_values(by=\"importance\",\n                                           ascending=False))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()\nplt.savefig('lgbm_importances.png')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d83f9093cd718a2e72b3d9b84a02a8e72156c625"},"cell_type":"markdown","source":"Stacking the models together."},{"metadata":{"_uuid":"a65e5bb380bc06a3dcac649cc1168457d3c156a3","trusted":false},"cell_type":"code","source":"from sklearn.linear_model import BayesianRidge\n\ntrain_stack = np.vstack([oof,oof_2]).transpose()\ntest_stack = np.vstack([predictions, predictions_2]).transpose()\n\nfolds_stack = RepeatedKFold(n_splits=5, n_repeats=1, random_state=4590)\noof_stack = np.zeros(train_stack.shape[0])\npredictions_3 = np.zeros(test_stack.shape[0])\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds_stack.split(train_stack,target)):\n    print(\"fold {}\".format(fold_))\n    trn_data, trn_y = train_stack[trn_idx], target.iloc[trn_idx].values\n    val_data, val_y = train_stack[val_idx], target.iloc[val_idx].values\n    \n    clf_3 = BayesianRidge()\n    clf_3.fit(trn_data, trn_y)\n    \n    oof_stack[val_idx] = clf_3.predict(val_data)\n    predictions_3 += clf_3.predict(test_stack) / 5\n    \nnp.sqrt(mean_squared_error(target.values, oof_stack))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8b3c5b718779a1d5729e89a5341258c44de24e2"},"cell_type":"markdown","source":"Final Predictions and Submission File."},{"metadata":{"_uuid":"e87474495a3b14a96345eb2773fce5564268d9ac","trusted":false},"cell_type":"code","source":"sample_submission = pd.read_csv('../input/sample_submission.csv')\nsample_submission['target'] = predictions_3\nsample_submission.to_csv('submission_ashish_lgbm.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}