{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6a53b426-a1c5-485b-807c-02c3bddb5a85",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5fd2a1b-ac9e-474a-a04b-d583a48d3b3f",
   "metadata": {},
   "source": [
    "# Dzień 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cdb50f3-a713-4f9d-b58d-5169529cc736",
   "metadata": {},
   "source": [
    "## Najpopularniejsze modele uczenia maszynowego\n",
    "### Regresja liniowa\n",
    " - model przewiduje wartość liczbową na podstawie wartości wejściowych\n",
    "### Regresja logistyczna\n",
    " - mimo nazwy, służy głownie do klasyfikacji binarnej - prawdopodobieństwo przynależności do grupy\n",
    "   - np. w jakim stopniu prawdopodobieństwa dane objawy pasują do choroby, albo dane finansowe pasują do grupy \"dostanie kredyt\"\n",
    " -  \n",
    "## Drzewa decyzyjne i last losowe\n",
    "### Drzewo decyzyjne\n",
    " - struktura przypominająca drzewo, gdzie każdy węzeł to pytanie o dane a gaęzie to odpowiedzi na to pytanie\n",
    "### Las losowy\n",
    " - grupa wielu drzew decyzyjnych, gdzie każde drzewo jest uczone na losowym podzbiorze cech\n",
    " - końcowa decyzja jest wynikiem głosowania drzew, co poprawia dokładność\n",
    "### Gradient boosting\n",
    " - technika polegająca na tym, że bierzemy słabe modele (zwykle drzewa) i łączymy z innymi modelami, które koncentrują się na poprawie błędów poprzenich modeli\n",
    "\n",
    "## Wzmacnianie \n",
    " - środowisko - przestreń, w której agent działa\n",
    " - agent - autonomiczny program, który podejmuje decyzje na podstawie środowiska\n",
    " - nagroda - sygnał informujący agenta o jakości akcji\n",
    " - akcja - działanie podjęte przez agenta, które wpływa na środowisko\n",
    "Przykład: Środowisko to plansza, duchy, owoce i tabletki, nagroda to owoce, agent to PacMan, akcja to poruszanie się.\n",
    "\n",
    "## Support Vector Machines - SVM \n",
    " - potężne narzędzie do klasyfikacji\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1edbb5f7-577a-4460-886e-482464157465",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.colors import ListedColormap\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# To check recent matplotlib compatibility\n",
    "import matplotlib\n",
    "from distutils.version import LooseVersion\n",
    "\n",
    "\n",
    "def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):\n",
    "\n",
    "    # setup marker generator and color map\n",
    "    markers = ('o', 's', '^', 'v', '<')\n",
    "    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')\n",
    "    cmap = ListedColormap(colors[:len(np.unique(y))])\n",
    "\n",
    "    # plot the decision surface\n",
    "    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1\n",
    "    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1\n",
    "    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),\n",
    "                           np.arange(x2_min, x2_max, resolution))\n",
    "    lab = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)\n",
    "    lab = lab.reshape(xx1.shape)\n",
    "    plt.contourf(xx1, xx2, lab, alpha=0.3, cmap=cmap)\n",
    "    plt.xlim(xx1.min(), xx1.max())\n",
    "    plt.ylim(xx2.min(), xx2.max())\n",
    "\n",
    "    # plot class examples\n",
    "    for idx, cl in enumerate(np.unique(y)):\n",
    "        plt.scatter(x=X[y == cl, 0],\n",
    "                    y=X[y == cl, 1],\n",
    "                    alpha=0.8,\n",
    "                    c=colors[idx],\n",
    "                    marker=markers[idx],\n",
    "                    label=f'Class {cl}',\n",
    "                    edgecolor='black')\n",
    "     # highlight test examples\n",
    "    if test_idx:\n",
    "        # plot all examples\n",
    "        X_test, y_test = X[test_idx, :], y[test_idx]\n",
    "\n",
    "        plt.scatter(X_test[:, 0],\n",
    "                    X_test[:, 1],\n",
    "                    c='none',\n",
    "                    edgecolor='black',\n",
    "                    alpha=1.0,\n",
    "                    linewidth=1,\n",
    "                    marker='o',\n",
    "                    s=100,\n",
    "                    label='Test set')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1169616f-5bcf-458b-a5a2-12d642840167",
   "metadata": {},
   "source": [
    "## Import danych Irys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5987fc03-1a75-45bb-b154-7fb1d8c5c1e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "iris = datasets.load_iris()\n",
    "X = iris.data[:, [2, 3]]\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0de01538-5c3f-4110-bb9d-ecdbb7f0af1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=4, stratify=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "00d65b25-a305-4409-8daf-8bd4e3cd8005",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "sc = StandardScaler()\n",
    "sc.fit(X_train)\n",
    "X_train_std = sc.transform(X_train)\n",
    "X_test_std = sc.transform(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1dea55c-bafc-4362-b751-b90d3c8549e9",
   "metadata": {},
   "source": [
    "## Perceptron"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5cecc1db-c8b7-4153-897e-9a4a216e936e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-- Epoch 1\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 105, Avg. loss: 0.001261\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 2\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 210, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 3\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 315, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 4\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 420, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 5\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 525, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 6\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 630, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 7\n",
      "Norm: 0.18, NNZs: 2, Bias: -0.100000, T: 735, Avg. loss: 0.000000\n",
      "Total training time: 0.00 seconds.\n",
      "Convergence after 7 epochs took 0.00 seconds\n",
      "-- Epoch 1\n",
      "Norm: 0.28, NNZs: 2, Bias: -0.100000, T: 105, Avg. loss: 0.054884\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 2\n",
      "Norm: 0.28, NNZs: 2, Bias: -0.000000, T: 210, Avg. loss: 0.050780\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 3\n",
      "Norm: 0.26, NNZs: 2, Bias: -0.000000, T: 315, Avg. loss: 0.051955\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 4\n",
      "Norm: 0.35, NNZs: 2, Bias: -0.000000, T: 420, Avg. loss: 0.051103\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 5\n",
      "Norm: 0.38, NNZs: 2, Bias: 0.100000, T: 525, Avg. loss: 0.046239\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 6\n",
      "Norm: 0.46, NNZs: 2, Bias: -0.000000, T: 630, Avg. loss: 0.054860\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 7\n",
      "Norm: 0.47, NNZs: 2, Bias: -0.100000, T: 735, Avg. loss: 0.058374\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 8\n",
      "Norm: 0.57, NNZs: 2, Bias: -0.100000, T: 840, Avg. loss: 0.054651\n",
      "Total training time: 0.01 seconds.\n",
      "-- Epoch 9\n",
      "Norm: 0.41, NNZs: 2, Bias: -0.200000, T: 945, Avg. loss: 0.054692\n",
      "Total training time: 0.01 seconds.\n",
      "-- Epoch 10\n",
      "Norm: 0.45, NNZs: 2, Bias: -0.100000, T: 1050, Avg. loss: 0.047006\n",
      "Total training time: 0.01 seconds.\n",
      "Convergence after 10 epochs took 0.01 seconds\n",
      "-- Epoch 1\n",
      "Norm: 0.29, NNZs: 2, Bias: -0.200000, T: 105, Avg. loss: 0.006627\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 2\n",
      "Norm: 0.42, NNZs: 2, Bias: -0.200000, T: 210, Avg. loss: 0.002795\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 3\n",
      "Norm: 0.45, NNZs: 2, Bias: -0.300000, T: 315, Avg. loss: 0.002319\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 4\n",
      "Norm: 0.46, NNZs: 2, Bias: -0.300000, T: 420, Avg. loss: 0.002920\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 5\n",
      "Norm: 0.47, NNZs: 2, Bias: -0.300000, T: 525, Avg. loss: 0.004747\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 6\n",
      "Norm: 0.55, NNZs: 2, Bias: -0.300000, T: 630, Avg. loss: 0.000482\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 7\n",
      "Norm: 0.53, NNZs: 2, Bias: -0.400000, T: 735, Avg. loss: 0.002328\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 8\n",
      "Norm: 0.56, NNZs: 2, Bias: -0.400000, T: 840, Avg. loss: 0.002328\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 9\n",
      "Norm: 0.57, NNZs: 2, Bias: -0.400000, T: 945, Avg. loss: 0.002868\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 10\n",
      "Norm: 0.58, NNZs: 2, Bias: -0.400000, T: 1050, Avg. loss: 0.005004\n",
      "Total training time: 0.00 seconds.\n",
      "-- Epoch 11\n",
      "Norm: 0.49, NNZs: 2, Bias: -0.500000, T: 1155, Avg. loss: 0.004755\n",
      "Total training time: 0.00 seconds.\n",
      "Convergence after 11 epochs took 0.01 seconds\n",
      "Scoring: 0.87\n"
     ]
    }
   ],
   "source": [
    "print('Scoring: %.2f' % ppn.score(X_test_std, y_test))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "129fcd15-b8c9-4455-9f25-d164ab5cf81c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7d03e4f03cb0>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48824 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_combined_std = np.vstack((X_train_std, X_test_std))\n",
    "y_combined = np.hstack((y_train, y_test))\n",
    "plot_decision_regions(X=X_combined_std,y=y_combined,\n",
    "                      classifier=ppn,\n",
    "                      test_idx=range(105,150))\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b636369-1054-49d1-99fd-d76ff3d32e58",
   "metadata": {},
   "source": [
    "## Logistic Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "38b15a6d-67e1-4d45-8dbe-97514e0c490e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scoring: 0.96\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "lr = LogisticRegression(random_state=1)\n",
    "lr.fit(X_train_std, y_train)\n",
    "print('Scoring: %.2f' % lr.score(X_test_std, y_test))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0c72b7a-b92b-4678-8bdb-3b6ca4c201ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7d03e4aefb10>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 51696 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_combined_std = np.vstack((X_train_std, X_test_std))\n",
    "y_combined = np.hstack((y_train, y_test))\n",
    "plot_decision_regions(X=X_combined_std,y=y_combined,\n",
    "                      classifier=lr,\n",
    "                      test_idx=range(105,150))\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6740e8d-87c5-46a0-bb0f-66764c9e6caf",
   "metadata": {},
   "source": [
    "## Maszyna wektorów nośnych - SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e6a3ac4-b047-4ce5-ac19-abbe3817e2f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scoring: 0.98\n"
     ]
    }
   ],
   "source": [
    "from sklearn.svm import SVC\n",
    "# svm = SVC(kernel='linear', random_state=1, C=1000000.0)\n",
    "# svm = SVC(kernel='poly', degree=3, random_state=1, C=1000000.0)\n",
    "svm = SVC(kernel='rbf', gamma=2, random_state=1, C=50.0)\n",
    "svm.fit(X_train_std, y_train)\n",
    "print('Scoring: %.2f' % svm.score(X_test_std, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "63579d74-52bf-41ad-b6eb-9b8bf74099d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVM - Bias: [-0.21518701 -0.3724333  -0.97234964]\n"
     ]
    }
   ],
   "source": [
    "print(\"SVM - Bias:\", svm.intercept_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0486e529-a450-4ce8-964a-925660449fc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7d03e29e2c10>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 55392 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot the decision regions with SVC\n",
    "plot_decision_regions(X=X_combined_std, y=y_combined,\n",
    "                      classifier=svm,\n",
    "                      test_idx=range(105, 150))\n",
    "plt.title(\"Support Vector Machine\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1981b740-80e0-42c5-8375-bfbbf9705a79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Logistic Regression - Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        15\n",
      "           1       0.93      0.93      0.93        15\n",
      "           2       0.93      0.93      0.93        15\n",
      "\n",
      "    accuracy                           0.96        45\n",
      "   macro avg       0.96      0.96      0.96        45\n",
      "weighted avg       0.96      0.96      0.96        45\n",
      "\n",
      "SVM - Classification Report:\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       1.00      1.00      1.00        15\n",
      "           1       0.94      1.00      0.97        15\n",
      "           2       1.00      0.93      0.97        15\n",
      "\n",
      "    accuracy                           0.98        45\n",
      "   macro avg       0.98      0.98      0.98        45\n",
      "weighted avg       0.98      0.98      0.98        45\n",
      "\n",
      "K-Means - Classification Report:\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'kmeans' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[12]\u001b[39m\u001b[32m, line 13\u001b[39m\n\u001b[32m     11\u001b[39m \u001b[38;5;66;03m# Print the classification report for the K-Means model\u001b[39;00m\n\u001b[32m     12\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mK-Means - Classification Report:\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m13\u001b[39m \u001b[38;5;28mprint\u001b[39m(classification_report(y_test, \u001b[43mkmeans\u001b[49m.predict(X_test_std)))\n",
      "\u001b[31mNameError\u001b[39m: name 'kmeans' is not defined"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "\n",
    "# Print the classification report for the Logistic Regression model\n",
    "print(\"Logistic Regression - Classification Report:\")\n",
    "print(classification_report(y_test, lr.predict(X_test_std)))\n",
    "\n",
    "# Print the classification report for the SVM model\n",
    "print(\"SVM - Classification Report:\")\n",
    "print(classification_report(y_test, svm.predict(X_test_std)))\n",
    "\n",
    "# Print the classification report for the K-Means model\n",
    "print(\"K-Means - Classification Report:\")\n",
    "print(classification_report(y_test, kmeans.predict(X_test_std)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "45dbacb1-08ba-4e54-94ff-f2347b339a96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.96\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "tree_model = DecisionTreeClassifier(criterion=\"entropy\", max_depth=4, random_state=1)\n",
    "tree_model.fit(X_train, y_train)\n",
    "y_pred = tree_model.predict(X_test)\n",
    "print(f'Accuracy: {accuracy_score(y_test, y_pred):.2f}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "270cffdb-b14d-4517-ad2d-f85415baeb40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7d03e25aca50>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 50092 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_combined = np.vstack((X_train, X_test))\n",
    "X_combined_std = np.vstack((X_train_std, X_test_std))\n",
    "y_combined = np.hstack((y_train, y_test))\n",
    "plot_decision_regions(X=X_combined, y=y_combined,\n",
    "                      classifier=tree_model,\n",
    "                      test_idx=range(105, 150))\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "55ff8530-3d52-4234-80da-3901478319ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Text(0.4, 0.9, 'x[1] <= 0.8\\nentropy = 1.585\\nsamples = 105\\nvalue = [35, 35, 35]'),\n",
       " Text(0.3, 0.7, 'entropy = 0.0\\nsamples = 35\\nvalue = [35, 0, 0]'),\n",
       " Text(0.35, 0.8, 'True  '),\n",
       " Text(0.5, 0.7, 'x[1] <= 1.65\\nentropy = 1.0\\nsamples = 70\\nvalue = [0, 35, 35]'),\n",
       " Text(0.45, 0.8, '  False'),\n",
       " Text(0.2, 0.5, 'x[0] <= 4.95\\nentropy = 0.406\\nsamples = 37\\nvalue = [0, 34, 3]'),\n",
       " Text(0.1, 0.3, 'entropy = 0.0\\nsamples = 33\\nvalue = [0, 33, 0]'),\n",
       " Text(0.3, 0.3, 'x[1] <= 1.55\\nentropy = 0.811\\nsamples = 4\\nvalue = [0, 1, 3]'),\n",
       " Text(0.2, 0.1, 'entropy = 0.0\\nsamples = 2\\nvalue = [0, 0, 2]'),\n",
       " Text(0.4, 0.1, 'entropy = 1.0\\nsamples = 2\\nvalue = [0, 1, 1]'),\n",
       " Text(0.8, 0.5, 'x[0] <= 4.85\\nentropy = 0.196\\nsamples = 33\\nvalue = [0, 1, 32]'),\n",
       " Text(0.7, 0.3, 'x[0] <= 4.65\\nentropy = 0.918\\nsamples = 3\\nvalue = [0, 1, 2]'),\n",
       " Text(0.6, 0.1, 'entropy = 0.0\\nsamples = 1\\nvalue = [0, 0, 1]'),\n",
       " Text(0.8, 0.1, 'entropy = 1.0\\nsamples = 2\\nvalue = [0, 1, 1]'),\n",
       " Text(0.9, 0.3, 'entropy = 0.0\\nsamples = 30\\nvalue = [0, 0, 30]')]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 61588 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn import tree\n",
    "names = print(iris.feature_names)\n",
    "tree.plot_tree(tree_model, feature_names=names,filled=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "858216bf-d561-476b-85ad-f197eea0c6b7",
   "metadata": {},
   "source": [
    "## las losowy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b4c0a6d3-19f1-4f6a-a988-e9add27c5619",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scoring: 0.98\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "rfc = RandomForestClassifier(n_estimators=25, random_state=1, n_jobs=3)\n",
    "rfc.fit(X_train, y_train)\n",
    "print('Scoring: %.2f' % rfc.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "15c30272-131c-4c2d-8028-af30b557827f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x70cc2fa79e50>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 55772 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_combined = np.vstack((X_train, X_test))\n",
    "y_combined = np.hstack((y_train, y_test))\n",
    "plot_decision_regions(X=X_combined, y=y_combined,\n",
    "                      classifier=rfc,\n",
    "                      test_idx=range(105, 150))\n",
    "plt.title(\"Random Forest Classifier\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5a9cde25-eb8c-4a2e-9b43-1844b6ef429b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Text(0.4, 0.9, 'x[1] <= 0.8\\ngini = 0.661\\nsamples = 64\\nvalue = [36.0, 29.0, 40.0]'),\n",
       " Text(0.2, 0.7, 'gini = 0.0\\nsamples = 20\\nvalue = [36, 0, 0]'),\n",
       " Text(0.30000000000000004, 0.8, 'True  '),\n",
       " Text(0.6, 0.7, 'x[1] <= 1.6\\ngini = 0.487\\nsamples = 44\\nvalue = [0, 29, 40]'),\n",
       " Text(0.5, 0.8, '  False'),\n",
       " Text(0.4, 0.5, 'x[1] <= 1.35\\ngini = 0.121\\nsamples = 21\\nvalue = [0, 29, 2]'),\n",
       " Text(0.2, 0.3, 'gini = 0.0\\nsamples = 14\\nvalue = [0, 21, 0]'),\n",
       " Text(0.6, 0.3, 'x[0] <= 4.95\\ngini = 0.32\\nsamples = 7\\nvalue = [0, 8, 2]'),\n",
       " Text(0.4, 0.1, 'gini = 0.0\\nsamples = 5\\nvalue = [0, 8, 0]'),\n",
       " Text(0.8, 0.1, 'gini = 0.0\\nsamples = 2\\nvalue = [0, 0, 2]'),\n",
       " Text(0.8, 0.5, 'gini = 0.0\\nsamples = 23\\nvalue = [0, 0, 38]')]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 69100 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tree.plot_tree(rfc.estimators_[0], feature_names=names,filled=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b71cd90-c2a9-45eb-8976-915e11e4df8e",
   "metadata": {},
   "source": [
    "## K-Means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e0b2c126-95bc-4b25-8e60-77951a381f8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scoring: -5.53\n"
     ]
    }
   ],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "kmeans = KMeans(n_clusters=3, random_state=1)\n",
    "kmeans.fit(X_train_std)\n",
    "print('Scoring: %.2f' % kmeans.score(X_test_std, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "20c0c3e9-71fd-42ea-9077-2a4e3f363b2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x70cc2e227250>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 56548 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot the decision regions with KMeans\n",
    "plot_decision_regions(X=X_combined_std, y=y_combined,\n",
    "                      classifier=kmeans,\n",
    "                      test_idx=range(105, 150))\n",
    "plt.title(\"K-Means Clustering\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d11ae63-0604-4297-8c1d-fe58b32e11a5",
   "metadata": {},
   "source": [
    "## KNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d623920b-103a-4f7f-8a04-6ca5c89ad5bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy: 0.96\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier(n_neighbors=5, p=2)\n",
    "knn.fit(X_train_std, y_train)\n",
    "\n",
    "# Make predictions on the test data\n",
    "y_pred = knn.predict(X_test_std)\n",
    "print(f'Accuracy: {accuracy_score(y_test, y_pred):.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b3177642-a4d1-4547-a747-9b28345800c1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7d03e2356850>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 57460 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot the decision regions with KNN\n",
    "X_combined_std = np.vstack((X_train_std, X_test_std))\n",
    "y_combined = np.hstack((y_train, y_test))\n",
    "\n",
    "plot_decision_regions(X=X_combined_std, y=y_combined,\n",
    "                      classifier=knn,\n",
    "                      test_idx=range(105, 150))\n",
    "plt.title(\"K-Nearest Neighbors (KNN)\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a521408a-7441-42b6-a91f-4d36fc1c72bf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4035ea37-1ea9-4fc7-9a5e-936d666cd576",
   "metadata": {},
   "source": [
    "# Budowanie datasetów z własnych danych"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ab18d90f-803d-4bed-9bf9-0b407a29ed80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0   1     2       3\n",
      "0      red   M  18.5  class1\n",
      "1     blue   L  21.0  class2\n",
      "2    green  XL  22.1  class2\n",
      "3   yellow   S  16.2  class1\n",
      "4   purple  XL  24.0  class3\n",
      "5      red   M  18.5  class1\n",
      "6     blue   L  21.0  class2\n",
      "7    green  XL  22.1  class2\n",
      "8   yellow   S  16.2  class1\n",
      "9   purple  XL  24.0  class3\n",
      "10     red   M  18.5  class1\n",
      "11    blue   L  21.0  class2\n",
      "12   green  XL  22.1  class2\n",
      "13  yellow   S  16.2  class1\n",
      "14  purple  XL  24.0  class3\n",
      "15     red   M  18.5  class1\n",
      "16    blue   L  21.0  class2\n",
      "17   green  XL  22.1  class2\n",
      "18  yellow   S  16.2  class1\n",
      "19  purple  XL  24.0  class3\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Create a new DataFrame with more data\n",
    "df = pd.DataFrame(\n",
    "    [\n",
    "        ['red', 'M', 18.5, 'class1'],\n",
    "        ['blue', 'L', 21.0, 'class2'],\n",
    "        ['green', 'XL', 22.1, 'class2'],\n",
    "        ['yellow', 'S', 16.2, 'class1'],\n",
    "        ['purple', 'XL', 24.0, 'class3'],\n",
    "        ['red', 'M', 18.5, 'class1'],\n",
    "        ['blue', 'L', 21.0, 'class2'],\n",
    "        ['green', 'XL', 22.1, 'class2'],\n",
    "        ['yellow', 'S', 16.2, 'class1'],\n",
    "        ['purple', 'XL', 24.0, 'class3'],\n",
    "        ['red', 'M', 18.5, 'class1'],\n",
    "        ['blue', 'L', 21.0, 'class2'],\n",
    "        ['green', 'XL', 22.1, 'class2'],\n",
    "        ['yellow', 'S', 16.2, 'class1'],\n",
    "        ['purple', 'XL', 24.0, 'class3'],\n",
    "        ['red', 'M', 18.5, 'class1'],\n",
    "        ['blue', 'L', 21.0, 'class2'],\n",
    "        ['green', 'XL', 22.1, 'class2'],\n",
    "        ['yellow', 'S', 16.2, 'class1'],\n",
    "        ['purple', 'XL', 24.0, 'class3']\n",
    "    ]\n",
    ")\n",
    "\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bdd9a5f0-225e-467a-b076-2d710cc8a802",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2</th>\n",
       "      <th>0_blue</th>\n",
       "      <th>0_green</th>\n",
       "      <th>0_purple</th>\n",
       "      <th>0_red</th>\n",
       "      <th>0_yellow</th>\n",
       "      <th>1_L</th>\n",
       "      <th>1_M</th>\n",
       "      <th>1_S</th>\n",
       "      <th>1_XL</th>\n",
       "      <th>3_class1</th>\n",
       "      <th>3_class2</th>\n",
       "      <th>3_class3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.5</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21.0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>22.1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>24.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>18.5</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>21.0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>22.1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>16.2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>24.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>18.5</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>21.0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>22.1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>16.2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>24.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>18.5</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>21.0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>22.1</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>16.2</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>24.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       2  0_blue  0_green  0_purple  0_red  0_yellow    1_L    1_M    1_S  \\\n",
       "0   18.5   False    False     False   True     False  False   True  False   \n",
       "1   21.0    True    False     False  False     False   True  False  False   \n",
       "2   22.1   False     True     False  False     False  False  False  False   \n",
       "3   16.2   False    False     False  False      True  False  False   True   \n",
       "4   24.0   False    False      True  False     False  False  False  False   \n",
       "5   18.5   False    False     False   True     False  False   True  False   \n",
       "6   21.0    True    False     False  False     False   True  False  False   \n",
       "7   22.1   False     True     False  False     False  False  False  False   \n",
       "8   16.2   False    False     False  False      True  False  False   True   \n",
       "9   24.0   False    False      True  False     False  False  False  False   \n",
       "10  18.5   False    False     False   True     False  False   True  False   \n",
       "11  21.0    True    False     False  False     False   True  False  False   \n",
       "12  22.1   False     True     False  False     False  False  False  False   \n",
       "13  16.2   False    False     False  False      True  False  False   True   \n",
       "14  24.0   False    False      True  False     False  False  False  False   \n",
       "15  18.5   False    False     False   True     False  False   True  False   \n",
       "16  21.0    True    False     False  False     False   True  False  False   \n",
       "17  22.1   False     True     False  False     False  False  False  False   \n",
       "18  16.2   False    False     False  False      True  False  False   True   \n",
       "19  24.0   False    False      True  False     False  False  False  False   \n",
       "\n",
       "     1_XL  3_class1  3_class2  3_class3  \n",
       "0   False      True     False     False  \n",
       "1   False     False      True     False  \n",
       "2    True     False      True     False  \n",
       "3   False      True     False     False  \n",
       "4    True     False     False      True  \n",
       "5   False      True     False     False  \n",
       "6   False     False      True     False  \n",
       "7    True     False      True     False  \n",
       "8   False      True     False     False  \n",
       "9    True     False     False      True  \n",
       "10  False      True     False     False  \n",
       "11  False     False      True     False  \n",
       "12   True     False      True     False  \n",
       "13  False      True     False     False  \n",
       "14   True     False     False      True  \n",
       "15  False      True     False     False  \n",
       "16  False     False      True     False  \n",
       "17   True     False      True     False  \n",
       "18  False      True     False     False  \n",
       "19   True     False     False      True  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(df[[0,1,2,3]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0ed772b7-34f5-4f6f-915c-c6f8c3ee739b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1     2  3\n",
      "0   3 NaN  18.5  0\n",
      "1   0 NaN  21.0  1\n",
      "2   1 NaN  22.1  1\n",
      "3   4 NaN  16.2  0\n",
      "4   2 NaN  24.0  2\n",
      "5   3 NaN  18.5  0\n",
      "6   0 NaN  21.0  1\n",
      "7   1 NaN  22.1  1\n",
      "8   4 NaN  16.2  0\n",
      "9   2 NaN  24.0  2\n",
      "10  3 NaN  18.5  0\n",
      "11  0 NaN  21.0  1\n",
      "12  1 NaN  22.1  1\n",
      "13  4 NaN  16.2  0\n",
      "14  2 NaN  24.0  2\n",
      "15  3 NaN  18.5  0\n",
      "16  0 NaN  21.0  1\n",
      "17  1 NaN  22.1  1\n",
      "18  4 NaN  16.2  0\n",
      "19  2 NaN  24.0  2\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "\n",
    "size_mapping = {'XS':0, 'S':1, 'M':2, 'L':3, 'XL':4}\n",
    "df[1] = df[1].map(size_mapping)\n",
    "\n",
    "label_encoder = LabelEncoder()\n",
    "df[3] = label_encoder.fit_transform(df[3])\n",
    "df[0] = label_encoder.fit_transform(df[0])\n",
    "\n",
    "print(df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eae17956-14a7-4f6c-a43a-d06e88250b9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
