Skip to content

Instantly share code, notes, and snippets.

@ashishpatel26
Created March 16, 2020 17:22
Show Gist options
  • Save ashishpatel26/c65b7a1e331fddf3b5bea093f0104283 to your computer and use it in GitHub Desktop.
Save ashishpatel26/c65b7a1e331fddf3b5bea093f0104283 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Boosting.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyMpAc4gIa1BxQ2gzhBdLXbi",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/ashishpatel26/Ensemble-Learning-Algorithm-Medium/blob/master/Boosting.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YBPbSenYUxQ0",
"colab_type": "text"
},
"source": [
"# Import All the required packages from sklearn"
]
},
{
"cell_type": "code",
"metadata": {
"id": "PwNQcuuNU0aL",
"colab_type": "code",
"colab": {}
},
"source": [
"from sklearn import model_selection\n",
"from sklearn.datasets import load_iris\n",
"from sklearn.ensemble import AdaBoostClassifier # Boosting Algorithm\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"import numpy as np"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "KB8d4w0XU--C",
"colab_type": "text"
},
"source": [
"#Load data "
]
},
{
"cell_type": "code",
"metadata": {
"id": "PFk9OJl2U7wX",
"colab_type": "code",
"colab": {}
},
"source": [
"iris = load_iris()\n",
"X = iris.data\n",
"Y = iris.target"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "6up7wHGvVGV5",
"colab_type": "text"
},
"source": [
"\n",
"#Split data in training and testing set "
]
},
{
"cell_type": "code",
"metadata": {
"id": "RNGsxs69VC2L",
"colab_type": "code",
"colab": {}
},
"source": [
"X_fit, X_eval, y_fit, y_test= model_selection.train_test_split( X, Y, test_size=0.20, random_state=1)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "T2IhUCtkVKnq",
"colab_type": "text"
},
"source": [
"#Define a decision tree classifier"
]
},
{
"cell_type": "code",
"metadata": {
"id": "usB03awyVIrK",
"colab_type": "code",
"colab": {}
},
"source": [
"cart = DecisionTreeClassifier()\n",
"num_trees = 25"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "i3ekolb-VRr3",
"colab_type": "text"
},
"source": [
"#Create classification model for bagging"
]
},
{
"cell_type": "code",
"metadata": {
"id": "JxU6Hv0RVN7i",
"colab_type": "code",
"colab": {}
},
"source": [
"model = AdaBoostClassifier(base_estimator=cart, n_estimators=num_trees, learning_rate = 0.1)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "F9ENpvjbVYaV",
"colab_type": "text"
},
"source": [
"#Train Classification model"
]
},
{
"cell_type": "code",
"metadata": {
"id": "fOQzerhHVZIA",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 289
},
"outputId": "a75d90f7-be8c-471c-d992-533198dea2ac"
},
"source": [
"model.fit(X_fit, y_fit)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"AdaBoostClassifier(algorithm='SAMME.R',\n",
" base_estimator=DecisionTreeClassifier(ccp_alpha=0.0,\n",
" class_weight=None,\n",
" criterion='gini',\n",
" max_depth=None,\n",
" max_features=None,\n",
" max_leaf_nodes=None,\n",
" min_impurity_decrease=0.0,\n",
" min_impurity_split=None,\n",
" min_samples_leaf=1,\n",
" min_samples_split=2,\n",
" min_weight_fraction_leaf=0.0,\n",
" presort='deprecated',\n",
" random_state=None,\n",
" splitter='best'),\n",
" learning_rate=0.1, n_estimators=25, random_state=None)"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OhKjXBGRVeaD",
"colab_type": "text"
},
"source": [
"# Test trained model over test set"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0P3-tonoVbsi",
"colab_type": "code",
"colab": {}
},
"source": [
"pred_label = model.predict(X_eval)\n",
"nnz = np.float(np.shape(y_test)[0] - np.count_nonzero(pred_label - y_test))\n",
"acc = 100*nnz/np.shape(y_test)[0]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "o8hkKajvVot6",
"colab_type": "text"
},
"source": [
"#Print accuracy of the model"
]
},
{
"cell_type": "code",
"metadata": {
"id": "nq4d9YG0VhSL",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "dbb47258-dcc1-44ed-efb3-c068d65c5252"
},
"source": [
"print('accuracy is: '+str(acc))"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"accuracy is: 96.66666666666667\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment