{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Instance selection with FRPS\n\nSample usage of FRPS preprocessing, demonstrated in combination with (strict) FRNN classification.\n\nThe figures contain the selected prototypes within a section of the feature space.\nThe prototypes are coloured according to their true labels,\nwhile the feature space is coloured according to predictions on the basis of the prototypes,\nmaking the decision boundaries visible.\n\nIn total, nine subfigures are displayed,\nto illustrate the effect of the `quality_measure` (rows) and `aggr_R` (columns) parameters.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import ListedColormap\nfrom sklearn import datasets\n\nfrom frlearn.base import select_class\nfrom frlearn.classifiers import FRNN\nfrom frlearn.instance_preprocessors import FRPS\nfrom frlearn.t_norms import heyting_t_norm, lukasiewicz_t_norm\n\n# Import example data and reduce to 2 dimensions.\niris = datasets.load_iris()\nX_orig = iris.data[:, :2]\ny_orig = iris.target\n\n# Create a mesh of points in the attribute space.\nstep_size = .02\nx_min, x_max = X_orig[:, 0].min() - 1, X_orig[:, 0].max() + 1\ny_min, y_max = X_orig[:, 1].min() - 1, X_orig[:, 1].max() + 1\nxx, yy = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))\n\n# Define color maps.\ncmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])\ncmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])\n\n# Initialise figure.\nplt.figure()\n\nfor i, (aggr_name, aggr_R) in enumerate([('mean', np.mean), ('\u0141ukasiewicz', lukasiewicz_t_norm), ('Heyting', heyting_t_norm)]):\n    for j, quality_measure in enumerate(['upper', 'lower', 'both']):\n        axes = plt.subplot(3, 3, i*3 + j + 1)\n\n        # Create an instance of the FRPS preprocessor and process the data.\n        preprocessor = FRPS(aggr_R=aggr_R, quality_measure=quality_measure)\n        X, y = preprocessor(X_orig, y_orig)\n\n        # Create an instance of the FRNN classifier and construct the model.\n        clf = FRNN(upper_weights=None, lower_weights=None, upper_k=1, lower_k=1)\n        model = clf(X, y)\n\n        # Query mesh points to obtain class values and select highest valued class.\n        Z = model(np.c_[xx.ravel(), yy.ravel()])\n        Z = select_class(Z, labels=model.classes)\n\n        # Plot mesh.\n        Z = Z.reshape(xx.shape)\n        plt.pcolormesh(xx, yy, Z, cmap=cmap_light)\n\n        # Plot training instances.\n        plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=20)\n\n        # Set plot dimensions.\n        plt.xlim(xx.min(), xx.max())\n        plt.ylim(yy.min(), yy.max())\n\n        # Describe columns and rows.\n        if axes.is_first_col():\n            plt.ylabel(aggr_name, rotation=0, size='large', ha='right')\n        if axes.is_first_row():\n            plt.title(quality_measure)\n\nplt.suptitle('FRNN applied to instances of iris dataset selected by FRPS', fontsize=14)\nplt.show()"
      ]
    }
  ],
  "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
}