{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "c2431c5e-4bec-18e6-bab0-65da7193d4fc"
      },
      "source": [
        "Simple leaf classification using random forest"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "647d3004-2dc0-c49f-d1fb-8e1cdc2658c0"
      },
      "outputs": [],
      "source": [
        "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n",
        "# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "library(ggplot2) # Data visualization\n",
        "library(readr) # CSV file I/O, e.g. the read_csv function\n",
        "library(randomForest)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "system(\"ls ../input\")\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3ddf1e84-f1cf-cb6f-a5eb-fb9aa0837b4b"
      },
      "outputs": [],
      "source": [
        "# prepare data\n",
        "train_org <- read.csv(\"../input/train.csv\")\n",
        "test_org <- read.csv(\"../input/test.csv\")\n",
        "train <- train_org[,-1]\n",
        "test <- test_org[,-1]\n",
        "\n",
        "# build model\n",
        "\n",
        "set.seed(10)\n",
        "\n",
        "forest <- randomForest(as.factor(train$species) ~ .,data=train, keep.forest = TRUE)\n",
        "\n",
        "# typing forest displays error rate of 1.82% which is quite good\n",
        "\n",
        "pred <- predict(forest,newdata = test, type = \"prob\")\n",
        "\n",
        "pred_frame <- as.data.frame(pred)\n",
        "\n",
        "results <- cbind(test_org[,1],pred_frame)\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}