{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Train Random Forest Estimator with H2O" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking whether there is an H2O instance running at http://localhost:54321..... not found.\n", "Attempting to start a local H2O server...\n", " Java Version: openjdk version \"1.8.0_181\"; OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13); OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)\n", " Starting server from /opt/conda/lib/python2.7/site-packages/h2o/backend/bin/h2o.jar\n", " Ice root: /tmp/tmpz8qTmm\n", " JVM stdout: /tmp/tmpz8qTmm/h2o_unknownUser_started_from_python.out\n", " JVM stderr: /tmp/tmpz8qTmm/h2o_unknownUser_started_from_python.err\n", " Server is running at http://127.0.0.1:54321\n", "Connecting to H2O server at http://127.0.0.1:54321... successful.\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
H2O cluster uptime:01 secs
H2O cluster timezone:Etc/UTC
H2O data parsing timezone:UTC
H2O cluster version:3.22.1.1
H2O cluster version age:23 days
H2O cluster name:H2O_from_python_unknownUser_ukj9f9
H2O cluster total nodes:1
H2O cluster free memory:3.042 Gb
H2O cluster total cores:7
H2O cluster allowed cores:7
H2O cluster status:accepting new members, healthy
H2O connection url:http://127.0.0.1:54321
H2O connection proxy:None
H2O internal security:False
H2O API Extensions:XGBoost, Algos, AutoML, Core V3, Core V4
Python version:2.7.15 final
" ], "text/plain": [ "-------------------------- ----------------------------------------\n", "H2O cluster uptime: 01 secs\n", "H2O cluster timezone: Etc/UTC\n", "H2O data parsing timezone: UTC\n", "H2O cluster version: 3.22.1.1\n", "H2O cluster version age: 23 days\n", "H2O cluster name: H2O_from_python_unknownUser_ukj9f9\n", "H2O cluster total nodes: 1\n", "H2O cluster free memory: 3.042 Gb\n", "H2O cluster total cores: 7\n", "H2O cluster allowed cores: 7\n", "H2O cluster status: accepting new members, healthy\n", "H2O connection url: http://127.0.0.1:54321\n", "H2O connection proxy:\n", "H2O internal security: False\n", "H2O API Extensions: XGBoost, Algos, AutoML, Core V3, Core V4\n", "Python version: 2.7.15 final\n", "-------------------------- ----------------------------------------" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Parse progress: |█████████████████████████████████████████████████████████| 100%\n" ] } ], "source": [ "import h2o\n", "from h2o.estimators.random_forest import H2ORandomForestEstimator\n", "\n", "import mlflow\n", "import mlflow.h2o\n", "\n", "h2o.init()\n", "\n", "wine = h2o.import_file(path=\"wine-quality.csv\")\n", "r = wine[\"quality\"].runif()\n", "train = wine[r < 0.7]\n", "test = wine[0.3 <= r]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def train_random_forest(ntrees):\n", " with mlflow.start_run():\n", " rf = H2ORandomForestEstimator(ntrees=ntrees)\n", " train_cols = [n for n in wine.col_names if n != \"quality\"]\n", " rf.train(train_cols, \"quality\", training_frame=train, validation_frame=test)\n", "\n", " mlflow.log_param(\"ntrees\", ntrees)\n", "\n", " mlflow.log_metric(\"rmse\", rf.rmse())\n", " mlflow.log_metric(\"r2\", rf.r2())\n", " mlflow.log_metric(\"mae\", rf.mae())\n", "\n", " mlflow.h2o.log_model(rf, name=\"model\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "drf Model Build progress: |███████████████████████████████████████████████| 100%\n", "drf Model Build progress: |███████████████████████████████████████████████| 100%\n", "drf Model Build progress: |███████████████████████████████████████████████| 100%\n", "drf Model Build progress: |███████████████████████████████████████████████| 100%\n", "drf Model Build progress: |███████████████████████████████████████████████| 100%\n" ] } ], "source": [ "for ntrees in [10, 20, 50, 100, 200]:\n", " train_random_forest(ntrees)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import yaml" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yaml.safe_dump" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 2 }