{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 58,
   "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>T</th>\n",
       "      <th>Al2O3</th>\n",
       "      <th>TiO2</th>\n",
       "      <th>Density</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.06250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05404</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    T  Al2O3  TiO2  Density\n",
       "0  20    0.0   0.0  1.06250\n",
       "1  25    0.0   0.0  1.05979\n",
       "2  35    0.0   0.0  1.05404"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "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>T</th>\n",
       "      <th>Al2O3</th>\n",
       "      <th>TiO2</th>\n",
       "      <th>Density</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.04158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>25</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.08438</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    T  Al2O3  TiO2  Density\n",
       "0  30   0.00   0.0  1.05696\n",
       "1  55   0.00   0.0  1.04158\n",
       "2  25   0.05   0.0  1.08438"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "density_train = pd.read_csv(\"data/density_train.csv\", sep=\";\", decimal=\",\")\n",
    "density_test = pd.read_csv(\"data/density_test.csv\", sep=\";\", decimal=\",\")\n",
    "\n",
    "display(density_train.head(3))\n",
    "display(density_test.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\user\\Projects\\python\\fuzzy\\.venv\\Lib\\site-packages\\skfuzzy\\control\\fuzzyvariable.py:125: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown\n",
      "  fig.show()\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 47016 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46196 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 66956 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from skfuzzy import control as ctrl\n",
    "import skfuzzy as fuzz\n",
    "\n",
    "temp = ctrl.Antecedent(density_train[\"T\"].sort_values().unique(), \"temp\")\n",
    "al = ctrl.Antecedent(np.arange(0, 0.3, 0.005), \"al\")\n",
    "ti = ctrl.Antecedent(np.arange(0, 0.3, 0.005), \"ti\")\n",
    "density = ctrl.Consequent(np.arange(1.03, 1.22, 0.00001), \"density\")\n",
    "\n",
    "temp.automf(3, variable_type=\"quant\")\n",
    "temp.view()\n",
    "al.automf(3, variable_type=\"quant\")\n",
    "al.view()\n",
    "ti.automf(3, variable_type=\"quant\")\n",
    "ti.view()\n",
    "density.automf(5, variable_type=\"quant\")\n",
    "density.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "rule11 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"low\"] & ti[\"low\"],\n",
    "    density[\"low\"],\n",
    ")\n",
    "rule12 = ctrl.Rule(\n",
    "    temp[\"average\"] & al[\"low\"] & ti[\"low\"],\n",
    "    density[\"lower\"],\n",
    ")\n",
    "rule13 = ctrl.Rule(\n",
    "    temp[\"high\"] & al[\"low\"] & ti[\"low\"],\n",
    "    density[\"lower\"],\n",
    ")\n",
    "\n",
    "rule21 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"average\"] & ti[\"low\"],\n",
    "    density[\"low\"],\n",
    ")\n",
    "rule22 = ctrl.Rule(\n",
    "    temp[\"average\"] & al[\"average\"] & ti[\"low\"],\n",
    "    density[\"low\"],\n",
    ")\n",
    "rule23 = ctrl.Rule(\n",
    "    temp[\"high\"] & al[\"average\"] & ti[\"low\"],\n",
    "    density[\"lower\"],\n",
    ")\n",
    "\n",
    "rule31 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"high\"] & ti[\"low\"],\n",
    "    density[\"high\"],\n",
    ")\n",
    "rule32 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"high\"] & ti[\"low\"],\n",
    "    density[\"high\"],\n",
    ")\n",
    "rule33 = ctrl.Rule(\n",
    "    temp[\"high\"] & al[\"high\"] & ti[\"low\"],\n",
    "    density[\"average\"],\n",
    ")\n",
    "\n",
    "rule41 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"low\"] & ti[\"average\"],\n",
    "    density[\"low\"],\n",
    ")\n",
    "rule42 = ctrl.Rule(\n",
    "    temp[\"average\"] & al[\"low\"] & ti[\"average\"],\n",
    "    density[\"low\"],\n",
    ")\n",
    "rule43 = ctrl.Rule(\n",
    "    temp[\"high\"] & al[\"low\"] & ti[\"average\"],\n",
    "    density[\"lower\"],\n",
    ")\n",
    "\n",
    "rule51 = ctrl.Rule(\n",
    "    temp[\"low\"] & al[\"low\"] & ti[\"high\"],\n",
    "    density[\"higher\"],\n",
    ")\n",
    "rule52 = ctrl.Rule(\n",
    "    temp[\"average\"] & al[\"low\"] & ti[\"high\"],\n",
    "    density[\"high\"],\n",
    ")\n",
    "rule53 = ctrl.Rule(\n",
    "    temp[\"high\"] & al[\"low\"] & ti[\"high\"],\n",
    "    density[\"high\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[IF (temp[low] AND al[low]) AND ti[low] THEN density[low]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[average] AND al[low]) AND ti[low] THEN density[lower]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[high] AND al[low]) AND ti[low] THEN density[lower]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[low] AND al[average]) AND ti[low] THEN density[low]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[average] AND al[average]) AND ti[low] THEN density[low]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[high] AND al[average]) AND ti[low] THEN density[lower]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[low] AND al[high]) AND ti[low] THEN density[high]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[low] AND al[high]) AND ti[low] THEN density[high]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[high] AND al[high]) AND ti[low] THEN density[average]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[low] AND al[low]) AND ti[average] THEN density[low]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[average] AND al[low]) AND ti[average] THEN density[low]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[high] AND al[low]) AND ti[average] THEN density[lower]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[low] AND al[low]) AND ti[high] THEN density[higher]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[average] AND al[low]) AND ti[high] THEN density[high]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax,\n",
       " IF (temp[high] AND al[low]) AND ti[high] THEN density[high]\n",
       " \tAND aggregation function : fmin\n",
       " \tOR aggregation function  : fmax]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fuzzy_rules = [\n",
    "    rule11,\n",
    "    rule12,\n",
    "    rule13,\n",
    "    rule21,\n",
    "    rule22,\n",
    "    rule23,\n",
    "    rule31,\n",
    "    rule32,\n",
    "    rule33,\n",
    "    rule41,\n",
    "    rule42,\n",
    "    rule43,\n",
    "    rule51,\n",
    "    rule52,\n",
    "    rule53,\n",
    "]\n",
    "\n",
    "density_cntrl = ctrl.ControlSystem(fuzzy_rules)\n",
    "\n",
    "sim = ctrl.ControlSystemSimulation(density_cntrl)\n",
    "\n",
    "fuzzy_rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=============\n",
      " Antecedents \n",
      "=============\n",
      "Antecedent: temp                    = 20\n",
      "  - low                             : 1.0\n",
      "  - average                         : 0.0\n",
      "  - high                            : 0.0\n",
      "Antecedent: al                      = 0.295\n",
      "  - low                             : 0.0\n",
      "  - average                         : 0.0\n",
      "  - high                            : 1.0\n",
      "Antecedent: ti                      = 0.0\n",
      "  - low                             : 1.0\n",
      "  - average                         : 0.0\n",
      "  - high                            : 0.0\n",
      "\n",
      "=======\n",
      " Rules \n",
      "=======\n",
      "RULE #0:\n",
      "  IF (temp[low] AND al[low]) AND ti[low] THEN density[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                       (temp[low] AND al[low]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              density[low] : 0.0\n",
      "\n",
      "RULE #1:\n",
      "  IF (temp[average] AND al[low]) AND ti[low] THEN density[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[average]                                          : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                   (temp[average] AND al[low]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                            density[lower] : 0.0\n",
      "\n",
      "RULE #2:\n",
      "  IF (temp[high] AND al[low]) AND ti[low] THEN density[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[high]                                             : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                      (temp[high] AND al[low]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                            density[lower] : 0.0\n",
      "\n",
      "RULE #3:\n",
      "  IF (temp[low] AND al[average]) AND ti[low] THEN density[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[average]                                            : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                   (temp[low] AND al[average]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              density[low] : 0.0\n",
      "\n",
      "RULE #4:\n",
      "  IF (temp[average] AND al[average]) AND ti[low] THEN density[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[average]                                          : 0.0\n",
      "  - al[average]                                            : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "               (temp[average] AND al[average]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              density[low] : 0.0\n",
      "\n",
      "RULE #5:\n",
      "  IF (temp[high] AND al[average]) AND ti[low] THEN density[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[high]                                             : 0.0\n",
      "  - al[average]                                            : 0.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                  (temp[high] AND al[average]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                            density[lower] : 0.0\n",
      "\n",
      "RULE #6:\n",
      "  IF (temp[low] AND al[high]) AND ti[low] THEN density[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[high]                                               : 1.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                      (temp[low] AND al[high]) AND ti[low] = 1.0\n",
      "  Activation (THEN-clause):\n",
      "                                             density[high] : 1.0\n",
      "\n",
      "RULE #7:\n",
      "  IF (temp[low] AND al[high]) AND ti[low] THEN density[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[high]                                               : 1.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                      (temp[low] AND al[high]) AND ti[low] = 1.0\n",
      "  Activation (THEN-clause):\n",
      "                                             density[high] : 1.0\n",
      "\n",
      "RULE #8:\n",
      "  IF (temp[high] AND al[high]) AND ti[low] THEN density[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[high]                                             : 0.0\n",
      "  - al[high]                                               : 1.0\n",
      "  - ti[low]                                                : 1.0\n",
      "                     (temp[high] AND al[high]) AND ti[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                          density[average] : 0.0\n",
      "\n",
      "RULE #9:\n",
      "  IF (temp[low] AND al[low]) AND ti[average] THEN density[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[average]                                            : 0.0\n",
      "                   (temp[low] AND al[low]) AND ti[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              density[low] : 0.0\n",
      "\n",
      "RULE #10:\n",
      "  IF (temp[average] AND al[low]) AND ti[average] THEN density[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[average]                                          : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[average]                                            : 0.0\n",
      "               (temp[average] AND al[low]) AND ti[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              density[low] : 0.0\n",
      "\n",
      "RULE #11:\n",
      "  IF (temp[high] AND al[low]) AND ti[average] THEN density[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[high]                                             : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[average]                                            : 0.0\n",
      "                  (temp[high] AND al[low]) AND ti[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                            density[lower] : 0.0\n",
      "\n",
      "RULE #12:\n",
      "  IF (temp[low] AND al[low]) AND ti[high] THEN density[higher]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[low]                                              : 1.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[high]                                               : 0.0\n",
      "                      (temp[low] AND al[low]) AND ti[high] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                           density[higher] : 0.0\n",
      "\n",
      "RULE #13:\n",
      "  IF (temp[average] AND al[low]) AND ti[high] THEN density[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[average]                                          : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[high]                                               : 0.0\n",
      "                  (temp[average] AND al[low]) AND ti[high] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                             density[high] : 0.0\n",
      "\n",
      "RULE #14:\n",
      "  IF (temp[high] AND al[low]) AND ti[high] THEN density[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - temp[high]                                             : 0.0\n",
      "  - al[low]                                                : 0.0\n",
      "  - ti[high]                                               : 0.0\n",
      "                     (temp[high] AND al[low]) AND ti[high] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                             density[high] : 0.0\n",
      "\n",
      "\n",
      "==============================\n",
      " Intermediaries and Conquests \n",
      "==============================\n",
      "Consequent: density                  = 1.1724924997377486\n",
      "  lower:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  low:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  average:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  high:\n",
      "    Accumulate using accumulation_max : 1.0\n",
      "  higher:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.float64(1.1724924997377486)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sim.input[\"temp\"] = 20\n",
    "sim.input[\"al\"] = 0.3\n",
    "sim.input[\"ti\"] = 0.0\n",
    "sim.compute()\n",
    "sim.print_state()\n",
    "display(sim.output[\"density\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fuzzy_pred(row):\n",
    "    sim.input[\"temp\"] = row[\"T\"]\n",
    "    sim.input[\"al\"] = row[\"Al2O3\"]\n",
    "    sim.input[\"ti\"] = row[\"TiO2\"]\n",
    "    sim.compute()\n",
    "    return sim.output[\"density\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "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>T</th>\n",
       "      <th>Al2O3</th>\n",
       "      <th>TiO2</th>\n",
       "      <th>Density</th>\n",
       "      <th>DensityPred</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.06250</td>\n",
       "      <td>1.077498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05979</td>\n",
       "      <td>1.076593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05404</td>\n",
       "      <td>1.069156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05103</td>\n",
       "      <td>1.061106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>45</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.04794</td>\n",
       "      <td>1.045833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.04477</td>\n",
       "      <td>1.046360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.03826</td>\n",
       "      <td>1.047642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>65</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.03484</td>\n",
       "      <td>1.046360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>70</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.03182</td>\n",
       "      <td>1.045833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>20</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.08755</td>\n",
       "      <td>1.077498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>45</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.07105</td>\n",
       "      <td>1.067145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>50</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.06760</td>\n",
       "      <td>1.067145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>55</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.06409</td>\n",
       "      <td>1.067988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>65</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05691</td>\n",
       "      <td>1.062538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>70</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.05291</td>\n",
       "      <td>1.047191</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     T  Al2O3  TiO2  Density  DensityPred\n",
       "0   20   0.00   0.0  1.06250     1.077498\n",
       "1   25   0.00   0.0  1.05979     1.076593\n",
       "2   35   0.00   0.0  1.05404     1.069156\n",
       "3   40   0.00   0.0  1.05103     1.061106\n",
       "4   45   0.00   0.0  1.04794     1.045833\n",
       "5   50   0.00   0.0  1.04477     1.046360\n",
       "6   60   0.00   0.0  1.03826     1.047642\n",
       "7   65   0.00   0.0  1.03484     1.046360\n",
       "8   70   0.00   0.0  1.03182     1.045833\n",
       "9   20   0.05   0.0  1.08755     1.077498\n",
       "10  45   0.05   0.0  1.07105     1.067145\n",
       "11  50   0.05   0.0  1.06760     1.067145\n",
       "12  55   0.05   0.0  1.06409     1.067988\n",
       "13  65   0.05   0.0  1.05691     1.062538\n",
       "14  70   0.05   0.0  1.05291     1.047191"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_train = density_train.copy()\n",
    "\n",
    "result_train[\"DensityPred\"] = result_train.apply(fuzzy_pred, axis=1)\n",
    "\n",
    "result_train.head(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "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>T</th>\n",
       "      <th>Al2O3</th>\n",
       "      <th>TiO2</th>\n",
       "      <th>Density</th>\n",
       "      <th>DensityPred</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.05696</td>\n",
       "      <td>1.073918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.04158</td>\n",
       "      <td>1.047642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>25</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.08438</td>\n",
       "      <td>1.076518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.08112</td>\n",
       "      <td>1.073918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>35</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.07781</td>\n",
       "      <td>1.069156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>40</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.07446</td>\n",
       "      <td>1.067145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>60</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.06053</td>\n",
       "      <td>1.067988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>35</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.17459</td>\n",
       "      <td>1.172492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>65</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.14812</td>\n",
       "      <td>1.124995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>45</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>1.07424</td>\n",
       "      <td>1.067145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>1.07075</td>\n",
       "      <td>1.067145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>1.06721</td>\n",
       "      <td>1.067988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>20</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.22417</td>\n",
       "      <td>1.204157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.21310</td>\n",
       "      <td>1.180834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>40</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.20265</td>\n",
       "      <td>1.173397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.18265</td>\n",
       "      <td>1.172492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>70</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>1.17261</td>\n",
       "      <td>1.172492</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     T  Al2O3  TiO2  Density  DensityPred\n",
       "0   30   0.00  0.00  1.05696     1.073918\n",
       "1   55   0.00  0.00  1.04158     1.047642\n",
       "2   25   0.05  0.00  1.08438     1.076518\n",
       "3   30   0.05  0.00  1.08112     1.073918\n",
       "4   35   0.05  0.00  1.07781     1.069156\n",
       "5   40   0.05  0.00  1.07446     1.067145\n",
       "6   60   0.05  0.00  1.06053     1.067988\n",
       "7   35   0.30  0.00  1.17459     1.172492\n",
       "8   65   0.30  0.00  1.14812     1.124995\n",
       "9   45   0.00  0.05  1.07424     1.067145\n",
       "10  50   0.00  0.05  1.07075     1.067145\n",
       "11  55   0.00  0.05  1.06721     1.067988\n",
       "12  20   0.00  0.30  1.22417     1.204157\n",
       "13  30   0.00  0.30  1.21310     1.180834\n",
       "14  40   0.00  0.30  1.20265     1.173397\n",
       "15  60   0.00  0.30  1.18265     1.172492\n",
       "16  70   0.00  0.30  1.17261     1.172492"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_test = density_test.copy()\n",
    "\n",
    "result_test[\"DensityPred\"] = result_test.apply(fuzzy_pred, axis=1)\n",
    "\n",
    "result_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RMSE_train': 0.0156766544822719,\n",
       " 'RMSE_test': 0.014603864614526584,\n",
       " 'RMAE_test': 0.1057231829134674,\n",
       " 'R2_test': 0.9436578668511394}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "from sklearn import metrics\n",
    "\n",
    "\n",
    "rmetrics = {}\n",
    "rmetrics[\"RMSE_train\"] = math.sqrt(\n",
    "    metrics.mean_squared_error(result_train[\"Density\"], result_train[\"DensityPred\"])\n",
    ")\n",
    "rmetrics[\"RMSE_test\"] = math.sqrt(\n",
    "    metrics.mean_squared_error(result_test[\"Density\"], result_test[\"DensityPred\"])\n",
    ")\n",
    "rmetrics[\"RMAE_test\"] = math.sqrt(\n",
    "    metrics.mean_absolute_error(result_test[\"Density\"], result_test[\"DensityPred\"])\n",
    ")\n",
    "rmetrics[\"R2_test\"] = metrics.r2_score(\n",
    "    result_test[\"Density\"], result_test[\"DensityPred\"]\n",
    ")\n",
    "\n",
    "rmetrics"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
