{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# DL8.5 classifier : python side iterative search\nIterative search is the idea that the algorithm starts with finding an optimal \nshallow tree, and then uses the quality of this tree to bound the quality of \ndeeper trees. This class shows how to perform this type of search by repeatedly \ncalling the DL8.5 algorithm.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport time\nfrom dl85 import DL85Classifier\n\ndataset = np.genfromtxt(\"../datasets/anneal.txt\", delimiter=' ')\nX, y = dataset[:, 1:], dataset[:, 0]\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)\n\n\nprint(\"###########################################################################\\n\"\n      \"#      DL8.5 default classifier using python-based iterative search       #\\n\"\n      \"###########################################################################\")\nstart = time.perf_counter()\nerror = 0  # default max error value expressing no bound\nclf = None\nremaining_time = 600\nfor i in range(1, 3):  # max depth = 2\n    clf = DL85Classifier(max_depth=i, max_error=error, time_limit=remaining_time)\n    clf.fit(X_train, y_train)\n    error = clf.error_\n    remaining_time -= clf.runtime_\nduration = time.perf_counter() - start\nprint(\"Model built. Duration of building =\", round(duration, 4))\ny_pred = clf.predict(X_test)\nprint(\"Confusion Matrix below\")\nprint(confusion_matrix(y_test, y_pred))\nprint(\"Accuracy DL8.5 on training set =\", round(clf.accuracy_, 4))\nprint(\"Accuracy DL8.5 on test set =\", round(accuracy_score(y_test, y_pred), 4))"
      ]
    }
  ],
  "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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}