AIM-PIbd-31-Kouvshinoff-T-A/lab_3/laba3.ipynb

1123 lines
175 KiB
Plaintext
Raw Permalink Normal View History

2024-10-19 02:38:29 +04:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['ID', 'Price', 'Levy', 'Manufacturer', 'Model', 'Prod. year',\n",
" 'Category', 'Leather interior', 'Fuel type', 'Engine volume', 'Mileage',\n",
" 'Cylinders', 'Gear box type', 'Drive wheels', 'Doors', 'Wheel', 'Color',\n",
" 'Airbags'],\n",
" dtype='object')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"df = pd.read_csv(\"..//static//csv//car_price_prediction.csv\", sep=\",\")\n",
"df.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"определение бизнесцелей:\n",
"1. Прогнозирование цены автомобиля.\n",
"2. Оценка факторов, влияющих на цену автомобиля."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Определение целей технического проекта:\n",
"1. Построить модель машинного обучения для регрессии, которая будет прогнозировать стоимость автомобиля на основе предоставленных данных о его характеристиках.\n",
"2. Провести анализ данных для выявления ключевых факторов, влияющих на стоимость автомобиля."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"преобразуем пробег в число"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"df['Mileage'] = df['Mileage'].str.replace(r'\\D+', '', regex=True).astype(float)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"посмотрим выбросы и усредним их:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Колонка Price:\n",
" Есть выбросы: Да\n",
" Количество выбросов: 1073\n",
" Минимальное значение: 1.0\n",
" Максимальное значение: 47191.0\n",
" 1-й квартиль (Q1): 5331.0\n",
" 3-й квартиль (Q3): 22075.0\n",
"\n",
"Колонка Mileage:\n",
" Есть выбросы: Да\n",
" Количество выбросов: 640\n",
" Минимальное значение: 0.0\n",
" Максимальное значение: 367011.5\n",
" 1-й квартиль (Q1): 70139.0\n",
" 3-й квартиль (Q3): 188888.0\n",
"\n"
]
}
],
"source": [
"numeric_columns = ['Price', 'Mileage']\n",
"for column in numeric_columns:\n",
" if pd.api.types.is_numeric_dtype(df[column]): # Проверяем, является ли колонка числовой\n",
" q1 = df[column].quantile(0.25) # Находим 1-й квартиль (Q1)\n",
" q3 = df[column].quantile(0.75) # Находим 3-й квартиль (Q3)\n",
" iqr = q3 - q1 # Вычисляем межквартильный размах (IQR)\n",
"\n",
" # Определяем границы для выбросов\n",
" lower_bound = q1 - 1.5 * iqr # Нижняя граница\n",
" upper_bound = q3 + 1.5 * iqr # Верхняя граница\n",
"\n",
" # Подсчитываем количество выбросов\n",
" outliers = df[(df[column] < lower_bound) | (df[column] > upper_bound)]\n",
" outlier_count = outliers.shape[0]\n",
"\n",
" # Устраняем выбросы: заменяем значения ниже нижней границы на саму нижнюю границу, а выше верхней — на верхнюю\n",
" df[column] = df[column].apply(lambda x: lower_bound if x < lower_bound else upper_bound if x > upper_bound else x)\n",
"\n",
" print(f\"Колонка {column}:\")\n",
" print(f\" Есть выбросы: {'Да' if outlier_count > 0 else 'Нет'}\")\n",
" print(f\" Количество выбросов: {outlier_count}\")\n",
" print(f\" Минимальное значение: {df[column].min()}\")\n",
" print(f\" Максимальное значение: {df[column].max()}\")\n",
" print(f\" 1-й квартиль (Q1): {q1}\")\n",
" print(f\" 3-й квартиль (Q3): {q3}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"превращаем тире во чтото\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\bingo\\AppData\\Local\\Temp\\ipykernel_13744\\3336777531.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
"The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
"\n",
"For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
"\n",
"\n",
" df['Levy'].replace(\"-\", np.nan, inplace=True)\n",
"C:\\Users\\bingo\\AppData\\Local\\Temp\\ipykernel_13744\\3336777531.py:9: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
"The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
"\n",
"For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
"\n",
"\n",
" df['Levy'].fillna(df['Levy'].median(), inplace=True)\n"
]
}
],
"source": [
"import numpy as np\n",
"# Замена прочерков \"-\" в столбце Levy на NaN\n",
"df['Levy'].replace(\"-\", np.nan, inplace=True)\n",
"\n",
"# Преобразование столбца Levy в числовой формат (если он был строковым)\n",
"df['Levy'] = pd.to_numeric(df['Levy'], errors='coerce')\n",
"\n",
"# Заполнение пропусков в столбце Levy медианой\n",
"df['Levy'].fillna(df['Levy'].median(), inplace=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"код конструирования полей, все номинальные превращаем в числовые\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ID Price Levy Model Mileage Cylinders Airbags Age \\\n",
"0 45654403 13328.0 1399.0 1242 186005.0 6.0 12 14 \n",
"1 44731507 16621.0 1018.0 658 192000.0 6.0 8 13 \n",
"2 45774419 8467.0 781.0 684 200000.0 4.0 2 18 \n",
"3 45769185 3607.0 862.0 661 168966.0 4.0 0 13 \n",
"4 45809263 11726.0 446.0 684 91901.0 4.0 4 10 \n",
"\n",
" Leather Interior Manufacturer_ALFA ROMEO ... Color_Orange Color_Pink \\\n",
"0 1 0.0 ... 0.0 0.0 \n",
"1 0 0.0 ... 0.0 0.0 \n",
"2 0 0.0 ... 0.0 0.0 \n",
"3 1 0.0 ... 0.0 0.0 \n",
"4 1 0.0 ... 0.0 0.0 \n",
"\n",
" Color_Purple Color_Red Color_Silver Color_Sky blue Color_White \\\n",
"0 0.0 0.0 1.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 0.0 1.0 \n",
"4 0.0 0.0 1.0 0.0 0.0 \n",
"\n",
" Color_Yellow Drive wheels_Front Drive wheels_Rear \n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 1.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 1.0 0.0 \n",
"\n",
"[5 rows x 218 columns]\n"
]
}
],
"source": [
"from sklearn.preprocessing import OneHotEncoder, LabelEncoder\n",
"#Пример: Создание нового признака \"Age\" (возраст автомобиля)\n",
"df['Age'] = 2024 - df['Prod. year']\n",
"\n",
"df['Leather Interior'] = df['Leather interior'].apply(lambda x: 1 if x == 'Yes' else 0)\n",
"\n",
"# Удаление ненужного столбца 'Prod. year', так как он был использован для создания 'Age'\n",
"df.drop(columns=['Prod. year'], inplace=True)\n",
"df.drop(columns=['Leather interior'], inplace=True)\n",
"\n",
"# Определение категориальных признаков для преобразования\n",
"categorical_columns = ['Manufacturer', 'Engine volume', 'Doors', 'Wheel', 'Category', 'Fuel type', 'Gear box type', 'Color', 'Drive wheels']\n",
"\n",
"# Инициализация OneHotEncoder\n",
"encoder = OneHotEncoder(sparse_output=False, drop=\"first\")\n",
"\n",
"# Применение OneHotEncoder к выбранным категориальным признакам\n",
"encoded_values = encoder.fit_transform(df[categorical_columns])\n",
"\n",
"# Получение имен новых закодированных столбцов\n",
"encoded_columns = encoder.get_feature_names_out(categorical_columns)\n",
"\n",
"# Преобразование в DataFrame\n",
"encoded_values_df = pd.DataFrame(encoded_values, columns=encoded_columns)\n",
"\n",
"# Объединение закодированных значений с оригинальным DataFrame, исключив исходные категориальные столбцы\n",
"df = df.drop(columns=categorical_columns)\n",
"df = pd.concat([df.reset_index(drop=True), encoded_values_df.reset_index(drop=True)], axis=1)\n",
"\n",
"# Применение Label Encoding для столбца 'Model'\n",
"label_encoder = LabelEncoder()\n",
"df['Model'] = label_encoder.fit_transform(df['Model'])\n",
"\n",
"print(df.head())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Создадим выборки данных. разбивать будем относительно параметра цены, ведь это тот самый параметр по которому наша выборка разбивается на классы. И собственно его нам и надо будет предсказывать"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Размер обучающей выборки: (11542, 217)\n",
"Размер контрольной выборки: (3847, 217)\n",
"Размер тестовой выборки: (3848, 217)\n"
]
}
],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"# Выделение признаков (X) и целевой переменной (y)\n",
"X = df.drop(columns=['Price']) # Признаки\n",
"y = df['Price'] # Целевая переменная (цена автомобиля)\n",
"\n",
"# Разделение данных на обучающую и временную выборки\n",
"X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, random_state=42)\n",
"\n",
"# Разделение временной выборки на контрольную и тестовую выборки\n",
"X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)\n",
"\n",
"# Проверка размеров выборок\n",
"print(f\"Размер обучающей выборки: {X_train.shape}\")\n",
"print(f\"Размер контрольной выборки: {X_val.shape}\")\n",
"print(f\"Размер тестовой выборки: {X_test.shape}\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1sAAAIjCAYAAAD1OgEdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlc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mn7ySefqEuXLvLx8VFoaKiGDRtW7vbPtW+nczgcmjFjhtq1aydvb29FRkbq4Ycf1qlTp1zaNWnSRDfeeGOZ7YwdO7bMc5ZX+2uvvVbmNZVKTm178cUX1bx5c3l5eSkmJkbPPvusCgsLy32tTte7d2+1b9++zPLXX39dJpNJR44ccVmekZGhJ554QjExMfLy8lLz5s01depUORwOZ5vS1+31118v87zt27cvd0z85z//OWuN999/f4VO42rSpInz52M2mxUVFaW77rpL8fHx5+0rSe+++67atWsnLy8vRUdHa8yYMcrIyHCu37t3r06dOqWAgAD16tVLvr6+CgoK0o033qgdO3Y4261YsUImk0lfffVVmW3Mnz9fJpNJq1evdtZ8//33u7QpfU1++ukn57JVq1bpjjvuUKNGjZw/4yeffFL5+fkufSdOnFhmLM2bN0+dO3eWt7e3wsLCdPfdd5d5Te6//375+/u7LPvPf/5Tpg5J8vf3L1OzVLH3Ve/evZ0//7Zt26pLly7aunVrue+r8pz5Pg8PD9eQIUNcXn+p5P0zduzYsz7P3LlzXcb3+vXr5XA4VFRUpK5du57ztZKk5cuXq2fPnvLz81NwcLBuueUW7d6926VN6c9iz549uvPOOxUYGOg8bbKgoKBMvae/34uLizV48GCFhoZq165dLm0r+vsLwLlxZAtAtSkNRmFhYZKkQ4cOadGiRbrjjjsUGxurlJQU/fOf/1SvXr20a9cuRUdHS5LsdrtuvPFGLVu2TMOGDdPjjz+u7OxsLV26VDt27FCzZs2c27j77rs1ePBgl+2OHz++3HpeeeUVmUwmPffcc0pNTdWMGTPUt29fbdmyRT4+PpJKPswMGjRIXbp00Ysvviiz2aw5c+bohhtu0KpVq3T11VeXed6GDRtqypQpkqScnBw98sgj5W77hRde0J133qk///nPOnHihN5++21dd9112rx5s4KDg8v0GT16tHr27ClJ+vLLL8t8iH744Yed18s99thjOnz4sN555x1t3rxZv/76q6xWa7mvQ2VkZGQ49+10DodDN998s3755ReNHj1abdq00fbt2/Xmm29q3759LhMRXKi8vDz16tVLCQkJevjhh9WoUSP99ttvG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MtJo1qyZkZOT42zz008/GZKMp556yrls5MiRhp+fn8vzL1y4sEwdhmEYfn5+Lq9zZd5XvXr1cvn5f/fdd4YkY+DAgWXeA+U5s79hGMbf/vY3Q5KRmprqXCbJGDNmzFmfp/R3Zen4Ln3ctm1bl9e69Gdx+mvVuXNnIyIiwjh58qRz2datWw2z2ezysyz9Wdx8880u2/7rX/9qSDK2bt3qUm/puBg/frxhsViMRYsWufSr7O8vAOfGaYQAqqxv376qV6+eYmJiNGzYMPn7++urr75SgwYNJJXMUmg2l/yasdvtOnnypPz9/dWqVStt2rTJ+TxffPGFwsPD9eijj5bZRkVO+TmbESNGKCAgwPn4T3/6k+rXr6/vvvtOUslU0Pv379c999yjkydPKi0tTWlpacrNzVWfPn20cuVKl9PWpJLTHb29vc+53S+//FIOh0N33nmn8znT0tIUFRWlFi1aaMWKFS7ti4qKJJW8XmezcOFCBQUFqV+/fi7P2aVLF/n7+5d5TpvN5tIuLS2tzClFZ0pISNDbb7+tF154ocypXgsXLlSbNm3UunVrl+csPXX0zO1fiIULF6pnz54KCQlx2Vbfvn1lt9u1cuVKl/Z5eXl
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1IAAAIjCAYAAAAJLyrXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB2ZklEQVR4nO3dd3xUVf7/8ffMZCa9QEISWiAQelVUiIioNBF3bbs2VEBsLLgqii67riC6i4oNFct+VwF/FhR7lypY6L13CCWFJKS3ycz9/REzy5AAuSFhkvB6Ph55JHPvufd+7uTMZN65955rMQzDEAAAAACgyqy+LgAAAAAA6huCFAAAAACYRJACAAAAAJMIUgAAAABgEkEKAAAAAEwiSAEAAACASQQpAAAAADCJIAUAAAAAJvn5ugAAAIBzSUlJiTIzM+V2u9WsWTNflwOgmjgiBQAAGoT33ntP+/fv9zyeNWuWDh8+7LuCjrN69WrdeuutioqKkr+/v5o2baobbrjB12UBOAMEKeAcNGvWLFksFs9XQECA2rdvr3Hjxik1NdXX5QFAtfz888969NFHtX//fv34448aO3asrFbff9T58ssvdckll2jr1q3617/+pfnz52v+/Pl66623fF0agDPAqX3AOWzKlCmKj49XUVGRfvnlF73xxhv67rvvtHnzZgUFBfm6PAAw5aGHHtJll12m+Ph4SdL48ePVtGlTn9aUmZmpu+66S0OGDNHcuXPlcDh8Wg+AmkOQAs5hQ4cO1QUXXCBJuuuuuxQZGakXX3xRX375pW655RYfVwcA5nTs2FF79uzR5s2bFRUVpbZt2/q6JM2cOVNFRUWaNWsWIQpoYHx/vBtAnXHFFVdIkvbt2yep7D+pjzzyiLp166aQkBCFhYVp6NCh2rBhQ4Vli4qKNHnyZLVv314BAQFq2rSprr/+eu3Zs0eStH//fq/TCU/8uuyyyzzr+umnn2SxWPTRRx/p73//u2JjYxUcHKw//vGPOnjwYIVtr1ixQldeeaXCw8MVFBSk/v3769dff610Hy+77LJKtz958uQKbd977z316tVLgYGBaty4sW6++eZKt3+qfTue2+3Wyy+/rC5duiggIEAxMTG69957dezYMa92rVu31tVXX11hO+PGjauwzspqnzZtWoXnVJKKi4s1adIkJSQkyN/fXy1bttSjjz6q4uLiSp+r41122WXq2rVrhenPP/+8LBaL13UpkpSVlaUHH3xQLVu2lL+/vxISEvTss8/K7XZ72pQ/b88//3yF9Xbt2rXSPvHJJ5+ctMaRI0eqdevWp92X1q1be34/VqtVsbGxuummm5SUlFSlZUeOHOk17Z577lFAQIB++uknr+mvv/66unTpIn9/fzVr1kxjx45VVlaWV5uqPq/H11zZV/l+H/+cvvTSS2rVqpUCAwPVv39/bd68ucJ2Fi1apH79+ik4OFgRERG65pprtG3bttM+b8d/Hb/fJ+u7xzPze5ektLQ0jR49WjExMQoICFCPHj00e/bsStc5a9YsBQcHq3fv3mrbtq3Gjh0ri8VS4Xd2sprKv+x2u1q3bq0JEyaopKTE0678tOjVq1efdF2XXXaZ1z4sX75cPXv21L///W/P66Fdu3Z65plnvF4PklRaWqqnnnpKbdu2lb+/v1q3bq2///3vFV6j5c/zvHnz1LNnTwUEBKhz58767LPPvNqV13v863PLli1q1KiRrr76apWWlnqmV+U1C8AbR6QAeJSHnsjISEnS3r179cUXX+jPf/6z4uPjlZqaqrfeekv9+/fX1q1bPaNNuVwuXX311Vq4cKFuvvlmPfDAA8rNzdX8+fO1efNmr/8K33LLLbrqqqu8tjtx4sRK6/nXv/4li8Wixx57TGlpaXr55Zc1cOBArV+/XoGBgZLKPggOHTpUvXr10qRJk2S1WjVz5kxdccUV+vnnn3XRRRdVWG+LFi00depUSVJeXp7GjBlT6bb/+c9/6sYbb9Rdd92lo0eP6tVXX9Wll16qdevWKSIiosIy99xzj/r16ydJ+uyzz/T55597zb/33ns1a9YsjRo1Sn/961+1b98+vfbaa1q3bp1+/fVX2e32Sp8HM7Kysjz7djy3260//vGP+uWXX3TPPfeoU6dO2rRpk1566SXt3LlTX3zxxRlvu1xBQYH69++vw4cP695771VcXJx+++03TZw4UcnJyXr55ZdrbFvV1a9fP91zzz1yu93avHmzXn75ZR05ckQ///yzqfVMmjRJb7/9tj766COvD8+TJ0/Wk08+qYEDB2rMmDHasWOH3njjDa1atapav+uXX35ZeXl5kqRt27bp3//+t/7+97+rU6dOkqSQkBCv9u+++65yc3M1duxYFRUVafr06briiiu0adMmxcTESJIWLFigoUOHqk2bNpo8ebIKCwv16quvqm/fvlq7dm2lobT8eTu+jtpUWFioyy67TLt379a4ceMUHx+vuXPnauTIkcrKytIDDzxw0mV3796t//u//zO1vfLXcHFxsX788Uc9//zzCggI0FNPPVXtfcjIyNAvv/yiX375RXfeead69eqlhQsXauLEidq/f7/efPNNT9u77rpLs2fP1p/+9Cc9/PDDWrFihaZOnapt27ZVeD/ZtWuXbrrpJt13330aMWKEZs6cqT//+c/64YcfNGj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GCAli5dWuG0lKKiIgUEBJxyu5999pncbrduvPFGzzrT09MVGxurdu3aafHixV7ty0/98ff3P+k6586dq/DwcA0aNMhrnb169VJISEiFdTqdTq926enpKioqOmXdhw8f1quvvqp//vOfFY5QzJ07V506dVLHjh291ll+OueJ2z8Tc+fOVb9+/dSoUSOvbQ0cOFAul0tLly71al9QUFBhX10uV6Xrzs3NVXp6eoVT5MwqLi5Wenq60tLSNH/+fC1atEgDBgyo8vJffvml/vKXv2jChAk
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1IAAAIjCAYAAAAJLyrXAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB0AElEQVR4nO3dd3wUdf7H8ffuZnfTE5KQhJAC0juKhaiIBUHkbOjZFT3bcVixHacnoN5hO9uJnt4p6M+OXQ+RImIBRHoLvQRII0B62zK/P0L2WBIgExI2Ca/n47EPsjPfmfnM7nfDvjMz37EYhmEIAAAAAFBv1kAXAAAAAAAtDUEKAAAAAEwiSAEAAACASQQpAAAAADCJIAUAAAAAJhGkAAAAAMAkghQAAAAAmESQAgAAAACTCFIAAAD15Ha7lZeXp8zMzECXAiDACFIAACDgvvnmGy1fvtz3/IsvvtCaNWsCV9ABNm7cqNtuu03t2rWTw+FQQkKC0tPTZRhGoEsDEEAEKQANMnXqVFksFt8jODhYXbt21Z133qnc3NxAlweghVm1apXuuecebdy4UQsXLtQf//hHFRcXB7osLVy4UKeeeqq+//57/fnPf9Z3332nWbNm6YsvvpDFYgl0eQACyGLw5xQADTB16lTdfPPNevzxx9WxY0dVVFTo559/1v/93/8pLS1Nq1evVmhoaKDLBNBC7N69W6effro2bdokSRo5cqQ+/fTTgNZUVVWlfv36KTIyUjNnzlRUVFRA6wHQvAQFugAALdvw4cN18sknS5JuvfVWxcbG6vnnn9eXX36pa665JsDVAWgp2rZtq9WrV/v+CNOjR49Al6Svv/5a69ev17p16whRAGrh1D4Ajercc8+VJG3dulWStHfvXj3wwAPq06ePwsPDFRkZqeHDh2vFihW1lq2oqNCECRPUtWtXBQcHq127dho5cqQ2b94sSdq2bZvf6YQHP84++2zfun744QdZLBZ99NFH+stf/qLExESFhYXp4osv1o4dO2pt+9dff9UFF1ygqKgohYaGavDgwfrll1/q3Mezzz67zu1PmDChVtt3331XAwYMUEhIiGJiYnT11VfXuf3D7duBvF6vXnzxRfXq1UvBwcFKSEjQHXfcoX379vm169Chg373u9/V2s6dd95Za5111f7ss8/Wek0lqbKyUuPHj1fnzp3ldDqVkpKihx56SJWVlXW+Vgc6++yz1bt371rTn3vuOVksFm3bts1vekFBge69916lpKTI6XSqc+fOevrpp+X1en1tal635557rtZ6e/fuXWef+OSTTw5Z40033aQOHToccV86dOjge3+sVqsSExN11VVXHXEAggOXq+tx4Lbr+15L0rfffqvBgwcrIiJCkZGROuWUU/T+++9LOnR/rauPud1uPfHEE+rUqZOcTqc6dOigv/zlL7Xe3/ruf2lpqe6//37fe9itWzc999xzta4tqumDTqdTAwYMUI8ePQ7ZB+ty4L7YbDa1b99et99+uwoKCnxtGvL+L1y4UB07dtSnn36qTp06yeFwKDU1VQ899JDKy8trLf/qq6+qV69ecjqdSkpK0pgxY/xqkP73OViyZIlOP/10hYSEqGPHjvrXv/7l166m3h9++ME3LSsrSx06dNDJJ5+skpIS3/Sj+VwCaDiOSAFoVDWhJzY2VpK0ZcsWffHFF/r973+vjh07Kjc3V6+//roGDx6stWvXKikpSZLk8Xj0u9/9TnPmzNHVV1+te+65R8XFxZo1a5ZWr16tTp06+bZxzTXX6MILL/Tb7rhx4+qs529/+5ssFosefvhh5eXl6cUXX9SQIUO0fPlyhYSESJK+//57DR8+XAMGDND48eNltVo1ZcoUnXvuufrpp5906qmn1lpvcnKyJk2aJEkqKSnR6NGj69z2X//6V1155ZW69dZbtXv3bv3zn//UWWedpWXLlik6OrrWMrfffrsGDRokSfrss8/0+eef+82/4447fKdV3n333dq6dateeeUVLVu2TL/88ovsdnudr4MZBQUFvn07kNfr1cUXX6yff/5Zt99+u3r06KFVq1bphRde0IYNG/TFF18c9bZrlJWVafDgwdq1a5fuuOMOpaamav78+Ro3bpyys7P14osvNtq2GmrQoEG6/fbb5fV6tXr1ar344ovKysrSTz/9dMhlXnzxRd8X4IyMDP3973/XX/7yF9/Rl/DwcF/b+r7XU6dO1R/+8Af16tVL48aNU3R0tJYtW6YZM2bo2muv1SOPPKJbb71VkpSfn6/77rvPr58d6NZbb9Xbb7+tK664Qvfff79+/fVXTZo0SRkZGbX64pH23zAMXXzxxZo7d65uueUW9e/fX999950efPBB7dq1Sy+88MIhX6dD9cHDueyyyzRy5Ei53W4tWLBAb7zxhsrLy/V///d/ptZzoD179mjLli36y1/+opEjR+r+++/X4sWL9eyzz2r16tX673//6wuiEyZM0MSJEzVkyBCNHj1a69ev12uvvabffvut1mdz3759uvDCC3XllVfqmmuu0ccff6zRo0fL4XDoD3/4Q521FBYWavjw4bLb7Zo+fbqvrxzLzyWAgxgA0ABTpkwxJBmzZ882du/ebezYscP48MMPjdjYWCMkJMTYuXOnYRiGUVFRYXg8Hr9lt27dajidTuPxxx/3TXvrrbcMScbzzz9fa1ter9e3nCTj2WefrdWmV69exuDBg33P586da0gy2rdvbxQVFfmmf/zxx4Yk46WXXvKtu0uXLsawYcN82zEMwygrKzM6duxonH/++bW2dfrppxu9e/f2Pd+9e7chyRg/frxv2rZt2wybzWb87W9/81t21apVRlBQUK3pGzduNCQZb7/9tm/a+PHjjQN/Tf/000+GJOO9997zW3bGjBm1pqelpRkjRoyoVfuYMWOMg3/1H1z7Qw89ZMTHxxsDBgzwe03/7//+z7BarcZPP/3kt/y//vUvQ5Lxyy+/1NregQYPHmz06tWr1vRnn33WkGRs3brVN+2JJ54wwsLCjA0bNvi1/fOf/2zYbDYjMzPTMIyG9Ylp06YdssZRo0YZaWlph90Pw6h+fUeNGuU37dprrzVCQ0OPuOzB9cydO7fWvPq+1wUFBUZERIRx2mmnGeXl5X5tD+zPNWperylTptSat3z5ckOSceutt/pNf+CBBwxJxvfff++bVp/9/+KLLwxJxpNPPunX7oorrjAsFouxadMm37T69sFDOXh5w6j+nPbs2dP3vCHv/6hRowxJxk033eTXruaz+fXXXxuGYRh5eXmGw+Ewhg4d6vf77pVXXjEkGW+99ZZv2uDBgw1Jxj/+8Q/ftMrKSqN///5GfHy8UVVV5Vfv3LlzjYqKCuPss8824uPj/V43wzj6zyWAhuPUPgBHZciQIWrbtq1SUlJ09dVXKzw8XJ9//rnat28vSXI6nbJaq3/VeDwe7dmzR+Hh4erWrZuWLl3qW8+nn36quLg43XXXXbW2cTQjY914442KiIjwPb/iiivUrl07TZ8+XZK0fPlybdy4Uddee6327Nmj/Px85efnq7S0VOedd55+/PFHv1PJpOpTEIODgw+73c8++0xer1dXXnmlb535+flKTExUly5dNHfuXL/2VVVVkqpfr0OZNm2aoqKidP755/utc8CAAQoPD6+1TpfL5dcuPz9fFRUVh617165d+uc//6m//vWvfkdHarbfo0cPde/e3W+dNadzHrz9ozFt2jQNGjRIbdq08dvWkCFD5PF49OOPP/q1Lysrq7WvHo+nznUXFxcrPz+/1ilXZlVWVio/P195eXmaNWuWvv/+e5133nlHtc4a9X2vZ82apeLiYv35z3+u1SfNfm5qPhNjx471m37//fdLkv773//6TT/S/k+fPl02m0133313rfUZhqFvv/22zjoO1wcPp6YP5OTk6NNPP9WKFSvqfD8a8v4/+OCDfs/vu+8+2Ww232sye/ZsVVVV6d577/X9vpOk2267TZGRkbVeu6CgIN1xxx2+5w6HQ3fccYfy8vK0ZMkSv7Zer1c33nijFi5cqOnTp/sdnZeO7ecSgD9O7QNwVCZ
"text/plain": [
"<Figure size 1000x600 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"# Функция для оценки распределения цены\n",
"def plot_distribution(y_data, title):\n",
" plt.figure(figsize=(10, 6))\n",
" sns.histplot(y_data, kde=True, bins=50)\n",
" plt.title(title)\n",
" plt.xlabel('Price')\n",
" plt.ylabel('Frequency')\n",
" plt.grid(True)\n",
" plt.show()\n",
"\n",
"# Оценка распределения цены в каждой выборке\n",
"plot_distribution(y_train, \"Распределение цены в обучающей выборке\")\n",
"plot_distribution(y_val, \"Распределение цены в контрольной выборке\")\n",
"plot_distribution(y_test, \"Распределение цены в тестовой выборке\")\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Нормированные данные:\n",
" ID Levy Model Mileage Cylinders Airbags Age \\\n",
"0 0.993528 0.112841 0.781624 0.506810 0.333333 0.750 0.123457 \n",
"1 0.956715 0.080072 0.414097 0.523144 0.333333 0.500 0.111111 \n",
"2 0.998315 0.059689 0.430459 0.544942 0.200000 0.125 0.172840 \n",
"3 0.998107 0.066655 0.415985 0.460383 0.200000 0.000 0.111111 \n",
"4 0.999705 0.030876 0.430459 0.250404 0.200000 0.250 0.074074 \n",
"\n",
" Leather Interior Manufacturer_ALFA ROMEO Manufacturer_ASTON MARTIN ... \\\n",
"0 1.0 0.0 0.0 ... \n",
"1 0.0 0.0 0.0 ... \n",
"2 0.0 0.0 0.0 ... \n",
"3 1.0 0.0 0.0 ... \n",
"4 1.0 0.0 0.0 ... \n",
"\n",
" Color_Orange Color_Pink Color_Purple Color_Red Color_Silver \\\n",
"0 0.0 0.0 0.0 0.0 1.0 \n",
"1 0.0 0.0 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 0.0 1.0 \n",
"\n",
" Color_Sky blue Color_White Color_Yellow Drive wheels_Front \\\n",
"0 0.0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 1.0 \n",
"3 0.0 1.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 1.0 \n",
"\n",
" Drive wheels_Rear \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"\n",
"[5 rows x 217 columns]\n",
"\n",
"Стандартизированные данные:\n",
" ID Levy Model Mileage Cylinders Airbags Age \\\n",
"0 0.083141 1.359450 0.923579 0.537156 1.180937 1.254005 0.161034 \n",
"1 -0.902262 0.382603 -0.498032 0.603282 1.180937 0.328091 -0.015379 \n",
"2 0.211286 -0.225042 -0.434741 0.691523 -0.485866 -1.060780 0.866685 \n",
"3 0.205697 -0.017366 -0.490729 0.349214 -0.485866 -1.523737 -0.015379 \n",
"4 0.248490 -1.083950 -0.434741 -0.500823 -0.485866 -0.597823 -0.544617 \n",
"\n",
" Leather Interior Manufacturer_ALFA ROMEO Manufacturer_ASTON MARTIN ... \\\n",
"0 0.615306 -0.014421 -0.00721 ... \n",
"1 -1.625208 -0.014421 -0.00721 ... \n",
"2 -1.625208 -0.014421 -0.00721 ... \n",
"3 0.615306 -0.014421 -0.00721 ... \n",
"4 0.615306 -0.014421 -0.00721 ... \n",
"\n",
" Color_Orange Color_Pink Color_Purple Color_Red Color_Silver \\\n",
"0 -0.115443 -0.036788 -0.045072 -0.185361 2.018180 \n",
"1 -0.115443 -0.036788 -0.045072 -0.185361 -0.495496 \n",
"2 -0.115443 -0.036788 -0.045072 -0.185361 -0.495496 \n",
"3 -0.115443 -0.036788 -0.045072 -0.185361 -0.495496 \n",
"4 -0.115443 -0.036788 -0.045072 -0.185361 2.018180 \n",
"\n",
" Color_Sky blue Color_White Color_Yellow Drive wheels_Front \\\n",
"0 -0.07989 -0.551707 -0.074436 -1.422413 \n",
"1 -0.07989 -0.551707 -0.074436 -1.422413 \n",
"2 -0.07989 -0.551707 -0.074436 0.703031 \n",
"3 -0.07989 1.812557 -0.074436 -1.422413 \n",
"4 -0.07989 -0.551707 -0.074436 0.703031 \n",
"\n",
" Drive wheels_Rear \n",
"0 -0.368962 \n",
"1 -0.368962 \n",
"2 -0.368962 \n",
"3 -0.368962 \n",
"4 -0.368962 \n",
"\n",
"[5 rows x 217 columns]\n"
]
}
],
"source": [
"from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
"\n",
"# Предполагаем, что вы уже выделили ваши признаки X\n",
"# Применение нормировки Min-Max к всем числовым признакам\n",
"min_max_scaler = MinMaxScaler()\n",
"X_normalized = pd.DataFrame(min_max_scaler.fit_transform(X), columns=X.columns)\n",
"\n",
"# Применение стандартизации к всем числовым признакам\n",
"standard_scaler = StandardScaler()\n",
"X_standardized = pd.DataFrame(standard_scaler.fit_transform(X), columns=X.columns)\n",
"\n",
"# Проверка первых 5 строк после нормировки\n",
"print(\"Нормированные данные:\")\n",
"print(X_normalized.head())\n",
"\n",
"# Проверка первых 5 строк после стандартизации\n",
"print(\"\\nСтандартизированные данные:\")\n",
"print(X_standardized.head())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## пример использование feature tools \n",
"попытаюсь вынести model в отдельную таблицу"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: featuretools in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (1.31.0)Note: you may need to restart the kernel to use updated packages.\n",
"\n",
"Requirement already satisfied: cloudpickle>=1.5.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (3.1.0)\n",
"Requirement already satisfied: holidays>=0.17 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (0.58)\n",
"Requirement already satisfied: numpy>=1.25.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (2.1.1)\n",
"Requirement already satisfied: packaging>=20.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (24.1)\n",
"Requirement already satisfied: pandas>=2.0.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (2.2.2)\n",
"Requirement already satisfied: psutil>=5.7.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (6.0.0)\n",
"Requirement already satisfied: scipy>=1.10.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (1.14.1)\n",
"Requirement already satisfied: tqdm>=4.66.3 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (4.66.5)\n",
"Requirement already satisfied: woodwork>=0.28.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from featuretools) (0.31.0)\n",
"Requirement already satisfied: python-dateutil in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from holidays>=0.17->featuretools) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from pandas>=2.0.0->featuretools) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.7 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from pandas>=2.0.0->featuretools) (2024.1)\n",
"Requirement already satisfied: colorama in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from tqdm>=4.66.3->featuretools) (0.4.6)\n",
"Requirement already satisfied: scikit-learn>=1.1.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from woodwork>=0.28.0->featuretools) (1.5.2)\n",
"Requirement already satisfied: importlib-resources>=5.10.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from woodwork>=0.28.0->featuretools) (6.4.5)\n",
"Requirement already satisfied: six>=1.5 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from python-dateutil->holidays>=0.17->featuretools) (1.16.0)\n",
"Requirement already satisfied: joblib>=1.2.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from scikit-learn>=1.1.0->woodwork>=0.28.0->featuretools) (1.4.2)\n",
"Requirement already satisfied: threadpoolctl>=3.1.0 in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (from scikit-learn>=1.1.0->woodwork>=0.28.0->featuretools) (3.5.0)\n"
]
}
],
"source": [
"pip install --upgrade featuretools"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: setuptools in d:\\мии\\aim-pibd-31-kouvshinoff-t-a\\laba\\lib\\site-packages (75.2.0)\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install --upgrade setuptools"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\featuretools\\entityset\\entityset.py:1733: UserWarning: index car_id not found in dataframe, creating new integer column\n",
" warnings.warn(\n",
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\featuretools\\synthesis\\dfs.py:321: UnusedPrimitiveWarning: Some specified primitives were not used during DFS:\n",
" trans_primitives: ['hour', 'weekday']\n",
" agg_primitives: ['any', 'mode']\n",
"This may be caused by a using a value of max_depth that is too small, not setting interesting values, or it may indicate no compatible columns for the primitive were found in the data. If the DFS call contained multiple instances of a primitive in the list above, none of them were used.\n",
" warnings.warn(warning_msg, UnusedPrimitiveWarning)\n",
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\featuretools\\computational_backends\\feature_set_calculator.py:785: FutureWarning: The provided callable <function mean at 0x0000018799E0EB60> is currently using SeriesGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
" ).agg(to_agg)\n"
]
},
{
"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>ID</th>\n",
" <th>Price</th>\n",
" <th>Levy</th>\n",
" <th>Mileage</th>\n",
" <th>Cylinders</th>\n",
" <th>Airbags</th>\n",
" <th>Age</th>\n",
" <th>Leather Interior</th>\n",
" <th>Manufacturer_ALFA ROMEO</th>\n",
" <th>Manufacturer_ASTON MARTIN</th>\n",
" <th>...</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_TOYOTA)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_UAZ)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_VAZ)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_VOLKSWAGEN)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_VOLVO)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_ZAZ)</th>\n",
" <th>model_table.MEAN(car_features.Manufacturer_სხვა)</th>\n",
" <th>model_table.MEAN(car_features.Mileage)</th>\n",
" <th>model_table.MEAN(car_features.Price)</th>\n",
" <th>model_table.MEAN(car_features.Wheel_Right-hand drive)</th>\n",
" </tr>\n",
" <tr>\n",
" <th>car_id</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></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>0</th>\n",
" <td>45654403</td>\n",
" <td>13328</td>\n",
" <td>1399.0</td>\n",
" <td>186005</td>\n",
" <td>6.0</td>\n",
" <td>12</td>\n",
" <td>14</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>186005.0</td>\n",
" <td>13328.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>44731507</td>\n",
" <td>16621</td>\n",
" <td>1018.0</td>\n",
" <td>192000</td>\n",
" <td>6.0</td>\n",
" <td>8</td>\n",
" <td>13</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>192000.0</td>\n",
" <td>16621.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>45774419</td>\n",
" <td>8467</td>\n",
" <td>781.0</td>\n",
" <td>200000</td>\n",
" <td>4.0</td>\n",
" <td>2</td>\n",
" <td>18</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>200000.0</td>\n",
" <td>8467.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>45769185</td>\n",
" <td>3607</td>\n",
" <td>862.0</td>\n",
" <td>168966</td>\n",
" <td>4.0</td>\n",
" <td>0</td>\n",
" <td>13</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>168966.0</td>\n",
" <td>3607.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>45809263</td>\n",
" <td>11726</td>\n",
" <td>446.0</td>\n",
" <td>91901</td>\n",
" <td>4.0</td>\n",
" <td>4</td>\n",
" <td>10</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>91901.0</td>\n",
" <td>11726.0</td>\n",
" <td>0.0</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\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>19232</th>\n",
" <td>45798355</td>\n",
" <td>8467</td>\n",
" <td>781.0</td>\n",
" <td>300000</td>\n",
" <td>4.0</td>\n",
" <td>5</td>\n",
" <td>25</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>300000.0</td>\n",
" <td>8467.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19233</th>\n",
" <td>45778856</td>\n",
" <td>15681</td>\n",
" <td>831.0</td>\n",
" <td>161600</td>\n",
" <td>4.0</td>\n",
" <td>8</td>\n",
" <td>13</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>161600.0</td>\n",
" <td>15681.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19234</th>\n",
" <td>45804997</td>\n",
" <td>26108</td>\n",
" <td>836.0</td>\n",
" <td>116365</td>\n",
" <td>4.0</td>\n",
" <td>4</td>\n",
" <td>14</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>116365.0</td>\n",
" <td>26108.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19235</th>\n",
" <td>45793526</td>\n",
" <td>5331</td>\n",
" <td>1288.0</td>\n",
" <td>51258</td>\n",
" <td>4.0</td>\n",
" <td>4</td>\n",
" <td>17</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>51258.0</td>\n",
" <td>5331.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19236</th>\n",
" <td>45813273</td>\n",
" <td>470</td>\n",
" <td>753.0</td>\n",
" <td>186923</td>\n",
" <td>4.0</td>\n",
" <td>12</td>\n",
" <td>12</td>\n",
" <td>1</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>...</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>186923.0</td>\n",
" <td>470.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>19237 rows × 438 columns</p>\n",
"</div>"
],
"text/plain": [
" ID Price Levy Mileage Cylinders Airbags Age \\\n",
"car_id \n",
"0 45654403 13328 1399.0 186005 6.0 12 14 \n",
"1 44731507 16621 1018.0 192000 6.0 8 13 \n",
"2 45774419 8467 781.0 200000 4.0 2 18 \n",
"3 45769185 3607 862.0 168966 4.0 0 13 \n",
"4 45809263 11726 446.0 91901 4.0 4 10 \n",
"... ... ... ... ... ... ... ... \n",
"19232 45798355 8467 781.0 300000 4.0 5 25 \n",
"19233 45778856 15681 831.0 161600 4.0 8 13 \n",
"19234 45804997 26108 836.0 116365 4.0 4 14 \n",
"19235 45793526 5331 1288.0 51258 4.0 4 17 \n",
"19236 45813273 470 753.0 186923 4.0 12 12 \n",
"\n",
" Leather Interior Manufacturer_ALFA ROMEO Manufacturer_ASTON MARTIN \\\n",
"car_id \n",
"0 1 0.0 0.0 \n",
"1 0 0.0 0.0 \n",
"2 0 0.0 0.0 \n",
"3 1 0.0 0.0 \n",
"4 1 0.0 0.0 \n",
"... ... ... ... \n",
"19232 1 0.0 0.0 \n",
"19233 1 0.0 0.0 \n",
"19234 1 0.0 0.0 \n",
"19235 1 0.0 0.0 \n",
"19236 1 0.0 0.0 \n",
"\n",
" ... model_table.MEAN(car_features.Manufacturer_TOYOTA) \\\n",
"car_id ... \n",
"0 ... 0.0 \n",
"1 ... 0.0 \n",
"2 ... 0.0 \n",
"3 ... 0.0 \n",
"4 ... 0.0 \n",
"... ... ... \n",
"19232 ... 0.0 \n",
"19233 ... 0.0 \n",
"19234 ... 0.0 \n",
"19235 ... 0.0 \n",
"19236 ... 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_UAZ) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_VAZ) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_VOLKSWAGEN) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_VOLVO) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_ZAZ) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Manufacturer_სხვა) \\\n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
" model_table.MEAN(car_features.Mileage) \\\n",
"car_id \n",
"0 186005.0 \n",
"1 192000.0 \n",
"2 200000.0 \n",
"3 168966.0 \n",
"4 91901.0 \n",
"... ... \n",
"19232 300000.0 \n",
"19233 161600.0 \n",
"19234 116365.0 \n",
"19235 51258.0 \n",
"19236 186923.0 \n",
"\n",
" model_table.MEAN(car_features.Price) \\\n",
"car_id \n",
"0 13328.0 \n",
"1 16621.0 \n",
"2 8467.0 \n",
"3 3607.0 \n",
"4 11726.0 \n",
"... ... \n",
"19232 8467.0 \n",
"19233 15681.0 \n",
"19234 26108.0 \n",
"19235 5331.0 \n",
"19236 470.0 \n",
"\n",
" model_table.MEAN(car_features.Wheel_Right-hand drive) \n",
"car_id \n",
"0 0.0 \n",
"1 0.0 \n",
"2 1.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"19232 0.0 \n",
"19233 0.0 \n",
"19234 0.0 \n",
"19235 0.0 \n",
"19236 0.0 \n",
"\n",
"[19237 rows x 438 columns]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import featuretools as ft\n",
"from woodwork.logical_types import Categorical, Integer\n",
"# Создание двух таблиц: одна с моделью, другая с остальными данными\n",
"model_df = df[['ID', 'Model']].drop_duplicates().reset_index(drop=True)\n",
"other_df = df.drop(columns=['Model'])\n",
"\n",
"# Создание уникального идентификатора для связи\n",
"model_df['model_id'] = model_df.index\n",
"other_df['model_id'] = other_df['ID'].map(model_df.set_index('ID')['model_id'])\n",
"\n",
"es = ft.EntitySet(id=\"orders\")\n",
"es = es.add_dataframe(\n",
" dataframe_name=\"model_table\",\n",
" dataframe=model_df,\n",
" index=\"model_id\", # Индекс для уникальной идентификации моделей\n",
" logical_types={\n",
" \"Model\": Categorical # Определяем логический тип для модели\n",
" },\n",
")\n",
"es = es.add_dataframe(\n",
" dataframe_name=\"car_features\",\n",
" dataframe=other_df,\n",
" index=\"car_id\", # Индекс для уникальной идентификации автомобилей\n",
" logical_types={\n",
" \"Price\": Integer, # Целевая переменная (цена)\n",
" \"Mileage\": Integer, # Пробег (числовой признак)\n",
" \"model_id\": Integer, # Пробег (числовой признак)\n",
" },\n",
")\n",
"es = es.add_relationship(\"model_table\", \"model_id\", \"car_features\", \"model_id\")\n",
"\n",
"feature_matrix, feature_defs = ft.dfs(\n",
" entityset=es,\n",
" target_dataframe_name=\"car_features\",\n",
" agg_primitives=[\"mean\", \"count\", \"mode\", \"any\"],\n",
" trans_primitives=[\"hour\", \"weekday\"],\n",
" max_depth=2,\n",
")\n",
"\n",
"feature_matrix"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "laba",
"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
}