{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5251 entries, 0 to 5250\n",
      "Data columns (total 8 columns):\n",
      " #   Column     Non-Null Count  Dtype         \n",
      "---  ------     --------------  -----         \n",
      " 0   Date       5251 non-null   object        \n",
      " 1   Open       5251 non-null   float64       \n",
      " 2   High       5251 non-null   float64       \n",
      " 3   Low        5251 non-null   float64       \n",
      " 4   Close      5251 non-null   float64       \n",
      " 5   Adj Close  5251 non-null   float64       \n",
      " 6   Volume     5251 non-null   int64         \n",
      " 7   date       5251 non-null   datetime64[ns]\n",
      "dtypes: datetime64[ns](1), float64(5), int64(1), object(1)\n",
      "memory usage: 328.3+ KB\n"
     ]
    }
   ],
   "source": [
    "import pandas as pn\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import matplotlib.ticker as ticker\n",
    "from datetime import datetime\n",
    "import matplotlib.dates as md\n",
    "\n",
    "df = pn.read_csv(\".//static//csv//Yamana_Gold_Inc._AUY.csv\")\n",
    "print(df.columns)\n",
    "\n",
    "df[\"date\"] = df.apply(lambda row: datetime.strptime(row[\"Date\"], \"%m/%d/%Y\"), axis=1)\n",
    "df.info()\n",
    "#print(df['date'].head)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Разделим на 3 выборки\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Размер обучающей выборки: 3360\n",
      "Размер контрольной выборки: 840\n",
      "Размер тестовой выборки: 1051\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# Разделение данных на обучающую и тестовую выборки (80% - обучение, 20% - тест)\n",
    "train_data, test_data = train_test_split(df, test_size=0.2, random_state=42)\n",
    "\n",
    "# Разделение обучающей выборки на обучающую и контрольную (80% - обучение, 20% - контроль)\n",
    "train_data, val_data = train_test_split(train_data, test_size=0.2, random_state=42)\n",
    "\n",
    "print(\"Размер обучающей выборки:\", len(train_data))\n",
    "print(\"Размер контрольной выборки:\", len(val_data))\n",
    "print(\"Размер тестовой выборки:\", len(test_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32988 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34596 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31648 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Гистограмма распределения объема в обучающей выборке\n",
    "sns.histplot(train_data[\"Volume\"], kde=True)\n",
    "plt.title('Распределение цены в обучающей выборке')\n",
    "plt.show()\n",
    "\n",
    "# Гистограмма распределения объема в контрольной выборке\n",
    "sns.histplot(val_data[\"Volume\"], kde=True)\n",
    "plt.title('Распределение цены в контрольной выборке')\n",
    "plt.show()\n",
    "\n",
    "# Гистограмма распределения объема в тестовой выборке\n",
    "sns.histplot(test_data[\"Volume\"], kde=True)\n",
    "plt.title('Распределение цены в тестовой выборке')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Процесс конструирования признаков\n",
    "\n",
    "\n",
    "\n",
    "### Унитарное кодирование категориальных признаков (one-hot encoding)\n",
    "\n",
    "One-hot encoding: Преобразование категориальных признаков в бинарные векторы."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Пример категориальных признаков\n",
    "categorical_features = [\n",
    "    \"Date\",\n",
    "    \"date\"\n",
    "]\n",
    "\n",
    "# Применение one-hot encoding\n",
    "train_data_encoded = pd.get_dummies(train_data, columns=categorical_features)\n",
    "val_data_encoded = pd.get_dummies(val_data, columns=categorical_features)\n",
    "test_data_encoded = pd.get_dummies(test_data, columns=categorical_features)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Дискретизация числовых признаков "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>High</th>\n",
       "      <th>High</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5231</th>\n",
       "      <td>5.83</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5232</th>\n",
       "      <td>5.93</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5233</th>\n",
       "      <td>6.06</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5234</th>\n",
       "      <td>6.04</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5235</th>\n",
       "      <td>6.15</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5236</th>\n",
       "      <td>6.23</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5237</th>\n",
       "      <td>6.40</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5238</th>\n",
       "      <td>6.25</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5239</th>\n",
       "      <td>6.33</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5240</th>\n",
       "      <td>6.25</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5241</th>\n",
       "      <td>6.03</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5242</th>\n",
       "      <td>5.72</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5243</th>\n",
       "      <td>5.65</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5244</th>\n",
       "      <td>5.44</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5245</th>\n",
       "      <td>5.61</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5246</th>\n",
       "      <td>5.69</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5247</th>\n",
       "      <td>5.39</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5248</th>\n",
       "      <td>5.53</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5249</th>\n",
       "      <td>5.61</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5250</th>\n",
       "      <td>5.66</td>\n",
       "      <td>(1.143, 7.625]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      High            High\n",
       "5231  5.83  (1.143, 7.625]\n",
       "5232  5.93  (1.143, 7.625]\n",
       "5233  6.06  (1.143, 7.625]\n",
       "5234  6.04  (1.143, 7.625]\n",
       "5235  6.15  (1.143, 7.625]\n",
       "5236  6.23  (1.143, 7.625]\n",
       "5237  6.40  (1.143, 7.625]\n",
       "5238  6.25  (1.143, 7.625]\n",
       "5239  6.33  (1.143, 7.625]\n",
       "5240  6.25  (1.143, 7.625]\n",
       "5241  6.03  (1.143, 7.625]\n",
       "5242  5.72  (1.143, 7.625]\n",
       "5243  5.65  (1.143, 7.625]\n",
       "5244  5.44  (1.143, 7.625]\n",
       "5245  5.61  (1.143, 7.625]\n",
       "5246  5.69  (1.143, 7.625]\n",
       "5247  5.39  (1.143, 7.625]\n",
       "5248  5.53  (1.143, 7.625]\n",
       "5249  5.61  (1.143, 7.625]\n",
       "5250  5.66  (1.143, 7.625]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "labels = [\"low hight price\", \"medium hight price\", \"big hight price\"]\n",
    "num_bins = 3\n",
    "\n",
    "hist1, bins1 = np.histogram(\n",
    "    df[\"High\"].fillna(df[\"High\"].median()), bins=num_bins\n",
    ")\n",
    "bins1, hist1\n",
    "\n",
    "pd.concat([df[\"High\"], pd.cut(df[\"High\"], list(bins1))], axis=1).tail(20)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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>High</th>\n",
       "      <th>High</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2.857143</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>3.142857</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>3.142857</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>3.428571</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>3.714286</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>3.142857</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>3.142857</td>\n",
       "      <td>low hight price</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        High             High\n",
       "0   3.428571  low hight price\n",
       "1   3.428571  low hight price\n",
       "2   3.714286  low hight price\n",
       "3   3.714286  low hight price\n",
       "4   3.714286  low hight price\n",
       "5   3.714286  low hight price\n",
       "6   3.714286  low hight price\n",
       "7   3.714286  low hight price\n",
       "8   3.428571  low hight price\n",
       "9   3.428571  low hight price\n",
       "10  3.428571  low hight price\n",
       "11  3.428571  low hight price\n",
       "12  3.428571  low hight price\n",
       "13  2.857143  low hight price\n",
       "14  3.142857  low hight price\n",
       "15  3.142857  low hight price\n",
       "16  3.428571  low hight price\n",
       "17  3.714286  low hight price\n",
       "18  3.142857  low hight price\n",
       "19  3.142857  low hight price"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat(\n",
    "    [df[\"High\"], pd.cut(df[\"High\"], list(bins1), labels=labels)], axis=1\n",
    ").head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ручной синтез"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Пример синтеза признака среднего значения в максимальной и минимальной цене\n",
    "train_data_encoded[\"medium\"] = train_data_encoded[\"High\"] / train_data_encoded[\"Low\"]\n",
    "val_data_encoded[\"medium\"] = val_data_encoded[\"High\"] / val_data_encoded[\"Low\"]\n",
    "test_data_encoded[\"medium\"] = test_data_encoded[\"High\"] / test_data_encoded[\"Low\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Масштабирование признаков - это процесс преобразования числовых признаков таким образом, чтобы они имели одинаковый масштаб. Это важно для многих алгоритмов машинного обучения, которые чувствительны к масштабу признаков, таких как линейная регрессия, метод опорных векторов (SVM) и нейронные сети."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler, MinMaxScaler\n",
    "\n",
    "# Пример масштабирования числовых признаков\n",
    "numerical_features = [\"Open\", \"Close\"]\n",
    "\n",
    "scaler = StandardScaler()\n",
    "train_data_encoded[numerical_features] = scaler.fit_transform(train_data_encoded[numerical_features])\n",
    "val_data_encoded[numerical_features] = scaler.transform(val_data_encoded[numerical_features])\n",
    "test_data_encoded[numerical_features] = scaler.transform(test_data_encoded[numerical_features])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Конструирование признаков с применением фреймворка Featuretools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\entityset\\entityset.py:1733: UserWarning: index id not found in dataframe, creating new integer column\n",
      "  warnings.warn(\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\synthesis\\deep_feature_synthesis.py:169: UserWarning: Only one dataframe in entityset, changing max_depth to 1 since deeper features cannot be created\n",
      "  warnings.warn(\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\computational_backends\\feature_set_calculator.py:143: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n",
      "  df = pd.concat([df, default_df], sort=True)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\woodwork\\logical_types.py:841: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  series = series.replace(ww.config.get_option(\"nan_values\"), np.nan)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\computational_backends\\feature_set_calculator.py:143: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n",
      "  df = pd.concat([df, default_df], sort=True)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\woodwork\\logical_types.py:841: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  series = series.replace(ww.config.get_option(\"nan_values\"), np.nan)\n"
     ]
    }
   ],
   "source": [
    "import featuretools as ft\n",
    "\n",
    "# Определение сущностей\n",
    "es = ft.EntitySet(id='gold_data')\n",
    "\n",
    "es = es.add_dataframe(dataframe_name='yamana', dataframe=train_data_encoded, index='id')\n",
    "\n",
    "\n",
    "# Генерация признаков\n",
    "feature_matrix, feature_defs = ft.dfs(\n",
    "    entityset=es, target_dataframe_name=\"yamana\", max_depth=2\n",
    ")\n",
    "\n",
    "# Преобразование признаков для контрольной и тестовой выборок\n",
    "val_feature_matrix = ft.calculate_feature_matrix(features=feature_defs, entityset=es, instance_ids=val_data_encoded.index)\n",
    "test_feature_matrix = ft.calculate_feature_matrix(features=feature_defs, entityset=es, instance_ids=test_data_encoded.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Оценка качества каждого набора признаков\n",
    "Предсказательная способность\n",
    "Метрики: RMSE, MAE, R²\n",
    "\n",
    "Методы: Обучение модели на обучающей выборке и оценка на контрольной и тестовой выборках.\n",
    "\n",
    "Скорость вычисления\n",
    "Методы: Измерение времени выполнения генерации признаков и обучения модели.\n",
    "\n",
    "Надежность\n",
    "Методы: Кросс-валидация, анализ чувствительности модели к изменениям в данных.\n",
    "\n",
    "Корреляция\n",
    "Методы: Анализ корреляционной матрицы признаков, удаление мультиколлинеарных признаков.\n",
    "\n",
    "Цельность\n",
    "Методы: Проверка логической связи между признаками и целевой переменной, интерпретация результатов модели."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\entityset\\entityset.py:724: UserWarning: A Woodwork-initialized DataFrame was provided, so the following parameters were ignored: index\n",
      "  warnings.warn(\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\synthesis\\deep_feature_synthesis.py:169: UserWarning: Only one dataframe in entityset, changing max_depth to 1 since deeper features cannot be created\n",
      "  warnings.warn(\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\computational_backends\\feature_set_calculator.py:143: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n",
      "  df = pd.concat([df, default_df], sort=True)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\woodwork\\logical_types.py:841: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  series = series.replace(ww.config.get_option(\"nan_values\"), np.nan)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\featuretools\\computational_backends\\feature_set_calculator.py:143: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n",
      "  df = pd.concat([df, default_df], sort=True)\n",
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\woodwork\\logical_types.py:841: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`\n",
      "  series = series.replace(ww.config.get_option(\"nan_values\"), np.nan)\n"
     ]
    }
   ],
   "source": [
    "import featuretools as ft\n",
    "\n",
    "# Определение сущностей\n",
    "es = ft.EntitySet(id='gold_data')\n",
    "es = es.add_dataframe(\n",
    "    dataframe_name=\"yamana\", dataframe=train_data_encoded, index=\"id\"\n",
    ")\n",
    "\n",
    "# Генерация признаков\n",
    "feature_matrix, feature_defs = ft.dfs(\n",
    "    entityset=es, target_dataframe_name=\"yamana\", max_depth=2\n",
    ")\n",
    "\n",
    "# Преобразование признаков для контрольной и тестовой выборок\n",
    "val_feature_matrix = ft.calculate_feature_matrix(features=feature_defs, entityset=es, instance_ids=val_data_encoded.index)\n",
    "test_feature_matrix = ft.calculate_feature_matrix(features=feature_defs, entityset=es, instance_ids=test_data_encoded.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\sklearn\\metrics\\_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RMSE: 1764152.3991770656\n",
      "R²: 0.942082609353535\n",
      "MAE: 1161195.586464497\n",
      "Cross-validated RMSE: 4835663.342127571\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\3_КУРС_ПИ\\МИИ\\aisenv\\Lib\\site-packages\\sklearn\\metrics\\_regression.py:492: FutureWarning: 'squared' is deprecated in version 1.4 and will be removed in 1.6. To calculate the root mean squared error, use the function'root_mean_squared_error'.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train RMSE: 1789275.1500045008\n",
      "Train R²: 0.944270691246794\n",
      "Train MAE: 1134044.475345238\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 85832 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error\n",
    "from sklearn.model_selection import cross_val_score\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# Удаление строк с NaN\n",
    "feature_matrix = feature_matrix.dropna()\n",
    "val_feature_matrix = val_feature_matrix.dropna()\n",
    "test_feature_matrix = test_feature_matrix.dropna()\n",
    "\n",
    "# Разделение данных на обучающую и тестовую выборки\n",
    "X_train = feature_matrix.drop(\"Volume\", axis=1)\n",
    "y_train = feature_matrix[\"Volume\"]\n",
    "X_val = val_feature_matrix.drop(\"Volume\", axis=1)\n",
    "y_val = val_feature_matrix[\"Volume\"]\n",
    "X_test = test_feature_matrix.drop(\"Volume\", axis=1)\n",
    "y_test = test_feature_matrix[\"Volume\"]\n",
    "\n",
    "# Выбор модели\n",
    "model = RandomForestRegressor(random_state=42)\n",
    "\n",
    "# Обучение модели\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# Предсказание и оценка\n",
    "y_pred = model.predict(X_test)\n",
    "\n",
    "rmse = mean_squared_error(y_test, y_pred, squared=False)\n",
    "r2 = r2_score(y_test, y_pred)\n",
    "mae = mean_absolute_error(y_test, y_pred)\n",
    "\n",
    "print(f\"RMSE: {rmse}\")\n",
    "print(f\"R²: {r2}\")\n",
    "print(f\"MAE: {mae}\")\n",
    "\n",
    "# Кросс-валидация\n",
    "scores = cross_val_score(model, X_train, y_train, cv=5, scoring='neg_mean_squared_error')\n",
    "rmse_cv = (-scores.mean())**0.5\n",
    "print(f\"Cross-validated RMSE: {rmse_cv}\")\n",
    "\n",
    "# Анализ важности признаков\n",
    "feature_importances = model.feature_importances_\n",
    "feature_names = X_train.columns\n",
    "\n",
    "\n",
    "# Проверка на переобучение\n",
    "y_train_pred = model.predict(X_train)\n",
    "\n",
    "rmse_train = mean_squared_error(y_train, y_train_pred, squared=False)\n",
    "r2_train = r2_score(y_train, y_train_pred)\n",
    "mae_train = mean_absolute_error(y_train, y_train_pred)\n",
    "\n",
    "print(f\"Train RMSE: {rmse_train}\")\n",
    "print(f\"Train R²: {r2_train}\")\n",
    "print(f\"Train MAE: {mae_train}\")\n",
    "\n",
    "# Визуализация результатов\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.scatter(y_test, y_pred, alpha=0.5)\n",
    "plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)\n",
    "plt.xlabel(\"Actual Volume\")\n",
    "plt.ylabel(\"Predicted Volume\")\n",
    "plt.title(\"Actual vs Predicted Volume\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Точность предсказаний: Модель показывает довольно высокий R² (0.944), что указывает на хорошее объяснение вариации распродаж. Значения RMSE и MAE довольно низки, что говорит о том, что модель достаточно точно предсказывает цены.\n",
    "\n",
    "Переобучение: Разница между RMSE на обучающей и тестовой выборках не очень большая, что указывает на то, что переобучение не является критическим. Однако, стоит быть осторожным и продолжать мониторинг этого показателя.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "aisenv",
   "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
}
