{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "f10cf91e",
      "metadata": {},
      "source": [
        "# Прогнозирование цены автомобиля"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "0757256f",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m25.3\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m26.0\u001b[0m\n",
            "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
            "Note: you may need to restart the kernel to use updated packages.\n"
          ]
        }
      ],
      "source": [
        "%pip -q install pandas numpy matplotlib seaborn scikit-learn"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "id": "ad0e2ade",
      "metadata": {},
      "outputs": [],
      "source": [
        "import re\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.compose import ColumnTransformer\n",
        "from sklearn.preprocessing import OneHotEncoder\n",
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.impute import SimpleImputer\n",
        "from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\n",
        "from sklearn.linear_model import LinearRegression\n",
        "from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\n",
        "\n",
        "pd.set_option(\"display.max_columns\", 50)\n",
        "sns.set_style(\"whitegrid\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "id": "2180fd89",
      "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>Car ID</th>\n",
              "      <th>Brand</th>\n",
              "      <th>Year</th>\n",
              "      <th>Engine Size</th>\n",
              "      <th>Fuel Type</th>\n",
              "      <th>Transmission</th>\n",
              "      <th>Mileage</th>\n",
              "      <th>Condition</th>\n",
              "      <th>Price</th>\n",
              "      <th>Model</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1.0</td>\n",
              "      <td>Tesla</td>\n",
              "      <td>2016.0</td>\n",
              "      <td>2.3</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Manual</td>\n",
              "      <td>114832.0</td>\n",
              "      <td>New</td>\n",
              "      <td>26613.92</td>\n",
              "      <td>Model X</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2.0</td>\n",
              "      <td>BMW</td>\n",
              "      <td>2018.0</td>\n",
              "      <td>4.4</td>\n",
              "      <td>Electric</td>\n",
              "      <td>Manual</td>\n",
              "      <td>143190.0</td>\n",
              "      <td>Used</td>\n",
              "      <td>14679.61</td>\n",
              "      <td>5 Series</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3.0</td>\n",
              "      <td>Audi</td>\n",
              "      <td>2013.0</td>\n",
              "      <td>4.5</td>\n",
              "      <td>Electric</td>\n",
              "      <td>Manual</td>\n",
              "      <td>181601.0</td>\n",
              "      <td>New</td>\n",
              "      <td>44402.61</td>\n",
              "      <td>A4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4.0</td>\n",
              "      <td>Tesla</td>\n",
              "      <td>2011.0</td>\n",
              "      <td>4.1</td>\n",
              "      <td>Diesel</td>\n",
              "      <td>Automatic</td>\n",
              "      <td>68682.0</td>\n",
              "      <td>New</td>\n",
              "      <td>86374.33</td>\n",
              "      <td>Model Y</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5.0</td>\n",
              "      <td>Ford</td>\n",
              "      <td>2009.0</td>\n",
              "      <td>2.6</td>\n",
              "      <td>Diesel</td>\n",
              "      <td>Manual</td>\n",
              "      <td>223009.0</td>\n",
              "      <td>Like New</td>\n",
              "      <td>73577.10</td>\n",
              "      <td>Mustang</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   Car ID  Brand    Year  Engine Size Fuel Type Transmission   Mileage  \\\n",
              "0     1.0  Tesla  2016.0          2.3    Petrol       Manual  114832.0   \n",
              "1     2.0    BMW  2018.0          4.4  Electric       Manual  143190.0   \n",
              "2     3.0   Audi  2013.0          4.5  Electric       Manual  181601.0   \n",
              "3     4.0  Tesla  2011.0          4.1    Diesel    Automatic   68682.0   \n",
              "4     5.0   Ford  2009.0          2.6    Diesel       Manual  223009.0   \n",
              "\n",
              "  Condition     Price     Model  \n",
              "0       New  26613.92   Model X  \n",
              "1      Used  14679.61  5 Series  \n",
              "2       New  44402.61        A4  \n",
              "3       New  86374.33   Model Y  \n",
              "4  Like New  73577.10   Mustang  "
            ]
          },
          "execution_count": 20,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# 1) Загрузка датасета\n",
        "df = pd.read_csv(\"car_price_prediction_with_missing.csv\")\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "id": "19d24a44",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "<class 'pandas.DataFrame'>\n",
            "RangeIndex: 2500 entries, 0 to 2499\n",
            "Data columns (total 10 columns):\n",
            " #   Column        Non-Null Count  Dtype  \n",
            "---  ------        --------------  -----  \n",
            " 0   Car ID        2250 non-null   float64\n",
            " 1   Brand         2250 non-null   str    \n",
            " 2   Year          2250 non-null   float64\n",
            " 3   Engine Size   2250 non-null   float64\n",
            " 4   Fuel Type     2250 non-null   str    \n",
            " 5   Transmission  2250 non-null   str    \n",
            " 6   Mileage       2250 non-null   float64\n",
            " 7   Condition     2250 non-null   str    \n",
            " 8   Price         2250 non-null   float64\n",
            " 9   Model         2250 non-null   str    \n",
            "dtypes: float64(5), str(5)\n",
            "memory usage: 195.4 KB\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "None"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "Car ID          0.1\n",
              "Brand           0.1\n",
              "Year            0.1\n",
              "Engine Size     0.1\n",
              "Fuel Type       0.1\n",
              "Transmission    0.1\n",
              "Mileage         0.1\n",
              "Condition       0.1\n",
              "Price           0.1\n",
              "Model           0.1\n",
              "dtype: float64"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Общая информация и пропуски\n",
        "display(df.info())\n",
        "display(df.isna().mean().sort_values(ascending=False))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c0dffc81",
      "metadata": {},
      "source": [
        "## Очистка и предобработка\n",
        "Исходный набор данных имеет пропуски\n",
        "- Удаляем дубликаты.\n",
        "- Удалить пропуски"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "id": "1ab1ead2",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "<class 'pandas.DataFrame'>\n",
            "Index: 2250 entries, 0 to 2499\n",
            "Data columns (total 10 columns):\n",
            " #   Column        Non-Null Count  Dtype  \n",
            "---  ------        --------------  -----  \n",
            " 0   Car ID        2250 non-null   float64\n",
            " 1   Brand         2250 non-null   str    \n",
            " 2   Year          2250 non-null   float64\n",
            " 3   Engine Size   2250 non-null   float64\n",
            " 4   Fuel Type     2250 non-null   str    \n",
            " 5   Transmission  2250 non-null   str    \n",
            " 6   Mileage       2250 non-null   float64\n",
            " 7   Condition     2250 non-null   str    \n",
            " 8   Price         2250 non-null   float64\n",
            " 9   Model         2250 non-null   str    \n",
            "dtypes: float64(5), str(5)\n",
            "memory usage: 193.4 KB\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "None"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "Car ID          0.0\n",
              "Brand           0.0\n",
              "Year            0.0\n",
              "Engine Size     0.0\n",
              "Fuel Type       0.0\n",
              "Transmission    0.0\n",
              "Mileage         0.0\n",
              "Condition       0.0\n",
              "Price           0.0\n",
              "Model           0.0\n",
              "dtype: float64"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "df = df.drop_duplicates()\n",
        "df = df.dropna()\n",
        "display(df.info())\n",
        "display(df.isna().mean().sort_values(ascending=False))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d7fdb1a8",
      "metadata": {},
      "source": [
        "## Задание 1. Анализ и визуализация\n",
        "\n",
        "Ниже 3 графика для изучения датасета\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "id": "83c5b8b2",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 20172 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# График 1: распределение цены\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.histplot(df[\"Price\"], bins=50)\n",
        "plt.title(\"Распределение цены\")\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4cf27c93",
      "metadata": {},
      "source": [
        "Видно, что распределение цен на автомобили примерно равномерное, близкое к случайному"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "id": "926cf42e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 162728 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# График 2: связь цены и года выпуска\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.scatterplot(data=df, x=\"Year\", y=\"Price\", alpha=0.6)\n",
        "plt.title(\"Цена vs Год выпуска\")\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "69a27e4a",
      "metadata": {},
      "source": [
        "Цена на автомобиль совсем не зависит от года выпуска"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "id": "fb59d4c3",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 22164 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# График 3: цена в зависимости от состояния авто\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.boxplot(data=df, x=\"Condition\", y=\"Price\")\n",
        "plt.title(\"Цена vs Состояние\")\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4948afde",
      "metadata": {},
      "source": [
        "От состояния цена также не зависит"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ab37182d",
      "metadata": {},
      "source": [
        "### Краткие выводы по графикам\n",
        "- В данных есть пропуски.\n",
        "- Данные не коррелируют между собой, они выглядят случайными"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "daa3521f",
      "metadata": {},
      "source": [
        "## Задание 2. Модели прогнозирования"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "id": "4dade46e",
      "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>model</th>\n",
              "      <th>split</th>\n",
              "      <th>MAE</th>\n",
              "      <th>RMSE</th>\n",
              "      <th>R2</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>LinearRegression</td>\n",
              "      <td>train</td>\n",
              "      <td>23677.424433</td>\n",
              "      <td>27261.296466</td>\n",
              "      <td>0.001463</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>LinearRegression</td>\n",
              "      <td>test</td>\n",
              "      <td>23386.369896</td>\n",
              "      <td>26995.898047</td>\n",
              "      <td>-0.000709</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>RandomForest</td>\n",
              "      <td>train</td>\n",
              "      <td>8877.283827</td>\n",
              "      <td>10364.788027</td>\n",
              "      <td>0.855658</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>RandomForest</td>\n",
              "      <td>test</td>\n",
              "      <td>23676.962601</td>\n",
              "      <td>27982.335432</td>\n",
              "      <td>-0.075178</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>GradientBoosting</td>\n",
              "      <td>train</td>\n",
              "      <td>20860.335930</td>\n",
              "      <td>24310.182306</td>\n",
              "      <td>0.205951</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>GradientBoosting</td>\n",
              "      <td>test</td>\n",
              "      <td>23625.922747</td>\n",
              "      <td>27750.650439</td>\n",
              "      <td>-0.057447</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "              model  split           MAE          RMSE        R2\n",
              "0  LinearRegression  train  23677.424433  27261.296466  0.001463\n",
              "1  LinearRegression   test  23386.369896  26995.898047 -0.000709\n",
              "2      RandomForest  train   8877.283827  10364.788027  0.855658\n",
              "3      RandomForest   test  23676.962601  27982.335432 -0.075178\n",
              "4  GradientBoosting  train  20860.335930  24310.182306  0.205951\n",
              "5  GradientBoosting   test  23625.922747  27750.650439 -0.057447"
            ]
          },
          "execution_count": 26,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Удаляем строки без цены\n",
        "df_model = df.dropna(subset=[\"Price\"]).copy()\n",
        "\n",
        "X = df_model.drop(columns=[\"Price\"])\n",
        "y = df_model[\"Price\"]\n",
        "\n",
        "cat_cols = X.select_dtypes(include=[\"object\", \"string\"]).columns\n",
        "num_cols = X.select_dtypes(exclude=[\"object\", \"string\"]).columns\n",
        "\n",
        "preprocess = ColumnTransformer(\n",
        "    transformers=[\n",
        "        (\"num\", Pipeline([\n",
        "            (\"imputer\", SimpleImputer(strategy=\"median\"))\n",
        "        ]), num_cols),\n",
        "        (\"cat\", Pipeline([\n",
        "            (\"imputer\", SimpleImputer(strategy=\"most_frequent\")),\n",
        "            (\"onehot\", OneHotEncoder(handle_unknown=\"ignore\"))\n",
        "        ]), cat_cols)\n",
        "    ]\n",
        ")\n",
        "\n",
        "models = {\n",
        "    \"LinearRegression\": LinearRegression(),\n",
        "    \"RandomForest\": RandomForestRegressor(n_estimators=200, random_state=42),\n",
        "    \"GradientBoosting\": GradientBoostingRegressor(random_state=42)\n",
        "}\n",
        "\n",
        "def evaluate_models(X, y, models, random_state=42):\n",
        "    mask = y.notna()\n",
        "    X = X.loc[mask]\n",
        "    y = y.loc[mask]\n",
        "\n",
        "    X_train, X_test, y_train, y_test = train_test_split(\n",
        "        X, y, test_size=0.2, random_state=random_state\n",
        "    )\n",
        "    rows = []\n",
        "    for name, model in models.items():\n",
        "        pipe = Pipeline([\n",
        "            (\"preprocess\", preprocess),\n",
        "            (\"model\", model)\n",
        "        ])\n",
        "        pipe.fit(X_train, y_train)\n",
        "\n",
        "        for split, (X_s, y_s) in {\n",
        "            \"train\": (X_train, y_train),\n",
        "            \"test\": (X_test, y_test)\n",
        "        }.items():\n",
        "            pred = pipe.predict(X_s)\n",
        "            rows.append({\n",
        "                \"model\": name,\n",
        "                \"split\": split,\n",
        "                \"MAE\": mean_absolute_error(y_s, pred),\n",
        "                \"RMSE\": np.sqrt(mean_squared_error(y_s, pred)),\n",
        "                \"R2\": r2_score(y_s, pred)\n",
        "            })\n",
        "\n",
        "    return pd.DataFrame(rows)\n",
        "\n",
        "results_before = evaluate_models(X, y, models)\n",
        "results_before"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "id": "2d537e27",
      "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 tr th {\n",
              "        text-align: left;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr:last-of-type th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th colspan=\"2\" halign=\"left\">MAE</th>\n",
              "      <th colspan=\"2\" halign=\"left\">R2</th>\n",
              "      <th colspan=\"2\" halign=\"left\">RMSE</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>split</th>\n",
              "      <th>test</th>\n",
              "      <th>train</th>\n",
              "      <th>test</th>\n",
              "      <th>train</th>\n",
              "      <th>test</th>\n",
              "      <th>train</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>model</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>GradientBoosting</th>\n",
              "      <td>23625.922747</td>\n",
              "      <td>20860.335930</td>\n",
              "      <td>-0.057447</td>\n",
              "      <td>0.205951</td>\n",
              "      <td>27750.650439</td>\n",
              "      <td>24310.182306</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>LinearRegression</th>\n",
              "      <td>23386.369896</td>\n",
              "      <td>23677.424433</td>\n",
              "      <td>-0.000709</td>\n",
              "      <td>0.001463</td>\n",
              "      <td>26995.898047</td>\n",
              "      <td>27261.296466</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>RandomForest</th>\n",
              "      <td>23676.962601</td>\n",
              "      <td>8877.283827</td>\n",
              "      <td>-0.075178</td>\n",
              "      <td>0.855658</td>\n",
              "      <td>27982.335432</td>\n",
              "      <td>10364.788027</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                           MAE                      R2            \\\n",
              "split                     test         train      test     train   \n",
              "model                                                              \n",
              "GradientBoosting  23625.922747  20860.335930 -0.057447  0.205951   \n",
              "LinearRegression  23386.369896  23677.424433 -0.000709  0.001463   \n",
              "RandomForest      23676.962601   8877.283827 -0.075178  0.855658   \n",
              "\n",
              "                          RMSE                \n",
              "split                     test         train  \n",
              "model                                         \n",
              "GradientBoosting  27750.650439  24310.182306  \n",
              "LinearRegression  26995.898047  27261.296466  \n",
              "RandomForest      27982.335432  10364.788027  "
            ]
          },
          "execution_count": 27,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Таблица результатов (train/test)\n",
        "pivot_before = results_before.pivot_table(index=\"model\", columns=\"split\", values=[\"MAE\", \"RMSE\", \"R2\"])\n",
        "pivot_before"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "id": "92be75ab",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 47044 characters]",
            "text/plain": [
              "<Figure size 1400x400 with 3 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Визуализация метрик на тесте\n",
        "test_before = results_before[results_before[\"split\"] == \"test\"]\n",
        "\n",
        "fig, axes = plt.subplots(1, 3, figsize=(14, 4))\n",
        "sns.barplot(data=test_before, x=\"model\", y=\"MAE\", ax=axes[0])\n",
        "sns.barplot(data=test_before, x=\"model\", y=\"RMSE\", ax=axes[1])\n",
        "sns.barplot(data=test_before, x=\"model\", y=\"R2\", ax=axes[2])\n",
        "\n",
        "axes[0].set_title(\"MAE (test)\")\n",
        "axes[1].set_title(\"RMSE (test)\")\n",
        "axes[2].set_title(\"R2 (test)\")\n",
        "plt.tight_layout()\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0fd28587",
      "metadata": {},
      "source": [
        "### Почему выбраны эти метрики\n",
        "\n",
        "- **MAE** показывает среднюю абсолютную ошибку в тех же единицах, что и цена.\n",
        "- **RMSE** сильнее штрафует большие ошибки.\n",
        "- **R2** — доля объяснённой дисперсии: 0 означает, что модель не лучше предсказания средним, 1 — полное совпадение с целевой переменной."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "39b556c4",
      "metadata": {},
      "source": [
        "## Задание 3. Добавляем `cars_new.csv` и сравниваем качество"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9a4377b3",
      "metadata": {},
      "source": [
        "cars_new.csv имеет другой формат, все данные в первом столбце и переведены на другой язык. Исправим это для объединения:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "id": "a9f78135",
      "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>Car ID</th>\n",
              "      <th>Brand</th>\n",
              "      <th>Year</th>\n",
              "      <th>Engine Size</th>\n",
              "      <th>Fuel Type</th>\n",
              "      <th>Transmission</th>\n",
              "      <th>Mileage</th>\n",
              "      <th>Condition</th>\n",
              "      <th>Price</th>\n",
              "      <th>Model</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2000</td>\n",
              "      <td>1.5 л</td>\n",
              "      <td>Бензин</td>\n",
              "      <td>Механика</td>\n",
              "      <td>150000</td>\n",
              "      <td>Отличное</td>\n",
              "      <td>70000</td>\n",
              "      <td>2110</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2001</td>\n",
              "      <td>1.6 л</td>\n",
              "      <td>Бензин</td>\n",
              "      <td>Механика</td>\n",
              "      <td>180000</td>\n",
              "      <td>Хорошее</td>\n",
              "      <td>80000</td>\n",
              "      <td>2107</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2002</td>\n",
              "      <td>1.5 л</td>\n",
              "      <td>Бензин</td>\n",
              "      <td>Механика</td>\n",
              "      <td>120000</td>\n",
              "      <td>Отличное</td>\n",
              "      <td>75000</td>\n",
              "      <td>2115</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2008</td>\n",
              "      <td>1.6 л</td>\n",
              "      <td>Бензин</td>\n",
              "      <td>Механика/Автомат</td>\n",
              "      <td>130000</td>\n",
              "      <td>Хорошее</td>\n",
              "      <td>180000</td>\n",
              "      <td>Kalina</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2009</td>\n",
              "      <td>98 л.с.,1.6 л</td>\n",
              "      <td>Бензин</td>\n",
              "      <td>Механика/Автомат</td>\n",
              "      <td>100000</td>\n",
              "      <td>Хорошее</td>\n",
              "      <td>200000</td>\n",
              "      <td>Priora</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  Car ID Brand  Year    Engine Size Fuel Type      Transmission Mileage  \\\n",
              "0      1  Lada  2000          1.5 л    Бензин          Механика  150000   \n",
              "1      2  Lada  2001          1.6 л    Бензин          Механика  180000   \n",
              "2      3  Lada  2002          1.5 л    Бензин          Механика  120000   \n",
              "3      4  Lada  2008          1.6 л    Бензин  Механика/Автомат  130000   \n",
              "4      5  Lada  2009  98 л.с.,1.6 л    Бензин  Механика/Автомат  100000   \n",
              "\n",
              "  Condition   Price   Model  \n",
              "0  Отличное   70000    2110  \n",
              "1   Хорошее   80000    2107  \n",
              "2  Отличное   75000    2115  \n",
              "3   Хорошее  180000  Kalina  \n",
              "4   Хорошее  200000  Priora  "
            ]
          },
          "execution_count": 29,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "def parse_cars_new(path):\n",
        "    raw = pd.read_csv(path, header=None)\n",
        "    lines = raw[0].astype(str).str.strip('\"')\n",
        "    rows = []\n",
        "    for line in lines:\n",
        "        parts = [p.strip() for p in line.split(\",\")]\n",
        "        if not parts or parts[0] == \"Car ID\":\n",
        "            continue\n",
        "        if len(parts) < 10:\n",
        "            continue\n",
        "        # Если внутри Engine Size есть запятая, будет больше 10 частей\n",
        "        head = parts[:3]\n",
        "        tail = parts[-6:]\n",
        "        engine = \",\".join(parts[3:-6]).strip()\n",
        "        row = head + [engine] + tail\n",
        "        if len(row) == 10:\n",
        "            rows.append(row)\n",
        "\n",
        "    cols = [\"Car ID\", \"Brand\", \"Year\", \"Engine Size\", \"Fuel Type\", \"Transmission\", \"Mileage\", \"Condition\", \"Price\", \"Model\"]\n",
        "    df_new = pd.DataFrame(rows, columns=cols)\n",
        "    return df_new\n",
        "\n",
        "df_new = parse_cars_new(\"cars_new.csv\")\n",
        "df_new.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "id": "ca74b22f",
      "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>Car ID</th>\n",
              "      <th>Brand</th>\n",
              "      <th>Year</th>\n",
              "      <th>Engine Size</th>\n",
              "      <th>Fuel Type</th>\n",
              "      <th>Transmission</th>\n",
              "      <th>Mileage</th>\n",
              "      <th>Condition</th>\n",
              "      <th>Price</th>\n",
              "      <th>Model</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2000</td>\n",
              "      <td>1.5</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Manual</td>\n",
              "      <td>150000</td>\n",
              "      <td>New</td>\n",
              "      <td>70000</td>\n",
              "      <td>2110</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2001</td>\n",
              "      <td>1.6</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Manual</td>\n",
              "      <td>180000</td>\n",
              "      <td>Used</td>\n",
              "      <td>80000</td>\n",
              "      <td>2107</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2002</td>\n",
              "      <td>1.5</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Manual</td>\n",
              "      <td>120000</td>\n",
              "      <td>New</td>\n",
              "      <td>75000</td>\n",
              "      <td>2115</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2008</td>\n",
              "      <td>1.6</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Automatic</td>\n",
              "      <td>130000</td>\n",
              "      <td>Used</td>\n",
              "      <td>180000</td>\n",
              "      <td>Kalina</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>Lada</td>\n",
              "      <td>2009</td>\n",
              "      <td>1.6</td>\n",
              "      <td>Petrol</td>\n",
              "      <td>Automatic</td>\n",
              "      <td>100000</td>\n",
              "      <td>Used</td>\n",
              "      <td>200000</td>\n",
              "      <td>Priora</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  Car ID Brand  Year  Engine Size Fuel Type Transmission  Mileage Condition  \\\n",
              "0      1  Lada  2000          1.5    Petrol       Manual   150000       New   \n",
              "1      2  Lada  2001          1.6    Petrol       Manual   180000      Used   \n",
              "2      3  Lada  2002          1.5    Petrol       Manual   120000       New   \n",
              "3      4  Lada  2008          1.6    Petrol    Automatic   130000      Used   \n",
              "4      5  Lada  2009          1.6    Petrol    Automatic   100000      Used   \n",
              "\n",
              "    Price   Model  \n",
              "0   70000    2110  \n",
              "1   80000    2107  \n",
              "2   75000    2115  \n",
              "3  180000  Kalina  \n",
              "4  200000  Priora  "
            ]
          },
          "execution_count": 30,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Приводим к единому формату\n",
        "fuel_map = {\n",
        "    \"Бензин\": \"Petrol\",\n",
        "    \"Дизель\": \"Diesel\",\n",
        "    \"Электро\": \"Electric\",\n",
        "    \"Гибрид\": \"Hybrid\"\n",
        "}\n",
        "trans_map = {\n",
        "    \"Механика\": \"Manual\",\n",
        "    \"Автомат\": \"Automatic\",\n",
        "    \"Механика/Автомат\": \"Automatic\"\n",
        "}\n",
        "cond_map = {\n",
        "    \"Отличное\": \"New\",\n",
        "    \"Хорошее\": \"Used\"\n",
        "}\n",
        "\n",
        "def extract_engine_size(x):\n",
        "    if pd.isna(x):\n",
        "        return np.nan\n",
        "    nums = re.findall(r\"\\d+(?:[.,]\\d+)?\", str(x))\n",
        "    if not nums:\n",
        "        return np.nan\n",
        "    return float(nums[-1].replace(\",\", \".\"))\n",
        "\n",
        "df_new[\"Fuel Type\"] = df_new[\"Fuel Type\"].map(fuel_map)\n",
        "df_new[\"Transmission\"] = df_new[\"Transmission\"].map(trans_map)\n",
        "df_new[\"Condition\"] = df_new[\"Condition\"].map(cond_map)\n",
        "\n",
        "df_new[\"Engine Size\"] = df_new[\"Engine Size\"].apply(extract_engine_size)\n",
        "df_new[\"Year\"] = pd.to_numeric(df_new[\"Year\"], errors=\"coerce\")\n",
        "df_new[\"Mileage\"] = pd.to_numeric(df_new[\"Mileage\"], errors=\"coerce\")\n",
        "df_new[\"Price\"] = pd.to_numeric(df_new[\"Price\"], errors=\"coerce\")\n",
        "\n",
        "df_new.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "id": "c807c43d",
      "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>model</th>\n",
              "      <th>split</th>\n",
              "      <th>MAE</th>\n",
              "      <th>RMSE</th>\n",
              "      <th>R2</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>LinearRegression</td>\n",
              "      <td>train</td>\n",
              "      <td>24385.641471</td>\n",
              "      <td>32867.632333</td>\n",
              "      <td>0.004216</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>LinearRegression</td>\n",
              "      <td>test</td>\n",
              "      <td>24857.121340</td>\n",
              "      <td>37804.530574</td>\n",
              "      <td>-0.007647</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>RandomForest</td>\n",
              "      <td>train</td>\n",
              "      <td>9174.571497</td>\n",
              "      <td>12278.185487</td>\n",
              "      <td>0.861038</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>RandomForest</td>\n",
              "      <td>test</td>\n",
              "      <td>24465.982962</td>\n",
              "      <td>34010.448955</td>\n",
              "      <td>0.184460</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>GradientBoosting</td>\n",
              "      <td>train</td>\n",
              "      <td>21391.653978</td>\n",
              "      <td>24988.338946</td>\n",
              "      <td>0.424423</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>GradientBoosting</td>\n",
              "      <td>test</td>\n",
              "      <td>23904.569889</td>\n",
              "      <td>29600.017743</td>\n",
              "      <td>0.382262</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "              model  split           MAE          RMSE        R2\n",
              "0  LinearRegression  train  24385.641471  32867.632333  0.004216\n",
              "1  LinearRegression   test  24857.121340  37804.530574 -0.007647\n",
              "2      RandomForest  train   9174.571497  12278.185487  0.861038\n",
              "3      RandomForest   test  24465.982962  34010.448955  0.184460\n",
              "4  GradientBoosting  train  21391.653978  24988.338946  0.424423\n",
              "5  GradientBoosting   test  23904.569889  29600.017743  0.382262"
            ]
          },
          "execution_count": 31,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Объединяем и повторяем оценку\n",
        "df_all = pd.concat([df, df_new], ignore_index=True)\n",
        "\n",
        "# Удаляем строки без цены\n",
        "df_all = df_all.dropna(subset=[\"Price\"]).copy()\n",
        "\n",
        "X_all = df_all.drop(columns=[\"Price\"])\n",
        "y_all = df_all[\"Price\"]\n",
        "\n",
        "results_after = evaluate_models(X_all, y_all, models)\n",
        "results_after"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "id": "4a4a7c79",
      "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>model</th>\n",
              "      <th>MAE_before</th>\n",
              "      <th>MAE_after</th>\n",
              "      <th>RMSE_before</th>\n",
              "      <th>RMSE_after</th>\n",
              "      <th>R2_before</th>\n",
              "      <th>R2_after</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>LinearRegression</td>\n",
              "      <td>23386.369896</td>\n",
              "      <td>24857.121340</td>\n",
              "      <td>26995.898047</td>\n",
              "      <td>37804.530574</td>\n",
              "      <td>-0.000709</td>\n",
              "      <td>-0.007647</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>RandomForest</td>\n",
              "      <td>23676.962601</td>\n",
              "      <td>24465.982962</td>\n",
              "      <td>27982.335432</td>\n",
              "      <td>34010.448955</td>\n",
              "      <td>-0.075178</td>\n",
              "      <td>0.184460</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>GradientBoosting</td>\n",
              "      <td>23625.922747</td>\n",
              "      <td>23904.569889</td>\n",
              "      <td>27750.650439</td>\n",
              "      <td>29600.017743</td>\n",
              "      <td>-0.057447</td>\n",
              "      <td>0.382262</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "              model    MAE_before     MAE_after   RMSE_before    RMSE_after  \\\n",
              "0  LinearRegression  23386.369896  24857.121340  26995.898047  37804.530574   \n",
              "1      RandomForest  23676.962601  24465.982962  27982.335432  34010.448955   \n",
              "2  GradientBoosting  23625.922747  23904.569889  27750.650439  29600.017743   \n",
              "\n",
              "   R2_before  R2_after  \n",
              "0  -0.000709 -0.007647  \n",
              "1  -0.075178  0.184460  \n",
              "2  -0.057447  0.382262  "
            ]
          },
          "execution_count": 32,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Сравнение метрик на тесте ДО и ПОСЛЕ добавления новых данных\n",
        "test_before = results_before[results_before[\"split\"] == \"test\"].copy()\n",
        "test_after = results_after[results_after[\"split\"] == \"test\"].copy()\n",
        "\n",
        "compare = test_before.merge(test_after, on=\"model\", suffixes=(\"_before\", \"_after\"))\n",
        "compare[[\"model\", \"MAE_before\", \"MAE_after\", \"RMSE_before\", \"RMSE_after\", \"R2_before\", \"R2_after\"]]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 33,
      "id": "0821f421",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 30632 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Визуализация влияния новых данных (R2)\n",
        "plt.figure(figsize=(7, 4))\n",
        "compare_melt = compare.melt(id_vars=\"model\", value_vars=[\"R2_before\", \"R2_after\"], var_name=\"stage\", value_name=\"R2\")\n",
        "sns.barplot(data=compare_melt, x=\"model\", y=\"R2\", hue=\"stage\")\n",
        "plt.title(\"R2 на тесте: до и после добавления новых данных\")\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7905ea14",
      "metadata": {},
      "source": [
        "### Итоговый анализ\n",
        "\n",
        "- Новые данные добавлены и приведены к единому формату.\n",
        "- Для корректного сравнения смотрим метрики `до` и `после`.\n",
        "\n",
        "Качество предсказания моделей после объединения данных заметно выросло, особенно у градиентного бустинга. Причина — в дополнительных данных (cars_new.csv) признаки связаны с ценой (год, состояние, пробег), что позволяет моделям лучше обобщать. Ниже — анализ корреляций в новых данных."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 34,
      "id": "9d37afe8",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 22196 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "image/png": "[encoded data, 27308 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "image/png": "[encoded data, 26024 characters]",
            "text/plain": [
              "<Figure size 700x400 with 1 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "image/png": "[encoded data, 53144 characters]",
            "text/plain": [
              "<Figure size 1000x800 with 2 Axes>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Анализ и визуализация cars_new.csv (как в Задании 1)\n",
        "df_plot = df_new.dropna(subset=[\"Price\"]).copy()\n",
        "\n",
        "# 1. Распределение цены\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.histplot(df_plot[\"Price\"], bins=50)\n",
        "plt.title(\"Распределение цены (cars_new.csv)\")\n",
        "plt.show()\n",
        "\n",
        "# 2. Цена vs Год выпуска\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.scatterplot(data=df_plot, x=\"Year\", y=\"Price\", alpha=0.6)\n",
        "plt.title(\"Цена vs Год выпуска (cars_new.csv)\")\n",
        "plt.show()\n",
        "\n",
        "# 3. Цена vs Состояние\n",
        "plt.figure(figsize=(7, 4))\n",
        "sns.boxplot(data=df_plot, x=\"Condition\", y=\"Price\")\n",
        "plt.title(\"Цена vs Состояние (cars_new.csv)\")\n",
        "plt.show()\n",
        "\n",
        "# 4. Тепловая карта корреляций\n",
        "numeric_cols = df_plot.select_dtypes(include=[np.number]).columns\n",
        "if len(numeric_cols) > 1:\n",
        "    plt.figure(figsize=(10, 8))\n",
        "    corr = df_plot[numeric_cols].corr()\n",
        "    sns.heatmap(corr, annot=True, fmt=\".2f\", cmap=\"coolwarm\", center=0, square=True)\n",
        "    plt.title(\"Тепловая карта корреляций (cars_new.csv)\")\n",
        "    plt.tight_layout()\n",
        "    plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e6e0089c",
      "metadata": {},
      "source": [
        "## Анализ новых данных\n",
        "\n",
        "В дополнительных данных видно сильную корреляцию состояния автомобиля и цены; года выпуска и цены; что приводит к более точным прогнозам"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".venv (3.14.2)",
      "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.14.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
