{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Ensemble Methods.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "-mCmaGdYCtBc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Load Library\n",
        "from sklearn.datasets import make_moons\n",
        "from sklearn.metrics import accuracy_score\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.tree import DecisionTreeClassifier\n",
        "from sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier,GradientBoostingClassifier"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4ja4yb4_CzlY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Step1: Create data set\n",
        "X, y = make_moons(n_samples=10000, noise=.5, random_state=0)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WbIUBF8pdXUF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Step2: Split the training test set\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8vRma5CzC1id",
        "colab_type": "code",
        "outputId": "47d557ac-f8d7-4b3d-8fb8-94893afbc5fd",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "# Step 3: Fit a Decision Tree model as comparison\n",
        "clf = DecisionTreeClassifier()\n",
        "clf.fit(X_train, y_train)\n",
        "print(\" Train Accuracy\", accuracy_score(y_train, clf.predict(X_train)))\n",
        "print(\" Test Accuracy\", accuracy_score(y_test, clf.predict(X_test)))"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            " Train Accuracy 1.0\n",
            " Test Accuracy 0.751\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LeoG6gvlC1oR",
        "colab_type": "code",
        "outputId": "42772dfc-410f-474d-c442-a940260ac783",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "# Step 4: Fit a Random Forest model, \" compared to \"Decision Tree model, accuracy go up by 5%\n",
        "clf = RandomForestClassifier(n_estimators=100, max_features=\"auto\",random_state=0)\n",
        "clf.fit(X_train, y_train)\n",
        "print(\" Train Accuracy\", accuracy_score(y_train, clf.predict(X_train)))\n",
        "print(\" Test Accuracy\", accuracy_score(y_test, clf.predict(X_test)))"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            " Train Accuracy 1.0\n",
            " Test Accuracy 0.7965\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h_ntW2CiC1ll",
        "colab_type": "code",
        "outputId": "753a8327-e25f-4c2d-9d39-e3ee62c13192",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "# Step 5: Fit a AdaBoost model, \" compared to \"Decision Tree model, accuracy go up by 10%\n",
        "clf = AdaBoostClassifier(n_estimators=100)\n",
        "clf.fit(X_train, y_train)\n",
        "print(\" Train Accuracy\", accuracy_score(y_train, clf.predict(X_train)))\n",
        "print(\" Test Accuracy\", accuracy_score(y_test, clf.predict(X_test)))"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            " Train Accuracy 0.825625\n",
            " Test Accuracy 0.833\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GXw2cMWuC92S",
        "colab_type": "code",
        "outputId": "ce935fdf-df0a-4405-e6d3-a41af02a7ae6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "source": [
        "# Step 6: Fit a Gradient Boosting model, \" compared to \"Decision Tree model, accuracy go up by 10%\n",
        "clf = GradientBoostingClassifier(n_estimators=100)\n",
        "clf.fit(X_train, y_train)\n",
        "print(\" Train Accuracy\", accuracy_score(y_train, clf.predict(X_train)))\n",
        "print(\" Test Accuracy\", accuracy_score(y_test, clf.predict(X_test)))"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            " Train Accuracy 0.829875\n",
            " Test Accuracy 0.8335\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QY37GWzl9CQD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}