{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "864483ce",
      "metadata": {
        "id": "864483ce"
      },
      "source": [
        "# Perceptron vs Multilayer Perceptron (MLP)\n",
        "\n",
        "This notebook demonstrates the application of a basic **Artificial Neural Network** using:\n",
        "\n",
        "- **Perceptron** (Single Layer Neural Network)\n",
        "- **Multilayer Perceptron (MLP)**\n",
        "\n",
        "We will use the popular **Iris Dataset** to show the difference between these models.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "fa647fdc",
      "metadata": {
        "id": "fa647fdc"
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import load_iris\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn.linear_model import Perceptron\n",
        "from sklearn.neural_network import MLPClassifier\n",
        "from sklearn.metrics import accuracy_score\n",
        "\n",
        "# Load the iris dataset\n",
        "iris = load_iris()\n",
        "X, y = iris.data, iris.target\n",
        "\n",
        "# For simplicity, let's convert this to a binary classification problem\n",
        "X = X[y != 2]\n",
        "y = y[y != 2]\n",
        "\n",
        "# Train/test split\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)\n",
        "\n",
        "# Standardize the features\n",
        "scaler = StandardScaler()\n",
        "X_train = scaler.fit_transform(X_train)\n",
        "X_test = scaler.transform(X_test)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "1b60dc68",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1b60dc68",
        "outputId": "3eb7facf-4608-4dde-803d-9f4dc177a19c"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Perceptron Accuracy: 1.0\n"
          ]
        }
      ],
      "source": [
        "# Train a Perceptron\n",
        "perceptron = Perceptron()\n",
        "perceptron.fit(X_train, y_train)\n",
        "y_pred_perceptron = perceptron.predict(X_test)\n",
        "\n",
        "# Evaluate\n",
        "acc_perceptron = accuracy_score(y_test, y_pred_perceptron)\n",
        "print(\"Perceptron Accuracy:\", acc_perceptron)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "b61f033e",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "b61f033e",
        "outputId": "129a9bd0-0660-4bad-ab6c-9866d05bc3f5"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Multilayer Perceptron Accuracy: 1.0\n"
          ]
        }
      ],
      "source": [
        "# Train a Multilayer Perceptron\n",
        "mlp = MLPClassifier(hidden_layer_sizes=(10,), max_iter=1000, random_state=42)\n",
        "mlp.fit(X_train, y_train)\n",
        "y_pred_mlp = mlp.predict(X_test)\n",
        "\n",
        "# Evaluate\n",
        "acc_mlp = accuracy_score(y_test, y_pred_mlp)\n",
        "print(\"Multilayer Perceptron Accuracy:\", acc_mlp)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "67395d66",
      "metadata": {
        "id": "67395d66"
      },
      "source": [
        "## Summary\n",
        "\n",
        "- The **Perceptron** is a basic linear classifier and works well on linearly separable data.\n",
        "- The **MLP** can model complex non-linear decision boundaries due to its hidden layers.\n",
        "\n",
        "We can see from the accuracy scores that the MLP performs better due to its greater capacity.\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "language_info": {
      "name": "python"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}