{"cells":[{"metadata":{"_uuid":"b060fec31632d5438b9d39a0a603ece1c48156db"},"cell_type":"markdown","source":"# Auto Elo  \n\nIn this kernel I try to the power of [AutoML](http://docs.h2o.ai/h2o/latest-stable/h2o-docs/automl.html from H2O) I stongly believe that these can serve as good starting points.  \n"},{"metadata":{"_uuid":"522c6485635b09f21b7638c0c48088c28ee224a2"},"cell_type":"markdown","source":"I did not spend time in creating features but I borrowed it from several fantastic starter notebooks below, the top 2  notables ones are:\n[Notebook 1](https://www.kaggle.com/youhanlee/hello-elo-ensemble-will-help-you)\n[Notebook 2](https://www.kaggle.com/fabiendaniel/elo-world)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Import all dependencies\nimport pandas as pd\nimport numpy as np\nfrom sklearn import preprocessing\nimport warnings\nimport datetime\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error\n\nimport os\nimport gc\n\nwarnings.filterwarnings(\"ignore\")\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81362e76ffcad5ad23efdb10ff5ce56372ab88b7"},"cell_type":"markdown","source":"# All File imports"},{"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\"])\nhist_trans = pd.read_csv(\"../input/historical_transactions.csv\")\nnew_trans = pd.read_csv(\"../input/new_merchant_transactions.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3e3c139a04dfc52f50a8b23c2c1d76dc4b778ca5"},"cell_type":"markdown","source":"# Feature Engineering"},{"metadata":{"trusted":true,"_uuid":"d69521c886a4adc58e9b60cb4e6b32a4871c8c7e"},"cell_type":"code","source":"train[\"month\"] = train[\"first_active_month\"].dt.month\ntest[\"month\"] = test[\"first_active_month\"].dt.month\ntrain[\"year\"] = train[\"first_active_month\"].dt.year\ntest[\"year\"] = test[\"first_active_month\"].dt.year\ntrain['elapsed_time'] = (datetime.date(2018, 2, 1) - train['first_active_month'].dt.date).dt.days\ntest['elapsed_time'] = (datetime.date(2018, 2, 1) - test['first_active_month'].dt.date).dt.days\ntrain = pd.get_dummies(train, columns=['feature_1', 'feature_2'])\ntest = pd.get_dummies(test, columns=['feature_1', 'feature_2'])\n#train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1062cc9771e756f6e266cda27bd6561c693c0b7"},"cell_type":"code","source":"hist_trans = pd.get_dummies(hist_trans, columns=['category_2', 'category_3'])\nhist_trans['authorized_flag'] = hist_trans['authorized_flag'].map({'Y': 1, 'N': 0})\nhist_trans['category_1'] = hist_trans['category_1'].map({'Y': 1, 'N': 0})\n#hist_trans.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2a703e826102012f1a9c5e79ebf39e2826d6da5"},"cell_type":"code","source":"def aggregate_transactions(trans, prefix):  \n    trans.loc[:, 'purchase_date'] = pd.DatetimeIndex(trans['purchase_date']).\\\n                                      astype(np.int64) * 1e-9\n    \n    agg_func = {\n        'authorized_flag': ['sum', 'mean'],\n        'category_1': ['mean'],\n        'category_2_1.0': ['mean'],\n        'category_2_2.0': ['mean'],\n        'category_2_3.0': ['mean'],\n        'category_2_4.0': ['mean'],\n        'category_2_5.0': ['mean'],\n        'category_3_A': ['mean'],\n        'category_3_B': ['mean'],\n        'category_3_C': ['mean'],\n        'merchant_id': ['nunique'],\n        'purchase_amount': ['sum', 'mean', 'max', 'min', 'std'],\n        'installments': ['sum', 'mean', 'max', 'min', 'std'],\n        'purchase_date': [np.ptp],\n        'month_lag': ['min', 'max']\n    }\n    agg_trans = trans.groupby(['card_id']).agg(agg_func)\n    agg_trans.columns = [prefix + '_'.join(col).strip() \n                           for col in agg_trans.columns.values]\n    agg_trans.reset_index(inplace=True)\n    \n    df = (trans.groupby('card_id')\n          .size()\n          .reset_index(name='{}transactions_count'.format(prefix)))\n    \n    agg_trans = pd.merge(df, agg_trans, on='card_id', how='left')\n    \n    return agg_trans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85ffdccc098195e81f7794225aaf130d699de77f"},"cell_type":"code","source":"merch_hist = aggregate_transactions(hist_trans, prefix='hist_')\ndel hist_trans\ngc.collect()\ntrain = pd.merge(train, merch_hist, on='card_id',how='left')\ntest = pd.merge(test, merch_hist, on='card_id',how='left')\ndel merch_hist\ngc.collect()\n\nnew_trans = pd.get_dummies(new_trans, columns=['category_2', 'category_3'])\nnew_trans['authorized_flag'] = new_trans['authorized_flag'].map({'Y': 1, 'N': 0})\nnew_trans['category_1'] = new_trans['category_1'].map({'Y': 1, 'N': 0})\nmerch_new = aggregate_transactions(new_trans, prefix='new_')\ndel new_trans\ngc.collect()\n\ntrain = pd.merge(train, merch_new, on='card_id',how='left')\ntest = pd.merge(test, merch_new, on='card_id',how='left')\ndel merch_new\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edf81375217cec3385e732ba8b18bae022f6e49f"},"cell_type":"code","source":"IDS = test['card_id']\ntarget = train['target']\ndrops = ['card_id', 'first_active_month', 'target']\nuse_cols = [c for c in train.columns if c not in drops]\nfeatures = list(train[use_cols].columns)\ntrain[features].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a421278393bda3aae5e2737faa6a264759e7ffbd"},"cell_type":"code","source":"print(train[features].shape)\nprint(test[features].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faa7c5caa0bc90d89f3030453e4439dde5f1c5b8"},"cell_type":"code","source":"train = train[features+['target']]\ntest = test[features]\nprint('Final train data shape is:',train.shape)\nprint('Final test data shape is:', test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"267ec8ede68f58bb7ed3206c2efd04dcd5de4f87"},"cell_type":"markdown","source":"# Let us bring in H2O now"},{"metadata":{"trusted":true,"_uuid":"8d9a9088ce176f25df5f84fb98c933e39293acc8"},"cell_type":"code","source":"import h2o\nfrom h2o.automl import H2OAutoML","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd77b2f77ba11c86b18cde5fa5f0d944c2fc9212"},"cell_type":"code","source":"# intializing H2o\nh2o.init()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54e01e95ccd7b8ad541b23b03a788cc30e2739cd"},"cell_type":"code","source":"# Convert pandas datarame into h2o dataframe\nhtrain = h2o.H2OFrame(train)\nhtest = h2o.H2OFrame(test)\ndel train, test\ngc.collect()\nprint(htrain.shape, htest.shape)\nprint(htrain.head)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a4facfbbb29439ebe0241952c724ea93a815cb67"},"cell_type":"code","source":"# Assign x as Independent and y as Dependent\nx = htrain.columns\ny = \"target\"\nx.remove(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4c61ea25865feafe5c3c1d93b08904ae833cc97"},"cell_type":"code","source":"# Uncomment and run locally to understand the different Parameter\n?? H2OAutoML\n\"\"\"\nH2OAutoML(nfolds=5, balance_classes=False, class_sampling_factors=None, \nmax_after_balance_size=5.0, max_runtime_secs=3600, max_models=None, stopping_metric='AUTO', \nstopping_tolerance=None, stopping_rounds=3, seed=None, project_name=None, \nexclude_algos=None, keep_cross_validation_predictions=False, \nkeep_cross_validation_models=False, keep_cross_validation_fold_assignment=False, \nsort_metric='AUTO')\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0624109337510e87552d040404393c807d56b4e7","scrolled":true},"cell_type":"code","source":"# Since our goal is AutoML, I am specifying the least parameters\nautoelo = H2OAutoML(max_models=20, seed=42)\nautoelo.train(x=x, y=y, training_frame=htrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0db835abf8c86e5b317ef75a243555e748990e4"},"cell_type":"code","source":"# Stack up and display the results of the top models, the metrics displayed will be relevant to regression problems\nlb = autoelo.leaderboard\nlb.head(rows=lb.nrows)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3eab59e59c21e7533413383833e309e75da0b9d4"},"cell_type":"code","source":"# Display the properties of the leader\nautoelo.leader","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a73de0cc5530067ca3c03fd3153c9612eb70207c"},"cell_type":"code","source":"# Let us get the predictions against the test data\npreds = autoelo.leader.predict(htest)\npreds = preds.as_data_frame()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a303705839686a951248791e6664a06aef120e2"},"cell_type":"code","source":"# Let us create a submission\nsub_df = pd.DataFrame({\"card_id\":IDS.values, \"target\": preds.predict.values})\nprint(sub_df.shape)\nsub_df.head()\n# Submit the prediction\nsub_df.to_csv('AML_sub.csv', index=False)","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}