{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Wrapping H2O Models\n\nThis example requires full licence, and the program will break if you use the trial licence.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Installation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To install the required package, use the following command:\n# !pip install modeva"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Authentication\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# To get authentication, use the following command: (To get full access please replace the token to your own token)\n# from modeva.utils.authenticate import authenticate\n# authenticate(auth_code='eaaa4301-b140-484c-8e93-f9f633c8bacb')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import required modules\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import h2o\nfrom h2o.estimators import H2OGradientBoostingEstimator\nfrom modeva import DataSet\nfrom modeva import TestSuite\nfrom modeva.models.wrappers.api import modeva_arbitrary_classifier"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Scripts for building a H2O model\nInitialize H2O\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    h2o.shutdown()\nexcept:\n    pass\nh2o.init()\nh2o.no_progress()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load a sample binary classification dataset\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = h2o.import_file(\"https://s3.amazonaws.com/h2o-public-test-data/smalldata/prostate/prostate.csv\")\ndata[\"CAPSULE\"] = data[\"CAPSULE\"].asfactor()  # Convert target column to factor\n\n# Split the dataset into train and test sets\ntrain, test = data.split_frame(ratios=[0.8], seed=1234)\n\n# Define feature and target columns\nX_columns = data.columns[2:-1]  # All columns except the target\ny_column = \"CAPSULE\"           # Target column"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Train H2O model\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "h2o_model = H2OGradientBoostingEstimator()\nh2o_model.train(x=X_columns, y=y_column, training_frame=train)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Wrap the data into Modeva\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ds = DataSet()\nds.load_dataframe(data=data.as_data_frame()[X_columns + [y_column]])\nds.set_train_idx(train[\"ID\"].as_data_frame().values.flatten() - 1)\nds.set_test_idx(test[\"ID\"].as_data_frame().values.flatten() - 1)\nds.set_task_type(\"Classification\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Wrap the model into Modeva\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def predict_func(X):\n    X_h2o = h2o.H2OFrame(X)  # Convert input to H2O Frame\n    X_h2o.col_names = X_columns\n    predictions = h2o_model.predict(X_h2o)[\"predict\"]\n    return predictions.as_data_frame(use_multi_thread=True).values.flatten()\n\ndef predict_proba_func(X):\n    X_h2o = h2o.H2OFrame(X)  # Convert input to H2O Frame\n    X_h2o.col_names = X_columns\n    probabilities = h2o_model.predict(X_h2o)\n    return probabilities.as_data_frame(use_multi_thread=True).values[:, 1:]\n\nmodel = modeva_arbitrary_classifier(\n    name=\"H2O-BinaryClassifier\",\n    predict_function=predict_func,\n    predict_proba_function=predict_proba_func\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create test suite for diagnostics\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts = TestSuite(ds, model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Basic accuracy analysis\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_accuracy_table()\nresults.table"
      ]
    }
  ],
  "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.11.11"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}