{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMVPYCqo2QuGZuv8nftLGZQ",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/p82maavd/MIML/blob/main/src/miml/tutorial/classifiers_mimltoml.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install mimllearning\n",
        "\n",
        "from miml.transformation import ArithmeticTransformation, GeometricTransformation, MinMaxTransformation\n",
        "from miml.classifier import MIMLtoMLClassifier\n",
        "from miml.datasets import load_birds_train, load_birds_test\n",
        "\n",
        "dataset_train = load_birds_train()\n",
        "dataset_test = load_birds_test()"
      ],
      "metadata": {
        "id": "QNmyu82hKcVB",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "4666b5fc-63fb-4596-d647-415bb6f2148f"
      },
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting mimllearning\n",
            "  Downloading mimllearning-0.5.1-py3-none-any.whl (796 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m797.0/797.0 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hInstalling collected packages: mimllearning\n",
            "Successfully installed mimllearning-0.5.1\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Examples of ML Classifiers that can be used in the library from scikit-learn:"
      ],
      "metadata": {
        "id": "KwibNMJV9Gns"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### One vs Rest Classifier (Binary Relevance Classifier) [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html)"
      ],
      "metadata": {
        "id": "DMTHDNJ2Vz67"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.multiclass import OneVsRestClassifier\n",
        "from sklearn.svm import SVC\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(OneVsRestClassifier(SVC()), ArithmeticTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Z-8LL2vbWEBh",
        "outputId": "d5d7f975-9f7b-4c27-ae09-8a0c316ed6b9"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Decision Tree Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html)"
      ],
      "metadata": {
        "id": "BfO7dgESMX9O"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.tree import DecisionTreeClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(DecisionTreeClassifier(), ArithmeticTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZP8rdL8KKFO-",
        "outputId": "935718df-1073-4861-a597-5a2c538a5b6e"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Extra Tree Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.tree.ExtraTreeClassifier.html)"
      ],
      "metadata": {
        "id": "tci9srfrMgdb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.tree import ExtraTreeClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(ExtraTreeClassifier(), GeometricTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f0ff1b16-3538-41a1-d316-ca39abee9d74",
        "id": "GSsomGovLXDu"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Extra Trees Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html)"
      ],
      "metadata": {
        "id": "tFiaHceqNhId"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.ensemble import ExtraTreesClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(ExtraTreesClassifier(), ArithmeticTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "1d120426-41d6-40e5-c662-0e695db28c9f",
        "id": "lqWQmt4GLXhQ"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### K-Nearest Neighbors Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html)"
      ],
      "metadata": {
        "id": "ntyyKA6zN3VI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(KNeighborsClassifier(), MinMaxTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "42057f11-1073-4488-c228-fc2867981bbe",
        "id": "0CRlS6loLX3Z"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Multi-layer Perceptron Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html)"
      ],
      "metadata": {
        "id": "SYuoXFFFOThR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.neural_network import MLPClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(MLPClassifier(), GeometricTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "33da06f0-8ef6-44a8-8698-b0d6705de57c",
        "id": "lrh21g0yLYDq"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Radius Neighbors Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.RadiusNeighborsClassifier.html)"
      ],
      "metadata": {
        "id": "xQoVtETZOgKP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.neighbors import RadiusNeighborsClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(RadiusNeighborsClassifier(outlier_label=0), MinMaxTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "5b1e0c49-899c-4012-9790-3bf97645fe0b",
        "id": "rrrjmBUULYTc"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Random Forest Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html)"
      ],
      "metadata": {
        "id": "lU1O_FOnO9Q0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.ensemble import RandomForestClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(RandomForestClassifier(), ArithmeticTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "fde77204-845e-47d3-bd6c-d414cb5a3d3a",
        "id": "SUo12TmnLYiT"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Ridge Classifier [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifier.html)"
      ],
      "metadata": {
        "id": "-6oH8LQhPXte"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.linear_model import RidgeClassifier\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(RidgeClassifier(), GeometricTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "cd86bc32-74ba-40c9-d999-f4e09735b7e6",
        "id": "yW0erV4eM_IS"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Ridge Classifier CV [Reference](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifierCV.html)\n",
        "\n"
      ],
      "metadata": {
        "id": "J21jhGbAMQb8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.linear_model import RidgeClassifierCV\n",
        "\n",
        "classifier_ml = MIMLtoMLClassifier(RidgeClassifierCV(), MinMaxTransformation())\n",
        "classifier_ml.fit(dataset_train)\n",
        "print(\"Predicted Labels: \", classifier_ml.predict_bag(dataset_test.get_bag(\"366\")))\n",
        "print(\"True Labels:       \", dataset_test.get_bag(\"366\").get_labels()[0].astype(int))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9f67ac0e-de98-47b4-ba03-dc7f739f158f",
        "id": "qUXu_fhNNBYL"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Predicted Labels:  [[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]]\n",
            "True Labels:        [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0]\n"
          ]
        }
      ]
    }
  ]
}