{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Discriminant Functions\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom sklearn.datasets import load_iris\nfrom sklearn.metrics import classification_report\n\nfrom sklvq import GLVQ\nfrom sklvq.discriminants import DiscriminantBaseClass\n\ndata, labels = load_iris(return_X_y=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The sklvq package contains a single discriminant function and additions are very welcome. Note\nthat they should work with the sklvq.objectives.GeneralizedLearningObjective, i.e.,\npassing additional or different arguments is not possible.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The discriminative function is depended on the objective function. This determines the\n# parameters of the call and gradient. See sklvq.objective.GeneralizedLearningObjective.\nclass CustomRelativeDistance(DiscriminantBaseClass):\n    def __call__(self, dist_same: np.ndarray, dist_diff: np.ndarray) -> np.ndarray:\n        # dist_same = distance to prototype with same label as X.\n        # dist_diff = distance to prototype with different label as X.\n        return (dist_same - dist_diff) / (dist_same + dist_diff)\n\n    def gradient(\n        self, dist_same: np.ndarray, dist_diff: np.ndarray, winner_same: bool\n    ) -> np.ndarray:\n        # Winner_same is an boolean flag to indicate if the considered prototype has the same or\n        # a different label compared to the considered X.\n        if winner_same:\n            return _gradient_same(dist_same, dist_diff)\n        return _gradient_diff(dist_same, dist_diff)\n\n\n# Gradient depends on if the label is the same or different\ndef _gradient_same(dist_same: np.ndarray, dist_diff: np.ndarray) -> np.ndarray:\n    return 2 * dist_diff / (dist_same + dist_diff) ** 2\n\n\n# Gradient depends on if the label is the same or different\ndef _gradient_diff(dist_same: np.ndarray, dist_diff: np.ndarray) -> np.ndarray:\n    return -2 * dist_same / (dist_same + dist_diff) ** 2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The CustomRelativeDistance above, accompanied with some tests and documentation, would make a\ngreat addition to the sklvq package. However, it can also directly be passed to the algorithm.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model = GLVQ(\n    discriminant_type=CustomRelativeDistance,\n    distance_type=\"squared-euclidean\",\n    activation_type=\"sigmoid\",\n    activation_params={\"beta\": 2},\n)\n\nmodel.fit(data, labels)\n\n# Predict the labels using the trained model\npredicted_labels = model.predict(data)\n\n# Print a classification report (sklearn)\nprint(classification_report(labels, predicted_labels))"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}