{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "ZZzI3hGVHutJ" }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import sklearn\n", "from sklearn import datasets" ] }, { "cell_type": "code", "source": [ "#Loading the Dataset:\n", "iris = datasets.load_iris()\n", "X = iris.data\n", "Y = iris.target\n" ], "metadata": { "id": "Q7Ox6qxSHyVc" }, "execution_count": 2, "outputs": [] }, { "cell_type": "code", "source": [ "#Visualizing the Dataset:\n", "iris.target_names\n", "iris.feature_names\n", "iris.data[0:5]\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uQhaDQc6H1vy", "outputId": "b11bc3a3-bfcb-4279-c5bc-eaff4db16029" }, "execution_count": 3, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[5.1, 3.5, 1.4, 0.2],\n", " [4.9, 3. , 1.4, 0.2],\n", " [4.7, 3.2, 1.3, 0.2],\n", " [4.6, 3.1, 1.5, 0.2],\n", " [5. , 3.6, 1.4, 0.2]])" ] }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "code", "source": [ "#Splitting the Dataset:\n", "from sklearn.model_selection import train_test_split\n", "X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3)" ], "metadata": { "id": "NhMqEiZdH45K" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "#Building the AdaBoost Model:\n", "from sklearn.ensemble import AdaBoostClassifier\n", "adb = AdaBoostClassifier()\n", "model = adb.fit(X_train, Y_train)\n", "y_pred = model.predict(X_test)\n", "y_pred\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KtRFVN1KH788", "outputId": "5be24e5e-abb8-4ead-d9d0-09cb6c496ed8" }, "execution_count": 5, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([2, 0, 2, 0, 0, 0, 1, 1, 0, 0, 1, 2, 1, 0, 1, 1, 0, 2, 1, 1, 1, 1,\n", " 1, 0, 0, 1, 0, 2, 1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 0, 2, 0,\n", " 2])" ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "#Evaluating the Model\n", "from sklearn.metrics import confusion_matrix, classification_report, accuracy_score\n", "print(confusion_matrix(Y_test, y_pred))\n", "print(classification_report(Y_test, y_pred))\n", "print(accuracy_score(Y_test, y_pred))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6qTKfesVH-7t", "outputId": "5a2524db-2654-4cda-c338-f378e1b83694" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[16 0 0]\n", " [ 0 19 1]\n", " [ 0 1 8]]\n", " precision recall f1-score support\n", "\n", " 0 1.00 1.00 1.00 16\n", " 1 0.95 0.95 0.95 20\n", " 2 0.89 0.89 0.89 9\n", "\n", " accuracy 0.96 45\n", " macro avg 0.95 0.95 0.95 45\n", "weighted avg 0.96 0.96 0.96 45\n", "\n", "0.9555555555555556\n" ] } ] }, { "cell_type": "code", "source": [ "\"Using SVM as a base estimator\"\"\"\n", "from sklearn.svm import SVC\n", "from sklearn.ensemble import AdaBoostClassifier\n", "svc = SVC(probability = True, kernel='linear')\n", "adb = AdaBoostClassifier(n_estimators=100,base_estimator = svc, learning_rate= 0.01)" ], "metadata": { "id": "6dWkRlsnID2d" }, "execution_count": 7, "outputs": [] }, { "cell_type": "code", "source": [ "#Training and Predicting the AdaBoost Classifier\n", "model = adb.fit(X_train, Y_train)\n", "y_pred = model.predict(X_test)\n", "y_pred\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uKAL9CRiIJDC", "outputId": "d2f2590c-fada-40e6-8528-c7cbe5f60883" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.9/dist-packages/sklearn/ensemble/_base.py:166: FutureWarning: `base_estimator` was renamed to `estimator` in version 1.2 and will be removed in 1.4.\n", " warnings.warn(\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "array([2, 0, 2, 0, 0, 0, 1, 2, 0, 0, 1, 2, 1, 0, 2, 1, 0, 2, 1, 1, 1, 1,\n", " 1, 0, 0, 1, 0, 2, 1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 0, 2, 0,\n", " 2])" ] }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "code", "source": [ "#Evaluating the Model:\n", "print(confusion_matrix(Y_test, y_pred))\n", "print(classification_report(Y_test, y_pred))\n", "print(accuracy_score(Y_test, y_pred))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "18Kfv9LXIN3H", "outputId": "10c7f8c6-1ab3-4763-ea59-ba293f7c17a2" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[16 0 0]\n", " [ 0 18 2]\n", " [ 0 0 9]]\n", " precision recall f1-score support\n", "\n", " 0 1.00 1.00 1.00 16\n", " 1 1.00 0.90 0.95 20\n", " 2 0.82 1.00 0.90 9\n", "\n", " accuracy 0.96 45\n", " macro avg 0.94 0.97 0.95 45\n", "weighted avg 0.96 0.96 0.96 45\n", "\n", "0.9555555555555556\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "Jxe86GSrIRzh" }, "execution_count": null, "outputs": [] } ] }