{"cells":[{"metadata":{"trusted":true,"_uuid":"ebd4d183dd9891d25fc9a0c9390c442a4e9c6668"},"cell_type":"code","source":"import matplotlib.pyplot as plt \nimport time\nimport numpy as np\nimport seaborn as sns \nimport time\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \nimport xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import make_scorer, mean_squared_error\nfrom sklearn.model_selection import cross_val_predict , KFold, cross_val_score\nfrom mlxtend.plotting import plot_sequential_feature_selection as plot_sfs\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom mlxtend.feature_selection import SequentialFeatureSelector as SFS\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.ensemble import AdaBoostRegressor\nfrom math import log1p\nimport numpy as np\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn import datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_selection import SelectFromModel\nfrom sklearn.metrics import accuracy_score\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nfrom datetime import datetime\nimport os\nprint(os.listdir(\"../input\"))\nfrom xgboost import plot_importance\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74ae50a18cdd61afe48571cac69e504575b9a7e3"},"cell_type":"code","source":"#import sys\n#sys.path.append('../input/feature-selector/')\n#from feature_selector import FeatureSelector","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train= pd.read_csv(\"../input/train.csv\", parse_dates=[\"first_active_month\"])\ntest = pd.read_csv(\"../input/test.csv\", parse_dates=[\"first_active_month\"])\nprint(\"{} observations and {} features in train set.\".format(train.shape[0],train.shape[1]))\nprint(\"{} observations and {} features in test set.\".format(test.shape[0],test.shape[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f8cd7d489470a7d5b09b5c1a297d3d26343f36e"},"cell_type":"code","source":"print(len(list(test.keys())))\nprint(len(list(train.keys())))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"949e157f69af78ad94b3bda4b490709d2a5ebfd0"},"cell_type":"code","source":"history = pd.read_csv(\"../input/historical_transactions.csv\", low_memory=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f54ee15975c337851f2b3757eecb3854bdfe036"},"cell_type":"code","source":"#history.head(n=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24b785ae33038df7703040c13b54e6706df1c2ae"},"cell_type":"code","source":"#fig, ax = plt.subplots(figsize=(12, 3))\n#sns.boxplot(x='target', data=train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38e41b651c99ff61cf9b71b98645ddfc9438cafb"},"cell_type":"code","source":"#fig, ax = plt.subplots(figsize=(16, 5))\n#sns.distplot(train.target, ax=ax)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b4ec6f6a9d351f43c62fa03e177c4c30c7f2a18"},"cell_type":"code","source":"train['feature_1'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d83593f785e047296a6e20260f5493bc2a3e954"},"cell_type":"code","source":"def missing_data_function(frame):\n\n    total = frame.isnull().sum().sort_values(ascending=False)\n    percent = (frame.isnull().sum()*100 / frame.isnull().count()).sort_values(ascending=False)\n    missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\n    return missing_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b8501cf2c5112f1f83eedd7ff7011656fc02342"},"cell_type":"code","source":"def reduce_mem_usage_func(df):\n    \"\"\" Memory saving function credit to https://www.kaggle.com/gemartin/load-data-reduce-memory-usage\n        iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n\n    for col in df.columns:\n        col_type = df[col].dtype\n\n        if col_type != object:\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\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n    return df\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a97e5413728a0067896d8e42091f08f901a3c3d"},"cell_type":"code","source":"history=reduce_mem_usage_func(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39c85536ba2e105624fbd8c6b41b84b9755d93fa"},"cell_type":"code","source":"history.reset_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6fb3667b3da71953f09131874522202d0241dd1c"},"cell_type":"code","source":"new_transactions = pd.read_csv('../input/new_merchant_transactions.csv',  low_memory=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c0dc12635ece5c8659e46967ded8b80097a2595"},"cell_type":"code","source":"new_transactions.head(n=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a862a3fcaff400f3135dc8551a7a2193c88553f8"},"cell_type":"code","source":"new_transactions=reduce_mem_usage_func(new_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11463257b6fcd25ad1fae4def127ab944bc27668"},"cell_type":"code","source":"#time.sleep(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19e2c26d89213b569c769b2af2429547c404fc95"},"cell_type":"code","source":"new_transactions.reset_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"89bc21b491f248105c3ca14a2d46405ac1ff3ee5"},"cell_type":"code","source":"#history.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9fa1fa575e7e5d6168b3ffcd4266e214448744e"},"cell_type":"code","source":"def merge_train_test(train, test , df ):\n    \n    train=pd.merge(left=train , right=df, how = 'left', on ='card_id')\n    test=pd.merge(left=test , right=df, how = 'left', on ='card_id')\n    return train, test\n    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f7dfa8585cb284ec0bf7ddcc3b99e4fb01a522f6"},"cell_type":"code","source":"history['purchase_date'] = pd.to_datetime(history['purchase_date'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0bb7350c50b1ccfe2a16e1e38d28f4992efb1b12"},"cell_type":"code","source":"new_transactions['purchase_date'] = pd.to_datetime(new_transactions['purchase_date'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d19e7143d3f34eee9f9d3ba16d29fd19da0e16b5"},"cell_type":"code","source":"def features(df):\n    df['feature_sum'] = df['feature_1'] + df['feature_2'] + df['feature_3']\n    df['feature_mean'] = df['feature_sum']/3\n    df['feature_max'] = df[['feature_1', 'feature_2', 'feature_3']].max(axis=1)\n    df['feature_min'] = df[['feature_1', 'feature_2', 'feature_3']].min(axis=1)\n    df['feature_var'] = df[['feature_1', 'feature_2', 'feature_3']].std(axis=1)\n    return df \n\n\n\n    \n    \n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53f7088352bd421c2cf4ca9633192901fc25f2c2"},"cell_type":"code","source":"train=features(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2423b5a3cf2c6f3aef2ec17e55c16003bfa609d7"},"cell_type":"code","source":"test=features(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f9a7109ae6b9b34a71000fd663e4b1ed4daf3e2"},"cell_type":"code","source":"def deal_missing(df):\n    df['category_2'].fillna(1.0,inplace=True)\n    df['category_3'].fillna('A',inplace=True)\n    df['merchant_id'].fillna('M_ID_00a6ca8a8a',inplace=True)\n    df['installments'].replace(-1, np.nan,inplace=True)\n    df['installments'].replace(999, np.nan,inplace=True)\n    df['purchase_amount'] = df['purchase_amount'].apply(lambda x: min(x, 0.8))\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90bd947e6bc9d2e64ecaceabf13b95101289406e"},"cell_type":"code","source":"history=deal_missing(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8b73920fa379f6d8d7e8333aace6c50e0ee90903"},"cell_type":"code","source":"new_transactions=deal_missing(new_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9852c1ba03acb7982079273d0f8b11cc5c6dcfb6"},"cell_type":"code","source":"def mapping(df):\n    df['authorized_flag'] = df['authorized_flag'].map({'Y': 1, 'N': 0}).astype('int8')\n    df['category_1'] = df['category_1'].map({'Y': 1, 'N': 0}).astype('int8')\n    df['category_3'] = df['category_3'].map({'A':0, 'B':1, 'C':2}).astype('int8')\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63793901397b1d31febd845ac32d98960aaf31d7"},"cell_type":"code","source":"history=mapping(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52a89e5d07221ab058337aa8df33b1765d12a88b"},"cell_type":"code","source":"new_transactions=mapping(new_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73493e8477dc1ad257aac14e2b8fa3ff88fb08ad"},"cell_type":"code","source":"def new_features(df):\n    df['month'] = df['purchase_date'].dt.month.astype('int8')\n    df['day'] = df['purchase_date'].dt.day.astype('int8')\n    df['hour'] = df['purchase_date'].dt.hour.astype('int8')\n    df['weekofyear'] = df['purchase_date'].dt.weekofyear.astype('int8')\n    df['weekday'] = df['purchase_date'].dt.weekday.astype('int8')\n    df['weekend'] = (df['purchase_date'].dt.weekday >=5).astype('int8')\n    time.sleep(60)\n    df['price'] = df['purchase_amount'] / (1+df['installments'])\n    df['month_diff'] = (((datetime.today() - df['purchase_date']).dt.days)//30).astype('int8')\n    df['month_diff'] += history['month_lag']\n    time.sleep(60)\n    df['Christmas_Day_2017']=(pd.to_datetime('2017-12-25')-df['purchase_date']).dt.days.apply(lambda x: x if x > 0 and x < 100 else 0).astype('int8')\n    df['Mothers_Day_2017']=(pd.to_datetime('2017-06-04')-df['purchase_date']).dt.days.apply(lambda x: x if x > 0 and x < 100 else 0).astype('int8')\n    df['Valentine_Day_2017']=(pd.to_datetime('2017-06-12')-df['purchase_date']).dt.days.apply(lambda x: x if x > 0 and x < 100 else 0).astype('int8')\n    df['Black_Friday_2017']=(pd.to_datetime('2017-11-24') - df['purchase_date']).dt.days.apply(lambda x: x if x > 0 and x < 100 else 0).astype('int8')\n    df['Mothers_Day_2018']=(pd.to_datetime('2018-05-13')-df['purchase_date']).dt.days.apply(lambda x: x if x > 0 and x < 100 else 0).astype('int8')\n    time.sleep(60)\n    df['activity_100_days']=(df['purchase_date'].max()-df['purchase_date']).dt.days.apply(lambda x: 1 if x > 0 and x < 100 else 0).astype('int8')\n    df['duration'] = df['purchase_amount']*df['month_diff']\n    df['amount_month_ratio'] = df['purchase_amount']/df['month_diff']\n    df['price'] = df['purchase_amount'] / df['installments']\n\n    return df \n#","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7ba44d6b55411a2352be206de36e789f1fdb153"},"cell_type":"code","source":"history=new_features(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e67891bb33b9a703dbb306b73c3b8ba06bfb1700"},"cell_type":"code","source":"#history=reduce_mem_usage_func(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47311251254bbcb5d089eb530b3be60218a7a625"},"cell_type":"code","source":"history.head(n=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4bdee7c19f4462c0e178084127e13d467c1bc5c"},"cell_type":"code","source":"new_transactions=new_features(new_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b68492d6f2a397f95438a745337d9b4ac0a1e99e"},"cell_type":"code","source":"for col in ['category_2','category_3']:\n    history[col+'_mean'] = history.groupby([col])['purchase_amount'].transform('mean')\n    new_transactions[col+'_mean'] = new_transactions.groupby([col])['purchase_amount'].transform('mean')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8c86bdb4010ebde0c1540bff7514b3f452058b4"},"cell_type":"code","source":"def aggregation(frame,name):\n    agg_func = {\n        'subsector_id':['nunique']\n        ,'merchant_id':['nunique']\n        ,'merchant_category_id':['nunique']\n        ,'month':['nunique', 'mean', 'min', 'max']\n        ,'hour':['nunique', 'mean', 'min', 'max']\n        ,'weekofyear':['nunique', 'mean', 'min', 'max']\n        ,'weekday':['nunique', 'mean', 'min', 'max']\n        ,'day':['nunique', 'mean', 'min', 'max']\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':['mean','var','skew']\n        ,'weekend':['mean']\n        ,'month':['mean', 'min', 'max']\n        ,'weekday':['mean', 'min', 'max']\n        ,'category_1' :['mean','std']\n        ,'category_2': ['mean','std']\n        ,'category_3': ['mean','std']\n        ,'card_id':['size','count']\n        ,'price': ['mean','max','min','var']\n        ,'Christmas_Day_2017': ['mean']\n        ,'Black_Friday_2017': ['mean']\n        ,'Mothers_Day_2018':['mean']\n        ,'activity_100_days':['mean']\n        ,'category_2_mean':['mean']\n        ,'category_3_mean':['mean']\n        ,'duration' : ['mean','min','max','var','skew']\n        ,'amount_month_ratio': ['mean','min','max','var','skew']\n        }\n    agg_new_trans = frame.groupby(['card_id']).agg(agg_func)\n    agg_new_trans.columns = [str(name) + '_'.join(col).strip() \n                           for col in agg_new_trans.columns.values]\n    agg_new_trans.reset_index(inplace=True)\n    return agg_new_trans\n    \n    \n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0de3a711aedf8d165adcf90f95aea679f5b86e26"},"cell_type":"code","source":"time.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"afa79fdc6e3bba4514efa6d2f36ee7b85f31183b"},"cell_type":"code","source":"#df = aggregation(history,'history_')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"951fab8122b76202be59052086a2639f348b963c"},"cell_type":"code","source":"train , test = merge_train_test(train, test , aggregation(history,'history_') )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"685733392606e8ed213c798502eb1773f686b563"},"cell_type":"code","source":"time.sleep(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8949055c43bcdd95b539ab8cf71e719cc9ee44f4"},"cell_type":"code","source":"time.sleep(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a6c919c1a52f0244b3d2b355e37f48d24adc4ac"},"cell_type":"code","source":"train , test = merge_train_test(train, test ,aggregation(new_transactions,'new_') )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d2eb86f1031cc6af18df320c664338971b77f47"},"cell_type":"code","source":"new_transactions.head(n=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e048b3b68f7fa793f6b2e02d56d9c6145020dad"},"cell_type":"code","source":"new_transactions.keys()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fed5c17efdf379921416aeb772c024e68e65abb5"},"cell_type":"markdown","source":"number of transactions : to make the difference between new and old history "},{"metadata":{"trusted":true,"_uuid":"77ac2e1244606286ae6e01b119473bd9929e100c"},"cell_type":"markdown","source":"train=pd.merge(left=train , right=card_id_cnt, how = 'left', on ='card_id')\ntest=pd.merge(left=test , right=card_id_cnt, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"120876a06ab4f19a4eeba92b32a305b16609c3d4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e13c61f8b4c8588c77d356c187d22c22eb4dd70e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a1d4499d6cf1c2c0351c7dd0e7f07a7935df9a1"},"cell_type":"markdown","source":"subsector_id_cnt = history.groupby('card_id')['subsector_id'].nunique().reset_index(name='subsector_id_cnt')\nsubsector_id_cnt.columns = ['card_id','subsector_id_cnt']"},{"metadata":{"trusted":true,"_uuid":"2c78745c2898f0fe3c6704208ca2b406982dc4e8"},"cell_type":"markdown","source":"train=pd.merge(left=train , right=subsector_id_cnt, how = 'left', on ='card_id')\ntest=pd.merge(left=test , right=subsector_id_cnt, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"c0312408bc652560a7173c33b5eaec9ae5ea8436"},"cell_type":"markdown","source":"card_id_cnt_new = new_transactions.groupby('card_id')['city_id'].count().reset_index(name='purchase_cnt_new')\ncard_id_cnt_new.columns = ['card_id','purchase_cnt_new']"},{"metadata":{"trusted":true,"_uuid":"d136a3ba461811299e575fd4b0a004507abefdb7"},"cell_type":"markdown","source":"train=pd.merge(left=train , right=card_id_cnt_new, how = 'left', on ='card_id')\ntest=pd.merge(left=test , right=card_id_cnt_new, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"647a91fcdc54f00739ba1ad04d3c078645cc8287"},"cell_type":"code","source":"#train=train.drop(columns=['amount_mean_x', 'amount_std_x', 'amount_max_x','amount_min_x', 'amount_sum_x','amount_mean_y', 'amount_std_y', 'amount_max_y',\n                         # 'amount_min_y', 'amount_sum_y'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f41acb69dddb32fbb4745ab71be68926d4071200"},"cell_type":"code","source":"train.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d0f3a3305feaeb35a725b1fc97bc9fdf5b9c61a"},"cell_type":"markdown","source":"train['diff_purchase']=train['purchase_cnt']-train['purchase_cnt_new']\ntest['diff_purchase']=test['purchase_cnt']-test['purchase_cnt_new']"},{"metadata":{"trusted":true,"_uuid":"daac884733bf67b8286b18e0bc486fa3decf1167"},"cell_type":"markdown","source":"amount = history.groupby('card_id').agg({'purchase_amount':['mean','std','max','min','sum']})\namount.columns = ['amount_mean','amount_std','amount_max','amount_min', 'amount_sum']\namount = amount.reset_index()"},{"metadata":{"trusted":true,"_uuid":"5810d1853794799846a1e69f170ba78deff30953"},"cell_type":"markdown","source":"amount.head(n=10)"},{"metadata":{"trusted":true,"_uuid":"4e078150d1854582ea73c94080c0abd0c97f61fa"},"cell_type":"markdown","source":"train=pd.merge(left=train , right=amount, how = 'left', on ='card_id')\ntest=pd.merge(left=test , right=amount, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"206f371fde877516806785749588ec6876235a92","scrolled":true},"cell_type":"markdown","source":"history['month_diff'] = ((datetime.today() - history['purchase_date']).dt.days)//30\nhistory['month_diff'] += history['month_lag']"},{"metadata":{"trusted":true,"_uuid":"e7e05e3d356afa5ba3d195df5320b27a8a1f2b58"},"cell_type":"markdown","source":"month_diff_mean = history.groupby('card_id')['month_diff'].mean().reset_index(name='month_diff_mean')\n"},{"metadata":{"trusted":true,"_uuid":"56c2f580f56c8ca1ac403c798a8ad60332fedfdd"},"cell_type":"markdown","source":"month_diff_mean.head(n=10)"},{"metadata":{"trusted":true,"_uuid":"1fc9a9159e5f9d0e2ef2f983b4ce38f5d0fb8bee"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , month_diff_mean )"},{"metadata":{"trusted":true,"_uuid":"34061f64662c7a12fdf74b174ed7dfe89a3a3c49"},"cell_type":"markdown","source":"new_transactions['month_diff'] = ((datetime.today() - new_transactions['purchase_date']).dt.days)//30\nnew_transactions['month_diff'] += new_transactions['month_lag']\n"},{"metadata":{"trusted":true,"_uuid":"7ebcfa4b293324654dd920b45a2c69cfbb139fb5"},"cell_type":"markdown","source":"month_diff_mean_new = history.groupby('card_id')['month_diff'].mean().reset_index(name='month_diff_mean_new')"},{"metadata":{"trusted":true,"_uuid":"a03c9d31cd647a7e347ce36561af49909d79bc54"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test ,month_diff_mean_new )"},{"metadata":{"trusted":true,"_uuid":"832168b53db26c6f3fbdcf85b28bda0f47f12297"},"cell_type":"markdown","source":"train['diff_month_lag_mean']=train['month_diff_mean']-train['month_diff_mean_new']\ntest['diff_month_lag_mean']=test['month_diff_mean']-test['month_diff_mean_new']"},{"metadata":{"trusted":true,"_uuid":"bc9a120cbd9c3579572af413a0f9f9859f15be37"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73fc27f139bdbfa127109fdc011537115601fb03"},"cell_type":"markdown","source":"mode = lambda x: x.mode().values[0] if len(x) > 1 else x.values[0]"},{"metadata":{"trusted":true,"_uuid":"a44ce7c02feed185c7d80676506497778a5091e4"},"cell_type":"markdown","source":"city_cnt = history.groupby('card_id')['city_id'].nunique().reset_index(name='city_cnt')\n"},{"metadata":{"trusted":true,"_uuid":"30a19ef73b02c5d418781a5b78965ccf26ce3414"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , city_cnt )"},{"metadata":{"trusted":true,"_uuid":"fdc30a8f0dc3b5b9e628196c2ffb727a089b7630"},"cell_type":"code","source":"#istory['installments']=history['installments'].replace({-1:0,999:0}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e93344dec244187a259ab9e91a6f3bafc609ad53"},"cell_type":"markdown","source":"history=history.replace({'installments': {-1:0,999:0}})"},{"metadata":{"trusted":true,"_uuid":"98270e638b40590e0aa75dcc05158abdbfd50dd4"},"cell_type":"markdown","source":"new_transactions=new_transactions.replace({'installments': {-1:0,999:0}})"},{"metadata":{"trusted":true,"_uuid":"13d5e5bc29567637eae0c22dab97c02320f75fcd"},"cell_type":"markdown","source":"installments = history.groupby('card_id').agg({'installments':['mean','std','max','min']})\ninstallments.columns = ['installments_mean','installments_std','installments_max','installments_min']\ninstallments=installments.reset_index()"},{"metadata":{"trusted":true,"_uuid":"d5d118415a47ddabc090df0396636dab486adad4"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , installments )"},{"metadata":{"trusted":true,"_uuid":"62dee07b4e411efd14ff949e3265067a57b4e408"},"cell_type":"markdown","source":"missing_installments = history.groupby('card_id').agg({'missing_installments':['mean','sum']})\nmissing_installments.columns = ['missing_installments_mean','missing_installments_sum']\nmissing_installments=missing_installments.reset_index()"},{"metadata":{"trusted":true,"_uuid":"f347dc536a9e6fd1feb4f3ebfb63f3a16b176ca7"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , missing_installments )"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"893044b3c12873654b15471bcca8bb7dd1bd2b7e"},"cell_type":"markdown","source":"train.head(n=5)"},{"metadata":{"trusted":true,"_uuid":"c896010eb90b9c536de45874a990f84df420b2cc"},"cell_type":"markdown","source":"train=pd.merge(left=train , right=most_frequent_city, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"09ca9c027000d47aa052a7c85a315d32965383d2"},"cell_type":"markdown","source":"test=pd.merge(left=test , right=most_frequent_city, how = 'left', on ='card_id')"},{"metadata":{"trusted":true,"_uuid":"cad9db51f78421a53dbcd3e2c8ba3a0f7028ab2a"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , most_frequent_city_new )"},{"metadata":{"trusted":true,"_uuid":"9cde6a8481cf89afba1130af48d7bc094fe2c7bd"},"cell_type":"code","source":"def shopping_days(df, name):\n    days_of_shopping=df.groupby('card_id')['purchase_date'].max()-df.groupby('card_id')['purchase_date'].min()\n    days_of_shopping=days_of_shopping.reset_index()\n    days_of_shopping.columns = ['card_id',str(name)+'_'+'shopping_days']\n    days_of_shopping[str(name)+'_'+'shopping_days']=days_of_shopping[str(name)+'_'+'shopping_days'].dt.days\n    days_of_shopping.reset_index(inplace=True)\n    return days_of_shopping\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f8f7b3d635c4af69228cdbfe03fdc7e13d3bd7c"},"cell_type":"code","source":"history_shopping=shopping_days(history,'history')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2c3ac0b28a49afb361f7f047c3bb22abb52a2398"},"cell_type":"markdown","source":"history_shopping.head(n=10)"},{"metadata":{"trusted":true,"_uuid":"80c6f1c8a8ebc7c5d17d90acc4fbdecd5309a7b9"},"cell_type":"code","source":"train , test = merge_train_test(train, test , history_shopping )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e524c0623e429773095fff64c399c9d228782f56"},"cell_type":"code","source":"new_shopping=shopping_days(new_transactions, 'new')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b543a9a4cabdfc36ae447a173f91cdf3cdaa3795"},"cell_type":"code","source":"new_shopping.head(n=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"133b1203505c144f7f2d3cd0e9b3471ff1156be8"},"cell_type":"code","source":"train , test =merge_train_test(train, test , new_shopping )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"253d91d946529d619e7b4314b88dc098d253821d"},"cell_type":"markdown","source":"to add to features engineering first function "},{"metadata":{"trusted":true,"_uuid":"5282085ac88ad19336193c709d426fd5ae69e8e5"},"cell_type":"markdown","source":"df = pd.DataFrame()\ndf['card_id']=history['card_id']\ndf['purchase_date_unique']=history['purchase_date'].apply(lambda x: x.date())\ndays_cnt = df.groupby('card_id')['purchase_date_unique'].nunique()\ndays_cnt.reset_index(inplace=True)"},{"metadata":{"trusted":true,"_uuid":"69d439f4b8445e4b502e0b6a985c2549c56d9742"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , days_cnt)"},{"metadata":{"trusted":true,"_uuid":"f220631793b5978145bb33c9e6b887480deffc09"},"cell_type":"markdown","source":"merchant_cnt = history.groupby('card_id')['merchant_id'].nunique()\nmerchant_cnt=merchant_cnt.reset_index()\nmerchant_cnt.columns = ['card_id','number_of_merchant']"},{"metadata":{"trusted":true,"_uuid":"0a79ec06ff8e43b75d3375aa9e9dfd71b8b882dc"},"cell_type":"markdown","source":"type(merchant_cnt)"},{"metadata":{"trusted":true,"_uuid":"ad4a5b28fffaef7f2de4a19682d3564b27fc665f"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , merchant_cnt )"},{"metadata":{"trusted":true,"_uuid":"f8abd8ffc7a3b2cfe6c6ab987d2e5e5c26f7b160"},"cell_type":"code","source":"\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ac4bae4dbecdfd5f46575f400252b0ec829554a"},"cell_type":"markdown","source":"installments_number_new=new_transactions.groupby('card_id')['installments'].apply(lambda x: (x>0).sum()).reset_index(name='installments_count_new')"},{"metadata":{"trusted":true,"_uuid":"807a5f83f7eceb359c47e7667f291115860f19c0"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , installments_number_new)"},{"metadata":{"_uuid":"902ebd510b3e265baf1daa28942078bc7a73feaa"},"cell_type":"markdown","source":"A optimiser "},{"metadata":{"trusted":true,"_uuid":"0af57a59fd017b5316145005b39cd6f859ea7f63"},"cell_type":"code","source":"#history['day_of_week']=history['purchase_date'].dt.weekday\n#history['day_of_month']=history['purchase_date'].dt.day\n#history['month_of_year']=history['purchase_date'].dt.month\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24eb0bc6a93e5192794b652ebbb2a0beded44da1"},"cell_type":"code","source":"#most_frequent_day_of_week=history.groupby('card_id')['day_of_week'].agg(mode).reset_index(name='most_frequent_day_of_week')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5f88741d0556879a817665dea498aa5e9bbd8a0"},"cell_type":"code","source":"#train , test = merge_train_test(train, test , most_frequent_day_of_week )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca4e046724ebd82a95d135118c195aa363f626cc"},"cell_type":"code","source":"#most_frequent_day_of_month=history.groupby('card_id')['day_of_month'].agg(mode).reset_index(name='most_frequent_day_of_month')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"081a704697445461421000505286bdd3043081f5"},"cell_type":"code","source":"#train , test = merge_train_test(train, test , most_frequent_day_of_month )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44ef5c06d75ca2cbe1b4a25466d8c6d0e91a3a0b"},"cell_type":"code","source":"#most_frequent_month=history.groupby('card_id')['month_of_year'].agg(mode).reset_index(name='most_frequent_month')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a12fa92c74a5e396638ebcb18b1d3aadb1a5be6"},"cell_type":"code","source":"#train , test = merge_train_test(train, test , most_frequent_month )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c69c4620526856aef95204623d0a58c66256b10"},"cell_type":"markdown","source":"history['weekend'] = (history.purchase_date.dt.weekday >=5).astype(int)"},{"metadata":{"trusted":true,"_uuid":"4fcad520b793b9148a4dbe1938a541c9bc597a00"},"cell_type":"markdown","source":"week_end=history.groupby('card_id')['weekend'].agg(['sum', 'mean']).reset_index()\nweek_end.columns = ['card_id','weekend_sum', 'weekend_mean']"},{"metadata":{"trusted":true,"_uuid":"de4e8ec8598fc25e40a0f61d109ca1d747a20803"},"cell_type":"markdown","source":"week_end.head()"},{"metadata":{"trusted":true,"_uuid":"459e98125cc9c79935759c9804a77f1e045d767c"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test ,week_end )"},{"metadata":{"trusted":true,"_uuid":"c49875a5632c584149f84c3b9c5e4be2e00d3267"},"cell_type":"markdown","source":"month_lags = history.groupby('card_id').agg({'month_lag':['mean','std']})\nmonth_lags.columns = ['month_lag_mean','month_lag_std']\nmonth_lags = month_lags.reset_index()\n\n"},{"metadata":{"trusted":true,"_uuid":"b9116a7a844b418b0bae7fe850b307f19d44cf53"},"cell_type":"markdown","source":"train , test = merge_train_test(train, test , month_lags )"},{"metadata":{"trusted":true,"_uuid":"0d71210959224a6f574f586c5e81d9daa5be464a"},"cell_type":"code","source":"last_buy = history.groupby('card_id')['purchase_date'].max()\nlast_buy = last_buy.reset_index(name='last_one')\nlast_buy['last_one']=(datetime.today() - last_buy['last_one']).dt.days.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed8b34c384afe4163f4accb784d0da34f17fe190"},"cell_type":"code","source":"train , test = merge_train_test(train, test , last_buy )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b694cafce7a67355b78dbe399eff9e3865ae1652"},"cell_type":"code","source":"First_buy = new_transactions.groupby('card_id')['purchase_date'].min().reset_index(name='first_one')\nFirst_buy['first_one']=(datetime.today() - First_buy['first_one']).dt.days.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"782bc4505f8a7b8305b655f603b1f92a5e378924"},"cell_type":"markdown","source":"First_buy.head(n=10)"},{"metadata":{"trusted":true,"_uuid":"b902fb29d7b7b6385a90927de65b16f065845280"},"cell_type":"code","source":"train , test = merge_train_test(train, test , First_buy )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5efaad159cf0bfe5a4438c415aa9d30984c05268"},"cell_type":"code","source":"train['between']=train['first_one'] - train['last_one']\ntest['between']=test['first_one'] - test['last_one']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb28e054f58ec5c2f321fdf554f50a098943ba53"},"cell_type":"code","source":"train['from']=(datetime.today() - train['first_active_month']).dt.days\ntest['from']=(datetime.today() - test['first_active_month']).dt.days","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63e4576094969c93fd99f8a0f229a666269ac842"},"cell_type":"code","source":"train['activation_month'] = train[\"first_active_month\"].dt.month\ntest['activation_month'] = test [\"first_active_month\"].dt.month","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53a5c8043441f6b2626cd4167cf757cead145bf2"},"cell_type":"code","source":"train['activation_year'] = train[\"first_active_month\"].dt.year\ntest['activation_year'] = test [\"first_active_month\"].dt.year","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e231bf4e55feffa7194b6e6c558c398ba370a5c"},"cell_type":"code","source":"def aggregate_per_month(df,history):\n    \n\n    agg_func = {\n            'purchase_amount': ['count', 'mean', 'min', 'max', 'std'],\n            'installments': ['count', 'sum', 'mean', 'min', 'max', 'std'],\n            }\n\n    intermediate_group = history[history['authorized_flag']==1].groupby(['card_id', 'month_lag']).agg(agg_func)\n    intermediate_group.columns = ['_'.join(col).strip() for col in intermediate_group.columns.values]\n    intermediate_group.reset_index(inplace=True)\n\n    final_group = intermediate_group.groupby('card_id').agg(['mean', 'std'])\n    final_group.columns = ['_'.join(col).strip() for col in final_group.columns.values]\n    final_group.reset_index(inplace=True)\n    final_group = pd.merge(df, final_group, on='card_id', how='left')\n    return final_group","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ac46328173353c8e88396f0551ff5894500ddcb"},"cell_type":"code","source":"import time\ntime.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56b7921944207e353a0f855ab60b7890b8cf5b41"},"cell_type":"code","source":"train = aggregate_per_month(train,history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c81d65f576257d75c236a02d31e50924a36d1e59"},"cell_type":"code","source":"import time\ntime.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee0f4fb5b596a1c0a5f87ba56f3e57aaeca704ed"},"cell_type":"code","source":"test=aggregate_per_month(test,history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48346cf76ddef7357c2aa0ad54c4fcb2069d1c10"},"cell_type":"code","source":"import time\ntime.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f962ce8cb17f7d519223f1513217107e80f4d493"},"cell_type":"code","source":"def successive_aggregates(tr,df, field1, field2, name ):\n    t = df.groupby(['card_id', field1])[field2].mean()\n    u = pd.DataFrame(t).reset_index().groupby('card_id')[field2].agg(['mean', 'min', 'max', 'std'])\n    u.columns = [str(name)+field1 + '_' + field2 + '_' + col for col in u.columns.values]\n    u.reset_index(inplace=True)\n    tr=pd.merge(tr, u, on='card_id', how='left')\n    return tr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d36ce75ed00525f247313cb6fa3fa212b4dcabd"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8941c2e31c1d3c1542d87ec23c317d20ecd53808"},"cell_type":"code","source":"train = successive_aggregates(train,new_transactions, 'category_1', 'purchase_amount','new')\ntest  = successive_aggregates(test,new_transactions, 'category_1', 'purchase_amount','new')\n\ntrain = successive_aggregates(train,new_transactions, 'category_2', 'purchase_amount','new')\ntest  = successive_aggregates(test,new_transactions, 'category_2', 'purchase_amount','new')\n\ntrain = successive_aggregates(train,new_transactions, 'category_3', 'purchase_amount','new')\ntest  = successive_aggregates(test,new_transactions, 'category_3', 'purchase_amount','new')\n\n\ntrain = successive_aggregates(train,new_transactions,  'installments', 'purchase_amount','new')\ntest  = successive_aggregates(test,new_transactions,  'installments', 'purchase_amount','new')\n\ntrain = successive_aggregates(train,new_transactions, 'city_id', 'purchase_amount','new')\ntest  = successive_aggregates(test,new_transactions, 'city_id', 'purchase_amount','new')\n\ntrain = successive_aggregates(train,new_transactions, 'category_1', 'installments','new')\ntest  = successive_aggregates(test,new_transactions, 'category_1', 'installments','new')\n\ntrain = successive_aggregates(train,new_transactions, 'category_2', 'installments','new')\ntest  = successive_aggregates(test,new_transactions, 'category_2', 'installments','new')\n\ntrain = successive_aggregates(train,new_transactions, 'category_3', 'installments','new')\ntest  = successive_aggregates(test,new_transactions, 'category_3', 'installments','new')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f66d944e3605d2646f20c2089a3e60dfeacb76c0"},"cell_type":"code","source":"time.sleep(15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4cae3009e479d71f07565c091a9baea8cbfebda"},"cell_type":"code","source":"train = successive_aggregates(train,history, 'category_1', 'purchase_amount','history')\ntest  = successive_aggregates(test,history, 'category_1', 'purchase_amount','history')\n\ntrain = successive_aggregates(train,history, 'category_2', 'purchase_amount','history')\ntest  = successive_aggregates(test,history, 'category_2', 'purchase_amount','history')\n\ntrain = successive_aggregates(train,history, 'category_3', 'purchase_amount','history')\ntest  = successive_aggregates(test,history, 'category_3', 'purchase_amount','history')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37c89b3722b3fa405820102ec2eaaecd0cc7f30e"},"cell_type":"code","source":"import time\ntime.sleep(15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"449159968b793c4c254752a023329ba3b62d5f62"},"cell_type":"code","source":"train = successive_aggregates(train,history, 'category_1', 'installments','history')\ntest  = successive_aggregates(test,history, 'category_1', 'installments','history')\n\ntrain = successive_aggregates(train,history, 'category_2', 'installments','history')\ntest  = successive_aggregates(test,history, 'category_2', 'installments','history')\n\ntrain = successive_aggregates(train,history, 'category_3', 'installments','history')\ntest  = successive_aggregates(test,history, 'category_3', 'installments','history')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c4ef071cf3f995d5d83e55e39dac6a923822cc1"},"cell_type":"code","source":"time.sleep(15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"71a187573a92a1b6324e4b7b159bf6b9f43b0685"},"cell_type":"code","source":"train = successive_aggregates(train,history,  'installments', 'purchase_amount','history')\ntest  = successive_aggregates(test,history,  'installments', 'purchase_amount','history')\n\ntrain = successive_aggregates(train,history, 'city_id', 'purchase_amount','history')\ntest  = successive_aggregates(test,history, 'city_id', 'purchase_amount','history')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b821a68bddd137ec3abcbf14f1086a03cbd24f3e"},"cell_type":"code","source":"train['outliers'] = 0\ntrain.loc[train['target'] < -30, 'outliers'] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e3170da14e48a10aa26c7da94ec5f16c41c4e6d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a5115fcc94591726489420420e20e97dd7ec246"},"cell_type":"code","source":"#for f in ['feature_1','feature_2','feature_3']:\n#    order_label = train.groupby([f])['outliers'].mean()\n#    train[f+'map'] = train[f].map(order_label)\n#    test[f+'map'] =  test[f].map(order_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7b20fc852b4b5afc45d79f9ded5b204586b36f3"},"cell_type":"code","source":"train.head(n=15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d19c1459af08656c9190fc573efed111a7a8120c"},"cell_type":"code","source":"import time\ntime.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adaf0ea826c500f26860892635930579afce85ff"},"cell_type":"code","source":"def aggregate_new_transactions(new_trans): \n    new_trans['authorized_flag'] = \\\n    new_trans['authorized_flag'].map({'Y':1, 'N':0})\n    agg_func = {\n        'authorized_flag': ['sum', 'mean'],\n        'merchant_id': ['nunique'],\n        'city_id': ['nunique'],\n        'purchase_amount': ['sum', 'median', 'max', 'min', 'std'],\n        'installments': ['sum', 'median', 'max', 'min', 'std'],\n        'month_lag': ['min', 'max','mean','std']\n        }\n    agg_new_trans = new_trans.groupby(['card_id']).agg(agg_func)\n    agg_new_trans.columns = ['new_' + '_'.join(col).strip() \n                           for col in agg_new_trans.columns.values]\n    agg_new_trans.reset_index(inplace=True)\n    \n    df = (new_trans.groupby('card_id')\n          .size()\n          .reset_index(name='new_transactions_count'))\n    \n    #agg_new_trans = pd.merge(df, agg_new_trans, on='card_id', how='left')\n    \n    return agg_new_trans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6508510693564f0e670270c9693cce00536b421f"},"cell_type":"code","source":"#df = aggregate_new_transactions(new_transactions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3fac1f3004f95c201435e97b33bd91382f4ff1e"},"cell_type":"code","source":"import time\ntime.sleep(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4751c28956b2e7aab04f6830143ec031da036c4"},"cell_type":"code","source":"#train , test = merge_train_test(train, test , df )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9219c048b6547ec22d7d9fbb7ba7b3562c8252d2"},"cell_type":"markdown","source":"to eliminate the next "},{"metadata":{"trusted":true,"_uuid":"6c5b58ba956dd7f22d66913e9c358ff232dc81e3"},"cell_type":"markdown","source":"def other_features (df):\n    df['authorized_percent']=df['authorized_flag_count']/df['purchase_cnt']\n    df['number_of_merchant_percent']=df['number_of_merchant']/df['purchase_cnt']\n    df['installments_count_percent']=df['installments_count']/df['purchase_cnt']\n    df['authorized_percent']=test['authorized_flag_count'] /df['purchase_cnt']\n    df['number_of_merchant_percent']=df['number_of_merchant']/df['purchase_cnt']\n    df['installments_count_percent']=df['installments_count']/df['purchase_cnt']\n    \n    return df "},{"metadata":{"trusted":true,"_uuid":"527bb0887a49a766a2ed28fca2526d226de1ab99"},"cell_type":"code","source":"import time\ntime.sleep(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47b02bbf29b3a7bcfa8da2eb77c9e6484262b7df"},"cell_type":"code","source":"#train = other_features (train )\n#test= other_features (test )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"452969c004d96fcc5c847077e108f527e91c1e16"},"cell_type":"code","source":"import time\ntime.sleep(120)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62dd4ac88476e21f7a08f38b3c8f70c2aa8f0af4"},"cell_type":"markdown","source":"\ncat=history[['card_id','category_2', 'category_3']]\ncat=pd.get_dummies(cat, columns=['category_2', 'category_3'])"},{"metadata":{"trusted":true,"_uuid":"83210dfe5973e4f0c3f4afc2044e4f08f419304c"},"cell_type":"code","source":"#history = pd.get_dummies(history, columns=['category_2', 'category_3'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40b63a8217260c396fcf6899bbc5c34f498f79a9"},"cell_type":"markdown","source":"cat.reset_index(inplace=True)"},{"metadata":{"trusted":true,"_uuid":"9a9e96e1ad7a30ba2e3cf4b0fceea3b6cf917310"},"cell_type":"code","source":"import time\ntime.sleep(60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f215a7278eda484f4ef2e481d7e08d327fc3556"},"cell_type":"code","source":"def convert(df):\n    for i in df.columns:\n        if df[i].dtype =='uint8' : df[i]=df[i].astype(float)\n    return df ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b052fca5ca4f80fbb5e81dc7b25ff04b9ddd509b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2cc9e0e6b034177a98d5bf7375f55899adcaa6a"},"cell_type":"markdown","source":"cat=convert(cat)"},{"metadata":{"trusted":true,"_uuid":"d51a90984fa1e950c5230f0240fd58e12ff6724f"},"cell_type":"markdown","source":"history.head()"},{"metadata":{"trusted":true,"_uuid":"7d6d83411967f56699d56593720d647749f5772d"},"cell_type":"code","source":"import time\ntime.sleep(120)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a506e42d7b290c1503ba622689e28098aedfb2e7"},"cell_type":"markdown","source":"new_transactions = pd.get_dummies(new_transactions, columns=['category_2', 'category_3'])"},{"metadata":{"trusted":true,"_uuid":"9636e3717ed2919fa1e4a1c996c1f9d36bb571d2"},"cell_type":"code","source":"new_transactions.head(n=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72bb8dcc8e30cc71159c0cd26d84fd19e4e4afaa"},"cell_type":"markdown","source":"new_transactions=convert(new_transactions)"},{"metadata":{"trusted":true,"_uuid":"fdbc93ca934524233f09267679aeb3d4d1c6bb5b"},"cell_type":"code","source":"def aggregate_transactions(df,frame,name):\n    \n    #frame.loc[:, 'purchase_date'] = pd.DatetimeIndex(frame['purchase_date']).\\\n                                     # astype(np.int64) * 1e-9\n    \n    agg_func = {\n    #'category_1_N': [ 'mean'],\n    #'category_1_Y': ['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    \n    }\n    \n    agg_history = frame.groupby(['card_id']).agg(agg_func)\n    agg_history.columns = [str(name)+'_'.join(col).strip() for col in agg_history.columns.values]\n    agg_history.reset_index(inplace=True)\n    agg_new_trans = pd.merge(df, agg_history, on='card_id', how='left')\n    \n    return agg_new_trans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6f10db97f793dbacf35486681c6ac8cbea75e1e"},"cell_type":"code","source":"history.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3388d455017c3aea80201d8588199d2a992d6330"},"cell_type":"markdown","source":"train=aggregate_transactions(train,cat,'history')"},{"metadata":{"trusted":true,"_uuid":"e95a052ec5509c1b0aed8d6d67a0a7f54f4691d2"},"cell_type":"code","source":"#train.head(n=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3aef8241a24475f0b3c7c0006a62c0d3e3ae0f24"},"cell_type":"markdown","source":"test=aggregate_transactions(test,cat,'history')"},{"metadata":{"trusted":true,"_uuid":"bd6effd00cc84d5d570f01b18618d4e9f310465f"},"cell_type":"markdown","source":"train=aggregate_transactions(train,new_transactions,'new')"},{"metadata":{"trusted":true,"_uuid":"86a8301a5ffc27029b2ae4b30ed625d788cc79b2"},"cell_type":"markdown","source":"test=aggregate_transactions(test,new_transactions,'new')"},{"metadata":{"trusted":true,"_uuid":"d32281107ae975c9d89705d36453f48681976f2f","scrolled":true},"cell_type":"code","source":"print(test.shape)\nprint(train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adb9d84ce5efce8cb8c3c03ade460652c5735f32"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b1778535672625daf84cf24835fe0feac9824c6"},"cell_type":"code","source":"\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7425fc0c931cc667923d67377eb049c823f8f9f3"},"cell_type":"code","source":"#train = aggregate_per_month(train,history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c820a59d6a4c4bc573c8b281323f2b8d4eae2f44"},"cell_type":"code","source":"#test=aggregate_per_month(test,history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"666222e83ed7c7daed2dc3401d440471f415f5c9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"520a1592033f76c3661a8646bed16f2fe65a8862"},"cell_type":"code","source":"history.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"02b540baa872348d0c981a36213d46223df1762b"},"cell_type":"code","source":"history.head(n=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aa4d07051ad1317722adb4e2c374119bcc951767"},"cell_type":"code","source":"for f in ['feature_1','feature_2','feature_3']:\n    order_label = train.groupby([f])['outliers'].mean()\n    train[f] = train[f].map(order_label)\n    test[f] = test[f].map(order_label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c04d586144a776943ba06d0049c6f1ef4fe6ca6"},"cell_type":"code","source":"X = train.drop(columns=['first_active_month','target','card_id'])\ntest_X = test.drop(columns=['first_active_month','card_id'])\n\nY=train['target']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a18b815cda620a3a4ac8eb0f09a5fc9e3fb5fadf"},"cell_type":"code","source":"#X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7)\nX.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94c3de3806a4209dd93432128ef788c9e3f35577"},"cell_type":"code","source":"corr_matrix = X.corr().abs()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07286bfd312557c8ab759b9dd944c5146159bcde"},"cell_type":"code","source":"plt.matshow(corr_matrix)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b00e8ba4e01cf30ae8a8a226d7e44409e01b2207"},"cell_type":"code","source":"upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(np.bool))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e303100a758531728267daa01c8d53245563ac6"},"cell_type":"code","source":"to_drop = [column for column in upper.columns if any(upper[column] > 0.95)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9574fa18119c9d22e56093e099775e3b0f5f6816"},"cell_type":"code","source":"to_drop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67ebd936290218d63496f4732450c16c2ad106dd"},"cell_type":"code","source":"#X = X.drop(columns=to_drop)\n#test_X = test_X.drop(columns=to_drop)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e48059ca460b65c2aeb9ff9b209ac41583c7927a"},"cell_type":"code","source":"X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6a2c935566fa1129f5e58f9cd50aa31f932899e"},"cell_type":"code","source":"test_X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e55d9b1d7dd4bc36d6d7a73e171511e92d7973a4"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb55f25ab529bd12c1f882e4efb877c9c12faeaf"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd25768b37219cbf0a47cb03b59d8157943bfb18"},"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"095eb7d3a9eb383025af97571f4150988fcc9ea1"},"cell_type":"code","source":"folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2efe8812669ac8925aad5d84ae0195530e51ddae"},"cell_type":"code","source":"for fold_, (trn_idx, val_idx) in enumerate(folds.split(train,train['outliers'].values)):\n    print(trn_idx, val_idx)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf7d6546d095ce14d8b1cb5b6f01df53af160694"},"cell_type":"code","source":"xgb_params = {'eta': 0.01, 'max_depth': 5, 'subsample': 0.8, 'colsample_bytree': 0.8, 'lambda' : 0.1 , 'alpha' : 0.4,'min_child_weight':1,\n          'objective': 'reg:linear', 'eval_metric': 'rmse', 'silent': True, 'nthread': 4}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df067dbed77af30e15208b5d6b02a55f89d071b2"},"cell_type":"code","source":"#xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,\n               # max_depth = 5, alpha = 10, lamda=2 , n_estimators = 10, eval_metric='rmse')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90176f5a726d6e3379ad15b89dc3bc91762ce07e"},"cell_type":"code","source":"import lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c95893ffe26cdf8381571ada74792cfae696f80"},"cell_type":"code","source":"param = {'num_leaves': 32,\n         'min_data_in_leaf': 149, \n         'objective':'regression',\n         'max_depth': 4,\n         'learning_rate': 0.005,\n         \"boosting\": \"gbdt\",\n         \"feature_fraction\": 0.7522,\n         \"bagging_freq\": 1,\n         \"bagging_fraction\": 0.7083 ,\n         \"bagging_seed\": 11,\n         \"metric\": 'rmse',\n         \"lambda_l1\": 0.45,\n         \"lambda_l2\":0.15,\n         \"random_state\": 133,\n         \"verbosity\": -1}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b585a7c540491b2c0559eb135490ead1fafefbd9"},"cell_type":"code","source":"features = [c for c in X.columns if c not in ['card_id', 'first_active_month', 'target','month_lag_mean_y','month_lag_std_y','outliers',\n                                             'history_purchase_date_max', 'history_purchase_date_min', 'new_purchase_date_max', 'new_purchase_date_min']]\ncategorical_feats = ['feature_1','feature_2','feature_3']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06bbbb252b71e94508b9e225d02bb861f02d1f62"},"cell_type":"code","source":"len(list(X.keys()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9159f4c89c215425f5656b3166fc73fcbb155cf0"},"cell_type":"code","source":"Y.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3e310ff3fda97bb36f54bfd9156e006a14a323e"},"cell_type":"code","source":"#missing_data_function(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c559681ad55bb88104552a8e468ee5171fd4dbf8"},"cell_type":"code","source":"#X=X.fillna(-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34aed4efaaa33a038650052e9d6efe367bef4f99"},"cell_type":"code","source":"#missing_data_function(test_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbc03b5282600c9a6275c3cd49f5c922c7492f43"},"cell_type":"code","source":"#test_X=test_X.fillna(-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f98d6c8c8372ec284a7d5f9fc3f369ab76755126"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b3a7677ee9d43220fa2ed7d0187613f737881f76"},"cell_type":"markdown","source":"modeling "},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"ba270eb229707e7674a6fc4b27e4c299e809f029"},"cell_type":"code","source":"X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4de5f83266106eb0b2ca93d966f142785b7fdee"},"cell_type":"code","source":"X=X.drop(columns=['history_purchase_date_max', 'history_purchase_date_min', 'new_purchase_date_max', 'new_purchase_date_min'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d7e7b166b2b570d2a9c1e290ec386c6cb104a72"},"cell_type":"code","source":"rest=features","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"8843477402c21b894d169b482f62474a0f0c02b7"},"cell_type":"code","source":"folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=15)\noof_lgbm = np.zeros(len(train))\npredictions_lgbd = np.zeros(len(test))\nstart = time.time()\nfeature_importance_df = pd.DataFrame()\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train,train['outliers'].values)):\n    print(\"fold n°{}\".format(fold_))\n    trn_data = lgb.Dataset(X.iloc[trn_idx][features],\n                           label=Y.iloc[trn_idx],\n                           categorical_feature=categorical_feats\n                          )\n    val_data = lgb.Dataset(X.iloc[val_idx][features],\n                           label=Y.iloc[val_idx],\n                           categorical_feature=categorical_feats\n                          )\n\n    num_round = 10000\n    clf = lgb.train(param,\n                    trn_data,\n                    num_round,\n                    valid_sets = [trn_data, val_data],\n                    verbose_eval=100,\n                    early_stopping_rounds = 100)\n    \n    oof_lgbm[val_idx] = clf.predict(X.iloc[val_idx][features], num_iteration=clf.best_iteration)\n    \n    \n    \n    predictions_lgbd += clf.predict(test_X[features], num_iteration=clf.best_iteration) / folds.n_splits\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"876d3804f46f0137d9840ea08f812b3662b60e08"},"cell_type":"code","source":"test_X.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6b2a097fcbc73a4abc652a9f656ba0340d9c89c"},"cell_type":"code","source":"folds = KFold(n_splits=5, shuffle=True, random_state=15)\noof = np.zeros(len(train))\noof_xgb_3 = np.zeros(len(train))\npredictions_xg = np.zeros(len(test))\n\nstart = time.time()\n\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train,train['outliers'].values)):\n        \n        X_train, X_valid = X.iloc[trn_idx][features], X.iloc[val_idx][features]\n        y_train, y_valid = Y.iloc[trn_idx], Y.iloc[val_idx]\n        \n        \n        \n        train_data = xgb.DMatrix(data=X_train, label=y_train)\n        valid_data = xgb.DMatrix(data=X_valid, label=y_valid)\n        watchlist = [(train_data, 'train'), (valid_data, 'valid_data')]\n        clf_xg = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=100, verbose_eval=500, params=xgb_params)\n        y_pred_valid = clf_xg.predict(xgb.DMatrix(X_valid), ntree_limit=clf_xg.best_ntree_limit)\n        oof_xgb_3[val_idx] = clf_xg.predict(xgb.DMatrix(X_valid), ntree_limit=clf_xg.best_ntree_limit)\n        y_pred = clf_xg.predict(xgb.DMatrix(test_X[features]), ntree_limit=clf_xg.best_ntree_limit)\n        predictions_xg += y_pred / folds.n_splits\n        \n        \n       \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"01df304240eaaab558973dfa79fda10707d9d042"},"cell_type":"code","source":"#predictions_xg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab0126a51d7cbb3506b8995a68a6b8fedfc104b9"},"cell_type":"code","source":"param_outliers = {'num_leaves': 8,\n         'min_data_in_leaf': 30, \n         'objective':'binary',\n         'max_depth': 3,\n         'learning_rate': 0.01,\n         \"boosting\": \"rf\",\n         \"feature_fraction\": 0.9,\n         \"bagging_freq\": 1,\n         \"bagging_fraction\": 0.9 ,\n         \"bagging_seed\": 11,\n         \"metric\": 'binary_logloss',\n         \"lambda_l1\": 0.15,\n         \"lambda_l2\": 0.0,        \n         \"verbosity\": -1,\n         \"random_state\": 2333}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"023ec683f94dc90e8e8faf143d03db4526f73e29"},"cell_type":"code","source":"#predictions_xg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"5e08865364c9452e2cccdfad28d9e4e578b51f66"},"cell_type":"code","source":"folds = KFold(n_splits=5, shuffle=True, random_state=15)\noof_outliers = np.zeros(len(train))\npredictions_outliers = np.zeros(len(test))\nfeature_importance_df = pd.DataFrame()\ntarget=train['outliers']\nstart = time.time()\n\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train.values, target.values)):\n    print(\"fold n°{}\".format(fold_))\n    trn_data = lgb.Dataset(train.iloc[trn_idx][features], label=target.iloc[trn_idx], categorical_feature=categorical_feats)\n    val_data = lgb.Dataset(train.iloc[val_idx][features], label=target.iloc[val_idx], categorical_feature=categorical_feats)\n\n    num_round = 10000\n    clf = lgb.train(param_outliers, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 200)\n    oof_outliers[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)\n    \n    fold_importance_df = pd.DataFrame()\n    fold_importance_df[\"feature\"] = features\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_outliers += clf.predict(test[features], num_iteration=clf.best_iteration) / folds.n_splits\n\n#print(\"CV score: {:<8.5f}\".format(log_loss(target, oof))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24524d708dea5237bd2d0bf1cda994fb82c009ca"},"cell_type":"code","source":"from sklearn.linear_model import Ridge","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"081854c4ba03695cb102befa3682a2041112ce46"},"cell_type":"code","source":"train_stack = np.vstack([oof_lgbm, oof_xgb_3,oof_outliers]).transpose()\ntest_stack = np.vstack([predictions_lgbd, predictions_xg,predictions_outliers]).transpose()\n\nfolds = KFold(n_splits=5, shuffle=True, random_state=15)\noof = np.zeros(train_stack.shape[0])\npredictions = np.zeros(test_stack.shape[0])\n\nfor fold_, (trn_idx, val_idx) in enumerate(folds.split(train,train['outliers'].values)):\n    print(\"fold n°{}\".format(fold_))\n    X_train, X_valid = train_stack[trn_idx], train_stack[val_idx]\n    y_train, y_valid = Y.iloc[trn_idx], Y.iloc[val_idx]\n\n    train_data = xgb.DMatrix(data=X_train, label=y_train)\n    valid_data = xgb.DMatrix(data=X_valid, label=y_valid)\n    watchlist = [(train_data, 'train'), (valid_data, 'valid_data')]\n    clf_xg = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=500, params=xgb_params)\n    y_pred_valid = clf_xg.predict(xgb.DMatrix(X_valid), ntree_limit=clf_xg.best_ntree_limit)\n    #oof_xgb_3[val_idx] = clf_xg.predict(xgb.DMatrix(train.iloc[val_idx][rest]), ntree_limit=clf_xg.best_ntree_limit)\n    y_pred = clf_xg.predict(xgb.DMatrix(test_stack), ntree_limit=clf_xg.best_ntree_limit)\n    predictions += y_pred / folds.n_splits\n    \n    #oof[val_idx] = clf.predict(X_valid)\n    #predictions += clf.predict(test_stack) / folds.n_splits\"\"\"\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5c341d6202449e7c21270d82890aff24a80cfcd"},"cell_type":"code","source":"predictions_xg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f516d0072fda625149ed669da8b4546cc62f008c"},"cell_type":"code","source":"sub_df = pd.DataFrame({\"card_id\":test[\"card_id\"].values})\nsub_df[\"target\"] =predictions_xg\nsub_df.to_csv(\"submit.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"193076d8aa3323e6a9a4a341ce2bea016b1ba004"},"cell_type":"code","source":"sub_df.head(n=100)","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}