{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### H2O.ai Explain Results \n\nThis notebook trains an H2O AutoML Model on a subset of the data and generates automated explanations for the model.","metadata":{}},{"cell_type":"code","source":"import h2o\nfrom h2o.automl import H2OAutoML\nimport pandas as pd\n\nh2o.init()\n\n# Import the data keeping only the final transaction for each customer\ndf = pd.read_csv(\"../input/amex-default-prediction/train_data.csv\", nrows=500000)\ndf = df.sort_values(['customer_ID', 'S_2'])\ndf = df.drop_duplicates(subset='customer_ID', keep='last')\ntrain_labels =  pd.read_csv(\"../input/amex-default-prediction/train_labels.csv\") \n\n# Add labels \ndf = df.merge(train_labels, on = 'customer_ID', how='left')\n\n# Reponse column\ny = \"target\"\n\n# Features to be included\nfeatures = list(df.columns)\nfeatures.remove('customer_ID')\nfeatures.remove('S_2')\nfeatures.remove(y)\n\n# Convert to H2O frame\ndf = h2o.H2OFrame(df)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-28T21:37:28.303723Z","iopub.execute_input":"2022-05-28T21:37:28.304189Z","iopub.status.idle":"2022-05-28T21:38:33.271122Z","shell.execute_reply.started":"2022-05-28T21:37:28.304100Z","shell.execute_reply":"2022-05-28T21:38:33.270099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split into train & test\nsplits = df.split_frame(ratios = [0.8], seed = 1)\ntrain = splits[0]\ntest = splits[1]\n\n# Change the target to a factor\ntrain[y] = train[y].asfactor()\ntest[y] = test[y].asfactor()","metadata":{"execution":{"iopub.status.busy":"2022-05-28T21:38:33.273606Z","iopub.execute_input":"2022-05-28T21:38:33.274497Z","iopub.status.idle":"2022-05-28T21:38:35.477027Z","shell.execute_reply.started":"2022-05-28T21:38:33.274450Z","shell.execute_reply":"2022-05-28T21:38:35.476030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run H2O.ai AutoML for 30 minutes\naml = H2OAutoML(max_runtime_secs=1800, seed=1)\naml.train(y=y, x=features, training_frame=train)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T21:38:35.480035Z","iopub.execute_input":"2022-05-28T21:38:35.480427Z","iopub.status.idle":"2022-05-28T22:10:37.724846Z","shell.execute_reply.started":"2022-05-28T21:38:35.480396Z","shell.execute_reply":"2022-05-28T22:10:37.723840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Explain leader model & compare with all AutoML models\nexa = aml.explain(test, top_n_features=20)","metadata":{"execution":{"iopub.status.busy":"2022-05-28T22:10:37.727124Z","iopub.execute_input":"2022-05-28T22:10:37.727743Z","iopub.status.idle":"2022-05-29T00:24:34.097486Z","shell.execute_reply.started":"2022-05-28T22:10:37.727698Z","shell.execute_reply":"2022-05-29T00:24:34.096261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Explain a single H2O model (e.g. leader model from AutoML)\n# exm = aml.leader.explain(test, top_n_features=10)","metadata":{"execution":{"iopub.status.busy":"2022-05-29T00:24:34.099260Z","iopub.execute_input":"2022-05-29T00:24:34.099593Z","iopub.status.idle":"2022-05-29T00:24:34.104549Z","shell.execute_reply.started":"2022-05-29T00:24:34.099565Z","shell.execute_reply":"2022-05-29T00:24:34.103352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# h2o.cluster().shutdown(prompt=False) ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}