{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Sliced Performance (Regression)\n\nThis example demonstrates how to analyze model performance across different data slices\nfor regression problems using various slicing methods and metrics.\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": [
        "from modeva import DataSet\nfrom modeva import TestSuite\nfrom modeva.models import MoLGBMRegressor, MoXGBRegressor\nfrom modeva.testsuite.utils.slicing_utils import get_data_info"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load and prepare dataset\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ds = DataSet()\nds.load(name=\"BikeSharing\")\nds.set_target(\"cnt\")\nds.set_random_split()\n\nds.scale_numerical(features=(\"cnt\",), method=\"log1p\")\nds.preprocess()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Train models\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model1 = MoXGBRegressor()\nmodel1.fit(ds.train_x, ds.train_y)\n\nmodel2 = MoLGBMRegressor(max_depth=2, verbose=-1, random_state=0)\nmodel2.fit(ds.train_x, ds.train_y.ravel())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Basic slice accuracy analysis\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Analyze residual feature importance\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ts = TestSuite(ds, model1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Categorical feature slicing\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(features=\"season\", metric=\"MAE\", threshold=0.2)\nresults.table\n\n# Uniform binning (Numerical feature)\nresults = ts.diagnose_slicing_accuracy(features=\"hr\", method=\"uniform\", bins=10, metric=\"MAE\")\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Quantile binning (Numerical feature)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(features=\"hr\", method=\"quantile\", bins=10, metric=\"MAE\")\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Auto-XGB binning (Numerical feature)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(features=\"hr\", method=\"auto-xgb1\", metric=\"MAE\")\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Custom binning (Numerical feature)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(\n    features=\"hr\", \n    method=\"precompute\",\n    bins={\"hr\": (0, 5, 10, 20, 23)}, \n    metric=\"MAE\"\n)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Advanced slicing analysis\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Batch mode 1D slicing\nresults = ts.diagnose_slicing_accuracy(\n    features=((\"hr\", ), (\"season\",), (\"temp\", )),\n    method=\"auto-xgb1\",\n    metric=\"MAE\"\n)\nresults.plot(name=\"hr\", figsize=(6, 6))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Batch mode 1D Slicing (all features by setting features=None)\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(\n    features=None,\n    method=\"auto-xgb1\",\n    metric=\"MAE\"\n)\nresults.table"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "2D feature interaction\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = ts.diagnose_slicing_accuracy(\n    features=(\"hr\", \"season\"), \n    method=\"uniform\", \n    bins=10, \n    metric=\"MAE\"\n)\nresults.plot(figsize=(6, 5))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Test distributional difference between weak samples and the rest\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_info = get_data_info(res_value=results.value)\ndata_results = ds.data_drift_test(**data_info[(\"hr\", \"season\")],\n                                  distance_metric=\"PSI\",\n                                  psi_method=\"uniform\",\n                                  psi_bins=10)\ndata_results.plot(\"summary\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get the list of available figure names in the result object\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_results.get_figure_names()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate a plot in the result object using the figure name\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data_results.plot(('density', 'hr'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Model comparison\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Compare models on numerical feature\ntsc = TestSuite(ds, models=[model1, model2])\nresults = tsc.compare_slicing_accuracy(\n    features=\"hr\", \n    method=\"quantile\", \n    bins=10, \n    metric=\"MAE\", \n    threshold=0.2\n)\nresults.plot(figsize=(6, 5))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare models on categorical feature\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "results = tsc.compare_slicing_accuracy(\n    features=\"season\", \n    metric=\"MAE\", \n    threshold=None\n)\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
}