{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Feature selection with FRFS\n\nSample usage of FRFS feature selection, demonstrated in combination with (strict) FRNN classification.\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 simple selection of the first two features,\nwhile the second represents selection of two features by FRFS.\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.feature_preprocessors import FRFS\n\n# Import example data.\niris = datasets.load_iris()\nX_orig = iris.data\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, use_frfs in enumerate([False, True]):\n    axes = plt.subplot(1, 2, i + 1)\n\n    if use_frfs:\n        # Create an instance of the FRFS preprocessor and process the data.\n        preprocessor = FRFS(n_features=2)\n        model = preprocessor(X_orig, y)\n        X = model(X_orig)\n    else:\n        # Select first two features.\n        X = X_orig[:, :2]\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    # 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, 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('...two features selected by FRFS' if use_frfs else '...first two features')\n\nplt.suptitle('FRNN applied to iris dataset with ...', 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
}