{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# SkopeRules example\n\n\nAn example using SkopeRules for imbalanced classification.\n\nSkopeRules find logical rules with high precision and fuse them. Finding good\nrules is done by fitting classification and regression trees to sub-samples.\nA fitted tree defines a set of rules (each tree node defines a rule); rules\nare then tested out of the bag, and the ones with higher precision are selected\nand merged. This produces a real-valued decision function, reflecting for\neach new sample how many rules (each weighted by respective precision) have\nfound it abnormal.\n\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom skrules import SkopeRules\nprint(__doc__)\n\nrng = np.random.RandomState(42)\n\nn_inliers = 1000\nn_outliers = 50\n\n# Generate train data\nI = 0.5 * rng.randn(int(n_inliers / 2), 2)\nX_inliers = np.r_[I + 2, I - 2]\nO = 0.5 * rng.randn(n_outliers, 2)\nX_outliers = O  # np.r_[O, O + [2, -2]]\nX_train = np.r_[X_inliers, X_outliers]\ny_train = [0] * n_inliers + [1] * n_outliers\n\n# fit the model\nclf = SkopeRules(random_state=rng, n_estimators=10)\nclf.fit(X_train, y_train)\n\n# plot the line, the samples, and the nearest vectors to the plane\nxx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50))\nZ = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])\nZ = Z.reshape(xx.shape)\n\nplt.title(\"Skope Rules\")\nplt.contourf(xx, yy, Z, cmap=plt.cm.Blues)\n\na = plt.scatter(X_inliers[:, 0], X_inliers[:, 1], c='white',\n                s=20, edgecolor='k')\nb = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red',\n                s=20, edgecolor='k')\nplt.axis('tight')\nplt.xlim((-5, 5))\nplt.ylim((-5, 5))\nplt.legend([a, b],\n           [\"inliers\", \"outliers\"],\n           loc=\"upper left\")\nplt.show()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.12", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}