{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Multiclass classification with FRNN\n\nThe figures contain the training instances within a section of the selected feature space.\nThe training instances are coloured according to their true labels,\nwhile the feature space is coloured according to predictions on the basis of the training instances,\nmaking the decision boundaries visible.\n\nTwo subfigures are displayed: the first represents strict FRNN (`k == 1`),\nwhile the second represents FRNN with linear OWA weights and `k == 20`.\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.weights import LinearWeights\n\n# Import example data and reduce to 2 dimensions.\niris = datasets.load_iris()\nX = iris.data[:, :2]\ny = iris.target\n\n# Define color maps.\ncmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])\ncmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])\n\n# Initialise figure with wide aspect for two side-by-side subfigures.\nplt.figure(figsize=(8, 4))\n\nfor i, owa_weights, k, title in [\n    (1, None, 1, 'Strict'),\n    (2, LinearWeights(), 20, 'With linear weights and k = 20')\n]:\n    axes = plt.subplot(1, 2, i)\n\n    # Create an instance of the FRNN classifier and construct the model.\n    clf = FRNN(upper_weights=owa_weights, lower_weights=owa_weights, upper_k=k, lower_k=k)\n    model = clf(X, y)\n\n    # Create a mesh of points in the attribute space.\n    step_size = .02\n    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1\n    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1\n    xx, yy = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))\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,\n                edgecolor='k', s=20)\n\n    # Set subplot aspect to standard aspect ratio.\n    axes.set_aspect(1.0 / axes.get_data_ratio() * .75)\n\n    # Set plot dimensions.\n    plt.xlim(xx.min(), xx.max())\n    plt.ylim(yy.min(), yy.max())\n\n    # Describe the subfigures.\n    plt.title(title)\n\nplt.suptitle('FRNN applied to iris dataset', 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
}