{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "importing all necessary libraries and the dataset" ], "metadata": { "id": "uCCi_sxuj6LA" } }, { "cell_type": "code", "execution_count": 26, "metadata": { "id": "w3PM3P2WaPnH" }, "outputs": [], "source": [ "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.ensemble import BaggingClassifier\n", "from sklearn.ensemble import AdaBoostClassifier\n", "from sklearn.ensemble import StackingClassifier\n", "from sklearn.datasets import load_breast_cancer\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.svm import SVC\n", "\n", "cancer_dataset = load_breast_cancer()\n", "X = cancer_dataset.data\n", "y = cancer_dataset.target\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" ] }, { "cell_type": "markdown", "source": [ "# BaggingClassifier" ], "metadata": { "id": "g_OJ1OT4hzJg" } }, { "cell_type": "code", "source": [ "base_model = DecisionTreeClassifier(max_depth=5)\n", "\n", "bagging_classifier_model = BaggingClassifier(base_model, max_samples=0.8, max_features=0.8)\n", "\n", "bagging_classifier_model.fit(X_train, y_train)\n", "\n", "accuracy = bagging_classifier_model.score(X_test, y_test)\n", "print(\"Accuracy: \", accuracy)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xFxb1KwshxFB", "outputId": "f65e4d1d-ff70-4c0a-bf96-ca2094af049f" }, "execution_count": 36, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy: 0.9649122807017544\n" ] } ] }, { "cell_type": "markdown", "source": [ "# BoostingClassifier" ], "metadata": { "id": "dTVe1-jLh74o" } }, { "cell_type": "code", "source": [ "base_model = DecisionTreeClassifier(max_depth=5)\n", "\n", "boosting_classifier_model = AdaBoostClassifier(base_model)\n", "\n", "boosting_classifier_model.fit(X_train, y_train)\n", "\n", "accuracy = boosting_classifier_model.score(X_test, y_test)\n", "print(\"Accuracy: \", accuracy)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Rasckig1ckCi", "outputId": "400ef180-a5d7-4739-87c1-7eca59cf7a1b" }, "execution_count": 31, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy: 0.9649122807017544\n" ] } ] }, { "cell_type": "markdown", "source": [ "# StackingClassifier" ], "metadata": { "id": "HwgqJ7D_h8p_" } }, { "cell_type": "code", "source": [ "logistic_model = LogisticRegression(max_iter=5000)\n", "decision_tree_model = DecisionTreeClassifier()\n", "svc_model = SVC()\n", "\n", "stack = StackingClassifier(\n", " estimators=[('lr', logistic_model), ('dt', decision_tree_model), ('svc', svc_model)],\n", " final_estimator=LogisticRegression(random_state=1)\n", ")\n", "\n", "stack.fit(X_train, y_train)\n", "\n", "accuracy = stack.score(X_test, y_test)\n", "print('Accuracy: %.2f' % accuracy)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "74Sdd84xdJ7E", "outputId": "9629ef74-bcb1-46c4-dc7d-0a70f21f4ea1" }, "execution_count": 38, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Accuracy: 0.96\n" ] } ] } ] }