{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# загрузка данных"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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>Rank</th>\n",
       "      <th>Name</th>\n",
       "      <th>Networth</th>\n",
       "      <th>Age</th>\n",
       "      <th>Country</th>\n",
       "      <th>Source</th>\n",
       "      <th>Industry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Elon Musk</td>\n",
       "      <td>219.0</td>\n",
       "      <td>50</td>\n",
       "      <td>United States</td>\n",
       "      <td>Tesla, SpaceX</td>\n",
       "      <td>Automotive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jeff Bezos</td>\n",
       "      <td>171.0</td>\n",
       "      <td>58</td>\n",
       "      <td>United States</td>\n",
       "      <td>Amazon</td>\n",
       "      <td>Technology</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Bernard Arnault &amp; family</td>\n",
       "      <td>158.0</td>\n",
       "      <td>73</td>\n",
       "      <td>France</td>\n",
       "      <td>LVMH</td>\n",
       "      <td>Fashion &amp; Retail</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Bill Gates</td>\n",
       "      <td>129.0</td>\n",
       "      <td>66</td>\n",
       "      <td>United States</td>\n",
       "      <td>Microsoft</td>\n",
       "      <td>Technology</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Warren Buffett</td>\n",
       "      <td>118.0</td>\n",
       "      <td>91</td>\n",
       "      <td>United States</td>\n",
       "      <td>Berkshire Hathaway</td>\n",
       "      <td>Finance &amp; Investments</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2595</th>\n",
       "      <td>2578</td>\n",
       "      <td>Jorge Gallardo Ballart</td>\n",
       "      <td>1.0</td>\n",
       "      <td>80</td>\n",
       "      <td>Spain</td>\n",
       "      <td>pharmaceuticals</td>\n",
       "      <td>Healthcare</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2596</th>\n",
       "      <td>2578</td>\n",
       "      <td>Nari Genomal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>82</td>\n",
       "      <td>Philippines</td>\n",
       "      <td>apparel</td>\n",
       "      <td>Fashion &amp; Retail</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2597</th>\n",
       "      <td>2578</td>\n",
       "      <td>Ramesh Genomal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>71</td>\n",
       "      <td>Philippines</td>\n",
       "      <td>apparel</td>\n",
       "      <td>Fashion &amp; Retail</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2598</th>\n",
       "      <td>2578</td>\n",
       "      <td>Sunder Genomal</td>\n",
       "      <td>1.0</td>\n",
       "      <td>68</td>\n",
       "      <td>Philippines</td>\n",
       "      <td>garments</td>\n",
       "      <td>Fashion &amp; Retail</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2599</th>\n",
       "      <td>2578</td>\n",
       "      <td>Horst-Otto Gerberding</td>\n",
       "      <td>1.0</td>\n",
       "      <td>69</td>\n",
       "      <td>Germany</td>\n",
       "      <td>flavors and fragrances</td>\n",
       "      <td>Food &amp; Beverage</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2600 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Rank                        Name  Networth  Age        Country  \\\n",
       "0         1                 Elon Musk      219.0   50  United States   \n",
       "1         2                Jeff Bezos      171.0   58  United States   \n",
       "2         3  Bernard Arnault & family      158.0   73         France   \n",
       "3         4                Bill Gates      129.0   66  United States   \n",
       "4         5            Warren Buffett      118.0   91  United States   \n",
       "...     ...                        ...       ...  ...            ...   \n",
       "2595   2578    Jorge Gallardo Ballart        1.0   80          Spain   \n",
       "2596   2578              Nari Genomal        1.0   82    Philippines   \n",
       "2597   2578            Ramesh Genomal        1.0   71    Philippines   \n",
       "2598   2578            Sunder Genomal        1.0   68    Philippines   \n",
       "2599   2578     Horst-Otto Gerberding        1.0   69        Germany   \n",
       "\n",
       "                      Source                Industry  \n",
       "0              Tesla, SpaceX             Automotive   \n",
       "1                     Amazon             Technology   \n",
       "2                       LVMH       Fashion & Retail   \n",
       "3                  Microsoft             Technology   \n",
       "4         Berkshire Hathaway  Finance & Investments   \n",
       "...                      ...                     ...  \n",
       "2595         pharmaceuticals             Healthcare   \n",
       "2596                 apparel       Fashion & Retail   \n",
       "2597                 apparel       Fashion & Retail   \n",
       "2598                garments       Fashion & Retail   \n",
       "2599  flavors and fragrances        Food & Beverage   \n",
       "\n",
       "[2600 rows x 7 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv(\"C://Users//annal//aim//static//csv//Forbes_Billionaires.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# входные и выходные переменные:\n",
    "# входные: age и country\n",
    "# выходные: networth"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# создание лингвистических переменных"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import skfuzzy as fuzz\n",
    "from skfuzzy import control as ctrl\n",
    "age_cat = ctrl.Antecedent(df[\"Age\"].sort_values(), \"age_cat\")\n",
    "networth = ctrl.Antecedent(df[\"Networth\"].sort_values(), \"networth\")\n",
    "rank = ctrl.Consequent(df[\"Rank \"].sort_values(), \"rank\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# формирование нечетких переменных для лингвистических переменных и их визуализация¶"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\annal\\aim\\.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, 48772 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 48708 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 65720 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "age_cat.automf(3, variable_type= \"quant\")\n",
    "age_cat.view()\n",
    "networth.automf(3, variable_type=\"quant\")\n",
    "networth.view()\n",
    "rank.automf(5, variable_type=\"quant\")\n",
    "rank.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# формирование и визуализация нечётких правил"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Figure size 640x480 with 1 Axes>, <Axes: >)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22316 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import skfuzzy as fuzz\n",
    "from skfuzzy import control as ctrl \n",
    "rule1 = ctrl.Rule(age_cat['low'] & networth['low'], rank['lower'])\n",
    "rule2 = ctrl.Rule(age_cat['low'] & networth['average'], rank['low'])\n",
    "rule3 = ctrl.Rule(age_cat['low'] & networth['high'], rank['average'])\n",
    "rule4 = ctrl.Rule(age_cat['average'] & networth['low'], rank['lower'])\n",
    "rule5 = ctrl.Rule(age_cat['average'] & networth['average'], rank['average'])\n",
    "rule6 = ctrl.Rule(age_cat['average'] & networth['high'], rank['high'])\n",
    "rule7 = ctrl.Rule(age_cat['high'] & networth['low'], rank['average'])\n",
    "rule8 = ctrl.Rule(age_cat['high'] & networth['average'], rank['high'])\n",
    "rule9 = ctrl.Rule(age_cat['high'] & networth['high'], rank['higher'])\n",
    "\n",
    "rule1.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# создание нечеткой системы и добавление нечетких правил в базу знаний нечеткой системы"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "rank_ctrl = ctrl.ControlSystem(\n",
    "    [\n",
    "        rule1,\n",
    "        rule2,\n",
    "        rule3,\n",
    "        rule4,\n",
    "        rule5,\n",
    "        rule6,\n",
    "        rule7,\n",
    "        rule8,\n",
    "        rule9,\n",
    "    ]\n",
    ")\n",
    "\n",
    "ranks = ctrl.ControlSystemSimulation(rank_ctrl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# пример расчёта rank на основе age_cat и networth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=============\n",
      " Antecedents \n",
      "=============\n",
      "Antecedent: age_cat                 = 50\n",
      "  - low                             : 0.2345679012345679\n",
      "  - average                         : 0.7654320987654321\n",
      "  - high                            : 0.0\n",
      "Antecedent: networth                = 219\n",
      "  - low                             : 0.0\n",
      "  - average                         : 0.0\n",
      "  - high                            : 1.0\n",
      "\n",
      "=======\n",
      " Rules \n",
      "=======\n",
      "RULE #0:\n",
      "  IF age_cat[low] AND networth[low] THEN rank[higher]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[low]                                           : 0.2345679012345679\n",
      "  - networth[low]                                          : 0.0\n",
      "                            age_cat[low] AND networth[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              rank[higher] : 0.0\n",
      "\n",
      "RULE #1:\n",
      "  IF age_cat[low] AND networth[average] THEN rank[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[low]                                           : 0.2345679012345679\n",
      "  - networth[average]                                      : 0.0\n",
      "                        age_cat[low] AND networth[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                                rank[high] : 0.0\n",
      "\n",
      "RULE #2:\n",
      "  IF age_cat[low] AND networth[high] THEN rank[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[low]                                           : 0.2345679012345679\n",
      "  - networth[high]                                         : 1.0\n",
      "                           age_cat[low] AND networth[high] = 0.2345679012345679\n",
      "  Activation (THEN-clause):\n",
      "                                                 rank[low] : 0.2345679012345679\n",
      "\n",
      "RULE #3:\n",
      "  IF age_cat[average] AND networth[low] THEN rank[high]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[average]                                       : 0.7654320987654321\n",
      "  - networth[low]                                          : 0.0\n",
      "                        age_cat[average] AND networth[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                                rank[high] : 0.0\n",
      "\n",
      "RULE #4:\n",
      "  IF age_cat[average] AND networth[average] THEN rank[average]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[average]                                       : 0.7654320987654321\n",
      "  - networth[average]                                      : 0.0\n",
      "                    age_cat[average] AND networth[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                             rank[average] : 0.0\n",
      "\n",
      "RULE #5:\n",
      "  IF age_cat[average] AND networth[high] THEN rank[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[average]                                       : 0.7654320987654321\n",
      "  - networth[high]                                         : 1.0\n",
      "                       age_cat[average] AND networth[high] = 0.7654320987654321\n",
      "  Activation (THEN-clause):\n",
      "                                               rank[lower] : 0.7654320987654321\n",
      "\n",
      "RULE #6:\n",
      "  IF age_cat[high] AND networth[low] THEN rank[higher]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[high]                                          : 0.0\n",
      "  - networth[low]                                          : 0.0\n",
      "                           age_cat[high] AND networth[low] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                              rank[higher] : 0.0\n",
      "\n",
      "RULE #7:\n",
      "  IF age_cat[high] AND networth[average] THEN rank[low]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[high]                                          : 0.0\n",
      "  - networth[average]                                      : 0.0\n",
      "                       age_cat[high] AND networth[average] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                                 rank[low] : 0.0\n",
      "\n",
      "RULE #8:\n",
      "  IF age_cat[high] AND networth[high] THEN rank[lower]\n",
      "\tAND aggregation function : fmin\n",
      "\tOR aggregation function  : fmax\n",
      "\n",
      "  Aggregation (IF-clause):\n",
      "  - age_cat[high]                                          : 0.0\n",
      "  - networth[high]                                         : 1.0\n",
      "                          age_cat[high] AND networth[high] = 0.0\n",
      "  Activation (THEN-clause):\n",
      "                                               rank[lower] : 0.0\n",
      "\n",
      "\n",
      "==============================\n",
      " Intermediaries and Conquests \n",
      "==============================\n",
      "Consequent: rank                     = 446.6186379699145\n",
      "  lower:\n",
      "    Accumulate using accumulation_max : 0.7654320987654321\n",
      "  low:\n",
      "    Accumulate using accumulation_max : 0.2345679012345679\n",
      "  average:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  high:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "  higher:\n",
      "    Accumulate using accumulation_max : 0.0\n",
      "\n",
      "446.6186379699145\n"
     ]
    }
   ],
   "source": [
    "ranks.input['age_cat'] = 50\n",
    "ranks.input['networth'] = 219\n",
    "ranks.compute()\n",
    "ranks.print_state()\n",
    "print(ranks.output['rank'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# тестирование нечёткой системы"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "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>Age</th>\n",
       "      <th>Networth</th>\n",
       "      <th>Rank</th>\n",
       "      <th>Pred</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50</td>\n",
       "      <td>219.0</td>\n",
       "      <td>1</td>\n",
       "      <td>446.618638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>58</td>\n",
       "      <td>171.0</td>\n",
       "      <td>2</td>\n",
       "      <td>948.540336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>73</td>\n",
       "      <td>158.0</td>\n",
       "      <td>3</td>\n",
       "      <td>952.599948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>66</td>\n",
       "      <td>129.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1138.614099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>91</td>\n",
       "      <td>118.0</td>\n",
       "      <td>5</td>\n",
       "      <td>814.043935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>49</td>\n",
       "      <td>111.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1473.194767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>48</td>\n",
       "      <td>107.0</td>\n",
       "      <td>7</td>\n",
       "      <td>1498.674963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>77</td>\n",
       "      <td>106.0</td>\n",
       "      <td>8</td>\n",
       "      <td>1040.193500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>66</td>\n",
       "      <td>91.4</td>\n",
       "      <td>9</td>\n",
       "      <td>1309.303429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>64</td>\n",
       "      <td>90.7</td>\n",
       "      <td>10</td>\n",
       "      <td>1344.732355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>59</td>\n",
       "      <td>90.0</td>\n",
       "      <td>11</td>\n",
       "      <td>1435.386123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>80</td>\n",
       "      <td>82.0</td>\n",
       "      <td>12</td>\n",
       "      <td>1200.124026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>82</td>\n",
       "      <td>81.2</td>\n",
       "      <td>13</td>\n",
       "      <td>1185.800088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>68</td>\n",
       "      <td>74.8</td>\n",
       "      <td>14</td>\n",
       "      <td>1370.702727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>37</td>\n",
       "      <td>67.3</td>\n",
       "      <td>15</td>\n",
       "      <td>1689.837998</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Age  Networth  Rank          Pred\n",
       "0    50     219.0      1   446.618638\n",
       "1    58     171.0      2   948.540336\n",
       "2    73     158.0      3   952.599948\n",
       "3    66     129.0      4  1138.614099\n",
       "4    91     118.0      5   814.043935\n",
       "5    49     111.0      6  1473.194767\n",
       "6    48     107.0      7  1498.674963\n",
       "7    77     106.0      8  1040.193500\n",
       "8    66      91.4      9  1309.303429\n",
       "9    64      90.7     10  1344.732355\n",
       "10   59      90.0     11  1435.386123\n",
       "11   80      82.0     12  1200.124026\n",
       "12   82      81.2     13  1185.800088\n",
       "13   68      74.8     14  1370.702727\n",
       "14   37      67.3     15  1689.837998"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fuzzy_pred(row):\n",
    "    ranks.input['age_cat'] = row['Age']\n",
    "    ranks.input['networth'] = row['Networth']\n",
    "    ranks.compute()\n",
    "    return ranks.output['rank']\n",
    "\n",
    "res = df[['Age', 'Networth', 'Rank ']].head(100)\n",
    "\n",
    "res['Pred'] = res.apply(fuzzy_pred, axis=1)\n",
    "\n",
    "res.head(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# оценка результатов (метрики для задачи регрессии)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'RMSE': 1526.018176278599,\n",
       " 'RMAE': 38.87544820606799,\n",
       " 'R2': -2805.351442150934}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "from sklearn import metrics\n",
    "\n",
    "\n",
    "rmetrics = {}\n",
    "rmetrics[\"RMSE\"] = math.sqrt(metrics.mean_squared_error(res['Rank '], res['Pred']))\n",
    "rmetrics[\"RMAE\"] = math.sqrt(metrics.mean_absolute_error(res['Rank '], res['Pred']))\n",
    "rmetrics[\"R2\"] = metrics.r2_score(res['Rank '], res['Pred'])\n",
    "\n",
    "rmetrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "p.s. менять датасет нет смысла. все подобные датасеты имеют одинаковые колонки. какой не возьми, резульат не хороший"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
