{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nPlot confusion matrix with User/Producer accuracy\n========================================================\n\nPlot confusion matrix from Cross-Validation, with accuracy (user/prod) as subplot.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import librairies\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from museotoolbox.ai import SuperLearner\nfrom museotoolbox.cross_validation import RandomStratifiedKFold\nfrom museotoolbox.charts import PlotConfusionMatrix\nfrom museotoolbox import datasets\nfrom sklearn.ensemble import RandomForestClassifier"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load HistoricalMap dataset\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "X,y = datasets.load_historical_data(low_res=True,return_X_y=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create CV\n-------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "RSKF = RandomStratifiedKFold(n_splits=2,\n                random_state=12,verbose=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize Random-Forest\n---------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "classifier = RandomForestClassifier()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Start learning\n---------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "SL = SuperLearner(classifier=classifier,param_grid=dict(n_estimators=[10,100]))\nSL.fit(X,y,cv=RSKF)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get kappa from each fold\n---------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for stats in SL.get_stats_from_cv(confusion_matrix=False,kappa=True):\n    print(stats['kappa'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Get each confusion matrix from folds\n-----------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cms = []\nfor stats in SL.get_stats_from_cv(confusion_matrix=True):\n    cms.append(stats['confusion_matrix'])\n    print(stats['confusion_matrix'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot confusion matrix\n-----------------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\n# a bug in Sphinx doesn't show the whole plot, sorry.\n\nlabels = ['Forest','Agriculture','Bare soil','Water','Building']\nfrom matplotlib.pyplot import cm as colorMap\nmeanCM = np.mean(cms,axis=0).astype(np.int16)\npltCM = PlotConfusionMatrix(meanCM.T) # Translate for Y = prediction and X = truth\npltCM.add_text()\npltCM.add_x_labels(labels,rotation=90)\npltCM.add_y_labels(labels)\npltCM.color_diagonal(diag_color=colorMap.Purples,matrix_color=colorMap.Reds)\npltCM.add_accuracy()\npltCM.add_f1()"
      ]
    }
  ],
  "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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}