{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nGetting started with Lab and scikit-learn\n=========================================\n\nThis example illustrates how Lab can be used to create and run a simple\nclassifier on the iris dataset.\n\nBegin by creating a new Lab Project:\n\n    >>> echo \"scikit-learn\" > requirements.txt\n    >>> lab init --name simple-iris\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import argparse\nfrom sklearn import datasets\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, precision_score\n\nfrom lab.experiment import Experiment\n\nparser = argparse.ArgumentParser('Test arguments')\n\nparser.add_argument('--n_estimators', type=int, dest='n_estimators')\nargs = parser.parse_args()\n\nn_estimators=args.n_estimators\n\nif n_estimators is None:\n    n_estimators=100\n    max_depth=2\n\nif __name__ == \"__main__\":\n    e = Experiment(dataset='iris_75')\n\n    @e.start_run\n    def train():\n        iris = datasets.load_iris()\n        X = iris.data\n        y = iris.target\n\n        X_train, X_test, y_train, y_test = train_test_split(X, y,\n                                                            test_size=0.25,\n                                                            random_state=42)\n\n        e.log_features(['Sepal Length', 'Sepal Width', 'Petal Length',\n                        'Petal Width'])\n        clf = RandomForestClassifier(n_estimators=n_estimators)\n\n        clf.fit(X_train, y_train)\n\n        y_pred = clf.predict(X_test)\n        accuracy = accuracy_score(y_test, y_pred)\n        precision = precision_score(y_test, y_pred, average = 'macro')\n\n        e.log_metric('accuracy_score', accuracy)\n        e.log_metric('precision_score', precision)\n\n        e.log_parameter('n_estimators', n_estimators)\n        e.log_parameter('max_depth', max_depth)\n\n        e.log_model('randomforest', clf)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "After execute training script through the `lab run` command.\n\n>>> lab run train.py\n>>> lab ls\n\n"
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}