AIM-PIbd-31-Kouvshinoff-T-A/lab_4/laba4.ipynb

1382 lines
128 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv(\"..//static//csv//car_price_prediction.csv\", sep=\",\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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>ID</th>\n",
" <th>Price</th>\n",
" <th>Levy</th>\n",
" <th>Manufacturer</th>\n",
" <th>Model</th>\n",
" <th>Prod. year</th>\n",
" <th>Category</th>\n",
" <th>Leather interior</th>\n",
" <th>Fuel type</th>\n",
" <th>Engine volume</th>\n",
" <th>Mileage</th>\n",
" <th>Cylinders</th>\n",
" <th>Gear box type</th>\n",
" <th>Drive wheels</th>\n",
" <th>Doors</th>\n",
" <th>Wheel</th>\n",
" <th>Color</th>\n",
" <th>Airbags</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>45654403</td>\n",
" <td>13328</td>\n",
" <td>1399</td>\n",
" <td>LEXUS</td>\n",
" <td>RX 450</td>\n",
" <td>2010</td>\n",
" <td>Jeep</td>\n",
" <td>Yes</td>\n",
" <td>Hybrid</td>\n",
" <td>3.5</td>\n",
" <td>186005 km</td>\n",
" <td>6.0</td>\n",
" <td>Automatic</td>\n",
" <td>4x4</td>\n",
" <td>04-May</td>\n",
" <td>Left wheel</td>\n",
" <td>Silver</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>44731507</td>\n",
" <td>16621</td>\n",
" <td>1018</td>\n",
" <td>CHEVROLET</td>\n",
" <td>Equinox</td>\n",
" <td>2011</td>\n",
" <td>Jeep</td>\n",
" <td>No</td>\n",
" <td>Petrol</td>\n",
" <td>3</td>\n",
" <td>192000 km</td>\n",
" <td>6.0</td>\n",
" <td>Tiptronic</td>\n",
" <td>4x4</td>\n",
" <td>04-May</td>\n",
" <td>Left wheel</td>\n",
" <td>Black</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>45774419</td>\n",
" <td>8467</td>\n",
" <td>-</td>\n",
" <td>HONDA</td>\n",
" <td>FIT</td>\n",
" <td>2006</td>\n",
" <td>Hatchback</td>\n",
" <td>No</td>\n",
" <td>Petrol</td>\n",
" <td>1.3</td>\n",
" <td>200000 km</td>\n",
" <td>4.0</td>\n",
" <td>Variator</td>\n",
" <td>Front</td>\n",
" <td>04-May</td>\n",
" <td>Right-hand drive</td>\n",
" <td>Black</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>45769185</td>\n",
" <td>3607</td>\n",
" <td>862</td>\n",
" <td>FORD</td>\n",
" <td>Escape</td>\n",
" <td>2011</td>\n",
" <td>Jeep</td>\n",
" <td>Yes</td>\n",
" <td>Hybrid</td>\n",
" <td>2.5</td>\n",
" <td>168966 km</td>\n",
" <td>4.0</td>\n",
" <td>Automatic</td>\n",
" <td>4x4</td>\n",
" <td>04-May</td>\n",
" <td>Left wheel</td>\n",
" <td>White</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>45809263</td>\n",
" <td>11726</td>\n",
" <td>446</td>\n",
" <td>HONDA</td>\n",
" <td>FIT</td>\n",
" <td>2014</td>\n",
" <td>Hatchback</td>\n",
" <td>Yes</td>\n",
" <td>Petrol</td>\n",
" <td>1.3</td>\n",
" <td>91901 km</td>\n",
" <td>4.0</td>\n",
" <td>Automatic</td>\n",
" <td>Front</td>\n",
" <td>04-May</td>\n",
" <td>Left wheel</td>\n",
" <td>Silver</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ID Price Levy Manufacturer Model Prod. year Category \\\n",
"0 45654403 13328 1399 LEXUS RX 450 2010 Jeep \n",
"1 44731507 16621 1018 CHEVROLET Equinox 2011 Jeep \n",
"2 45774419 8467 - HONDA FIT 2006 Hatchback \n",
"3 45769185 3607 862 FORD Escape 2011 Jeep \n",
"4 45809263 11726 446 HONDA FIT 2014 Hatchback \n",
"\n",
" Leather interior Fuel type Engine volume Mileage Cylinders \\\n",
"0 Yes Hybrid 3.5 186005 km 6.0 \n",
"1 No Petrol 3 192000 km 6.0 \n",
"2 No Petrol 1.3 200000 km 4.0 \n",
"3 Yes Hybrid 2.5 168966 km 4.0 \n",
"4 Yes Petrol 1.3 91901 km 4.0 \n",
"\n",
" Gear box type Drive wheels Doors Wheel Color Airbags \n",
"0 Automatic 4x4 04-May Left wheel Silver 12 \n",
"1 Tiptronic 4x4 04-May Left wheel Black 8 \n",
"2 Variator Front 04-May Right-hand drive Black 2 \n",
"3 Automatic 4x4 04-May Left wheel White 0 \n",
"4 Automatic Front 04-May Left wheel Silver 4 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Бизнес-цели\n",
"- **Задача регрессии**: Построить модель для предсказания цены автомобиля (`Price`) на основе его характеристик.\n",
"- **Задача классификации**: Определить категорию автомобиля (`Category`) по характеристикам."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ввиду того что я первый раз обучаю модель прогнозируемое качество предсказания - не выше 50 %"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ID int64\n",
"Price int64\n",
"Levy object\n",
"Manufacturer object\n",
"Model object\n",
"Prod. year int64\n",
"Category object\n",
"Leather interior object\n",
"Fuel type object\n",
"Engine volume object\n",
"Mileage object\n",
"Cylinders float64\n",
"Gear box type object\n",
"Drive wheels object\n",
"Doors object\n",
"Wheel object\n",
"Color object\n",
"Airbags int64\n",
"dtype: object"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# небольшая обработка данных"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"Q1 = df['Price'].quantile(0.25)\n",
"Q3 = df['Price'].quantile(0.75)\n",
"IQR = Q3 - Q1\n",
"df = df[(df['Price'] >= Q1 - 1.5 * IQR) & (df['Price'] <= Q3 + 1.5 * IQR)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"df['Levy']=pd.to_numeric(df['Levy'],errors='coerce')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"df['Mileage'] = df['Mileage'].str.replace(' km', '').str.replace(',', '')\n",
"df['Mileage'] = df['Mileage'].astype(int)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"df['Engine volume'] = df['Engine volume'].apply(lambda x: float(re.match(r'\\d+(\\.\\d+)?', x).group()) if isinstance(x, str) else x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"df.drop_duplicates(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Разделим данные на признаки и целевые переменные\n",
"X = df.drop(columns=['Price','ID']) # Признаки\n",
"y = df['Price'] # Целевая переменная для регрессии"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# наполняем пайплайн обработчиками"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.compose import ColumnTransformer\n",
"from sklearn.pipeline import Pipeline\n",
"from sklearn.impute import SimpleImputer\n",
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
"\n",
"# Определение числовых и категориальных столбцов\n",
"numeric_features = ['Prod. year', 'Engine volume', 'Mileage', 'Cylinders', 'Airbags']\n",
"categorical_features = ['Manufacturer', 'Model', 'Category', 'Fuel type', \n",
" 'Gear box type', 'Drive wheels', 'Doors', \n",
" 'Wheel', 'Color']\n",
"\n",
"# Обработка числовых данных\n",
"numeric_transformer = Pipeline(steps=[\n",
" ('imputer', SimpleImputer(strategy='median')), # Заполнение пропусков медианой\n",
" ('scaler', StandardScaler()) # Нормализация данных\n",
"])\n",
"\n",
"# Обработка категориальных данных\n",
"categorical_transformer = Pipeline(steps=[\n",
" ('imputer', SimpleImputer(strategy='most_frequent')), # Заполнение пропусков модой\n",
" ('onehot', OneHotEncoder(handle_unknown='ignore')) # Преобразование в One-Hot Encoding\n",
"])\n",
"\n",
"# Комбинированный трансформер\n",
"preprocessor = ColumnTransformer(\n",
" transformers=[\n",
" ('num', numeric_transformer, numeric_features), # Применяем числовую обработку\n",
" ('cat', categorical_transformer, categorical_features) # Применяем категориальную обработку\n",
" ]\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Transformed feature shape: (17869, 1610)\n"
]
}
],
"source": [
"X_transformed = preprocessor.fit_transform(X)\n",
"\n",
"print(f\"Transformed feature shape: {X_transformed.shape}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# посмотрим результат пайплайна"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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>Prod. year</th>\n",
" <th>Engine volume</th>\n",
" <th>Mileage</th>\n",
" <th>Cylinders</th>\n",
" <th>Airbags</th>\n",
" <th>Manufacturer_ACURA</th>\n",
" <th>Manufacturer_ALFA ROMEO</th>\n",
" <th>Manufacturer_AUDI</th>\n",
" <th>Manufacturer_BMW</th>\n",
" <th>Manufacturer_BUICK</th>\n",
" <th>...</th>\n",
" <th>Color_Green</th>\n",
" <th>Color_Grey</th>\n",
" <th>Color_Orange</th>\n",
" <th>Color_Pink</th>\n",
" <th>Color_Purple</th>\n",
" <th>Color_Red</th>\n",
" <th>Color_Silver</th>\n",
" <th>Color_Sky blue</th>\n",
" <th>Color_White</th>\n",
" <th>Color_Yellow</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-0.117071</td>\n",
" <td>1.427610</td>\n",
" <td>-0.029000</td>\n",
" <td>1.256101</td>\n",
" <td>1.255022</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>...</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>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.060861</td>\n",
" <td>0.844069</td>\n",
" <td>-0.028881</td>\n",
" <td>1.256101</td>\n",
" <td>0.330220</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>...</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>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-0.828800</td>\n",
" <td>-1.139970</td>\n",
" <td>-0.028722</td>\n",
" <td>-0.470989</td>\n",
" <td>-1.056983</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>...</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>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.060861</td>\n",
" <td>0.260528</td>\n",
" <td>-0.029340</td>\n",
" <td>-0.470989</td>\n",
" <td>-1.519384</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>...</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>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.594657</td>\n",
" <td>-1.139970</td>\n",
" <td>-0.030874</td>\n",
" <td>-0.470989</td>\n",
" <td>-0.594582</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>...</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>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.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>17864</th>\n",
" <td>-2.074326</td>\n",
" <td>-0.323013</td>\n",
" <td>-0.026730</td>\n",
" <td>-0.470989</td>\n",
" <td>-0.363382</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>...</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>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17865</th>\n",
" <td>0.060861</td>\n",
" <td>0.143820</td>\n",
" <td>-0.029486</td>\n",
" <td>-0.470989</td>\n",
" <td>0.330220</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>...</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>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17866</th>\n",
" <td>-0.117071</td>\n",
" <td>-0.323013</td>\n",
" <td>-0.030387</td>\n",
" <td>-0.470989</td>\n",
" <td>-0.594582</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>...</td>\n",
" <td>0.0</td>\n",
" <td>1.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>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17867</th>\n",
" <td>-0.650868</td>\n",
" <td>-0.323013</td>\n",
" <td>-0.031684</td>\n",
" <td>-0.470989</td>\n",
" <td>-0.594582</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>...</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>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17868</th>\n",
" <td>0.238793</td>\n",
" <td>0.143820</td>\n",
" <td>-0.028982</td>\n",
" <td>-0.470989</td>\n",
" <td>1.255022</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>...</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>0.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>17869 rows × 1610 columns</p>\n",
"</div>"
],
"text/plain": [
" Prod. year Engine volume Mileage Cylinders Airbags \\\n",
"0 -0.117071 1.427610 -0.029000 1.256101 1.255022 \n",
"1 0.060861 0.844069 -0.028881 1.256101 0.330220 \n",
"2 -0.828800 -1.139970 -0.028722 -0.470989 -1.056983 \n",
"3 0.060861 0.260528 -0.029340 -0.470989 -1.519384 \n",
"4 0.594657 -1.139970 -0.030874 -0.470989 -0.594582 \n",
"... ... ... ... ... ... \n",
"17864 -2.074326 -0.323013 -0.026730 -0.470989 -0.363382 \n",
"17865 0.060861 0.143820 -0.029486 -0.470989 0.330220 \n",
"17866 -0.117071 -0.323013 -0.030387 -0.470989 -0.594582 \n",
"17867 -0.650868 -0.323013 -0.031684 -0.470989 -0.594582 \n",
"17868 0.238793 0.143820 -0.028982 -0.470989 1.255022 \n",
"\n",
" Manufacturer_ACURA Manufacturer_ALFA ROMEO Manufacturer_AUDI \\\n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 \n",
"... ... ... ... \n",
"17864 0.0 0.0 0.0 \n",
"17865 0.0 0.0 0.0 \n",
"17866 0.0 0.0 0.0 \n",
"17867 0.0 0.0 0.0 \n",
"17868 0.0 0.0 0.0 \n",
"\n",
" Manufacturer_BMW Manufacturer_BUICK ... Color_Green Color_Grey \\\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 0.0 \n",
"3 0.0 0.0 ... 0.0 0.0 \n",
"4 0.0 0.0 ... 0.0 0.0 \n",
"... ... ... ... ... ... \n",
"17864 0.0 0.0 ... 0.0 0.0 \n",
"17865 0.0 0.0 ... 0.0 0.0 \n",
"17866 0.0 0.0 ... 0.0 1.0 \n",
"17867 0.0 0.0 ... 0.0 0.0 \n",
"17868 0.0 0.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",
"17864 0.0 0.0 0.0 0.0 1.0 \n",
"17865 0.0 0.0 0.0 1.0 0.0 \n",
"17866 0.0 0.0 0.0 0.0 0.0 \n",
"17867 0.0 0.0 0.0 0.0 0.0 \n",
"17868 0.0 0.0 0.0 0.0 0.0 \n",
"\n",
" Color_Sky blue Color_White Color_Yellow \n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 1.0 0.0 \n",
"4 0.0 0.0 0.0 \n",
"... ... ... ... \n",
"17864 0.0 0.0 0.0 \n",
"17865 0.0 0.0 0.0 \n",
"17866 0.0 0.0 0.0 \n",
"17867 0.0 0.0 0.0 \n",
"17868 0.0 1.0 0.0 \n",
"\n",
"[17869 rows x 1610 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Получим имена категориальных признаков после OneHotEncoder\n",
"categorical_feature_names = preprocessor.named_transformers_['cat']['onehot'].get_feature_names_out(categorical_features)\n",
"\n",
"# Объединим их с именами числовых признаков\n",
"feature_names = list(numeric_features) + list(categorical_feature_names)\n",
"\n",
"# Создадим DataFrame для преобразованных данных\n",
"X_transformed_df = pd.DataFrame(X_transformed.toarray() if hasattr(X_transformed, 'toarray') else X_transformed, columns=feature_names)\n",
"\n",
"# Выведем пример 5 строк\n",
"X_transformed_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# обучим 3 разные модели с применением RandomizedSearchCV(для подора гиперпараметров)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training LinearRegression...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\sklearn\\model_selection\\_search.py:320: UserWarning: The total space of parameters 1 is smaller than n_iter=10. Running 1 iterations. For exhaustive searches, use GridSearchCV.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training RandomForestRegressor...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\sklearn\\model_selection\\_search.py:320: UserWarning: The total space of parameters 9 is smaller than n_iter=10. Running 9 iterations. For exhaustive searches, use GridSearchCV.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training GradientBoostingRegressor...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\numpy\\ma\\core.py:2881: RuntimeWarning: invalid value encountered in cast\n",
" _data = np.array(data, dtype=dtype, copy=copy,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Model: LinearRegression\n",
"Best Params: {}\n",
"MAE: 6722.0902357642335\n",
"RMSE: 8991.273616765677\n",
"R2: 0.3722951567248176\n",
"\n",
"Model: RandomForestRegressor\n",
"Best Params: {'model__n_estimators': 200, 'model__max_depth': None}\n",
"MAE: 3568.360497561258\n",
"RMSE: 6055.406570308487\n",
"R2: 0.7152920023310496\n",
"\n",
"Model: GradientBoostingRegressor\n",
"Best Params: {'model__n_estimators': 200, 'model__max_depth': 10, 'model__learning_rate': 0.2}\n",
"MAE: 3933.35109066405\n",
"RMSE: 6171.208466996527\n",
"R2: 0.7042985281049783\n"
]
}
],
"source": [
"import numpy as np\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"from sklearn.ensemble import GradientBoostingRegressor\n",
"from sklearn.model_selection import GridSearchCV, RandomizedSearchCV\n",
"from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score\n",
"import matplotlib.pyplot as plt\n",
"\n",
"random_state = 42\n",
"\n",
"# Модели и параметры\n",
"models_regression = {\n",
" \"LinearRegression\": LinearRegression(),\n",
" \"RandomForestRegressor\": RandomForestRegressor(random_state=random_state),\n",
" \"GradientBoostingRegressor\": GradientBoostingRegressor(random_state=random_state)\n",
"}\n",
"\n",
"param_grids_regression = {\n",
" \"LinearRegression\": {},\n",
" \"RandomForestRegressor\": {\n",
" 'model__n_estimators': [50, 100, 200],\n",
" 'model__max_depth': [None, 10, 20],\n",
" },\n",
" \"GradientBoostingRegressor\": {\n",
" 'model__n_estimators': [50, 100, 200],\n",
" 'model__learning_rate': [0.01, 0.1, 0.2],\n",
" 'model__max_depth': [3, 5, 10]\n",
" }\n",
"}\n",
"\n",
"# Результаты\n",
"results_regression = {}\n",
"\n",
"# Перебор моделей\n",
"for name, model in models_regression.items():\n",
" print(f\"Training {name}...\")\n",
" pipeline = Pipeline(steps=[\n",
" ('preprocessor', preprocessor),\n",
" ('model', model)\n",
" ])\n",
" param_grid = param_grids_regression[name]\n",
" grid_search = RandomizedSearchCV(pipeline, param_grid, cv=5, scoring='neg_mean_absolute_error', n_jobs=-1)\n",
" grid_search.fit(X_train, y_train)\n",
"\n",
" # Лучшая модель\n",
" best_model = grid_search.best_estimator_\n",
" y_pred = best_model.predict(X_test)\n",
"\n",
" # Метрики\n",
" mae = mean_absolute_error(y_test, y_pred)\n",
" rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
" r2 = r2_score(y_test, y_pred)\n",
"\n",
" # Сохранение результатов\n",
" results_regression[name] = {\n",
" \"Best Params\": grid_search.best_params_,\n",
" \"MAE\": mae,\n",
" \"RMSE\": rmse,\n",
" \"R2\": r2\n",
" }\n",
"\n",
"# Печать результатов\n",
"for name, metrics in results_regression.items():\n",
" print(f\"\\nModel: {name}\")\n",
" for metric, value in metrics.items():\n",
" print(f\"{metric}: {value}\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"#T_7fd0f_row0_col0, #T_7fd0f_row0_col1 {\n",
" background-color: #26818e;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_7fd0f_row0_col2 {\n",
" background-color: #da5a6a;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_7fd0f_row1_col0 {\n",
" background-color: #228d8d;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_7fd0f_row1_col1 {\n",
" background-color: #25848e;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_7fd0f_row1_col2 {\n",
" background-color: #d7566c;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_7fd0f_row2_col0, #T_7fd0f_row2_col1 {\n",
" background-color: #a8db34;\n",
" color: #000000;\n",
"}\n",
"#T_7fd0f_row2_col2 {\n",
" background-color: #4e02a2;\n",
" color: #f1f1f1;\n",
"}\n",
"</style>\n",
"<table id=\"T_7fd0f\">\n",
" <thead>\n",
" <tr>\n",
" <th class=\"blank level0\" >&nbsp;</th>\n",
" <th id=\"T_7fd0f_level0_col0\" class=\"col_heading level0 col0\" >MAE</th>\n",
" <th id=\"T_7fd0f_level0_col1\" class=\"col_heading level0 col1\" >RMSE</th>\n",
" <th id=\"T_7fd0f_level0_col2\" class=\"col_heading level0 col2\" >R2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th id=\"T_7fd0f_level0_row0\" class=\"row_heading level0 row0\" >RandomForestRegressor</th>\n",
" <td id=\"T_7fd0f_row0_col0\" class=\"data row0 col0\" >3568.360498</td>\n",
" <td id=\"T_7fd0f_row0_col1\" class=\"data row0 col1\" >6055.406570</td>\n",
" <td id=\"T_7fd0f_row0_col2\" class=\"data row0 col2\" >0.715292</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_7fd0f_level0_row1\" class=\"row_heading level0 row1\" >GradientBoostingRegressor</th>\n",
" <td id=\"T_7fd0f_row1_col0\" class=\"data row1 col0\" >3933.351091</td>\n",
" <td id=\"T_7fd0f_row1_col1\" class=\"data row1 col1\" >6171.208467</td>\n",
" <td id=\"T_7fd0f_row1_col2\" class=\"data row1 col2\" >0.704299</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_7fd0f_level0_row2\" class=\"row_heading level0 row2\" >LinearRegression</th>\n",
" <td id=\"T_7fd0f_row2_col0\" class=\"data row2 col0\" >6722.090236</td>\n",
" <td id=\"T_7fd0f_row2_col1\" class=\"data row2 col1\" >8991.273617</td>\n",
" <td id=\"T_7fd0f_row2_col2\" class=\"data row2 col2\" >0.372295</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x29ebffeadb0>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Импортируем pandas для работы с таблицами\n",
"import pandas as pd\n",
"\n",
"# Формируем таблицу метрик\n",
"reg_metrics = pd.DataFrame.from_dict(results_regression, orient=\"index\")[\n",
" [\"MAE\", \"RMSE\", \"R2\"]\n",
"]\n",
"\n",
"# Визуализация результатов с помощью стилизации\n",
"styled_metrics = (\n",
" reg_metrics.sort_values(by=\"RMSE\")\n",
" .style.background_gradient(cmap=\"viridis\", low=1, high=0.3, subset=[\"RMSE\", \"MAE\"])\n",
" .background_gradient(cmap=\"plasma\", low=0.3, high=1, subset=[\"R2\"])\n",
")\n",
"\n",
"styled_metrics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# невероятно\n",
"Модель может что-то даже предсказать, с погрешностью в 3к$ конечно и ошибкой 70% но всё же. Я думал и 50% не будет.\n",
"Возможно если сузить сильнее входные данные, потому что выбросы очень большие, результат будет лучше. Линейная регрессия кстати вообще не справилась с данными а вот 2 другие ещё более менее"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# приступим к задаче классификации"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training LogisticRegression...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\sklearn\\model_selection\\_search.py:320: UserWarning: The total space of parameters 3 is smaller than n_iter=10. Running 3 iterations. For exhaustive searches, use GridSearchCV.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training RandomForestClassifier...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\МИИ\\AIM-PIbd-31-Kouvshinoff-T-A\\laba\\Lib\\site-packages\\numpy\\ma\\core.py:2881: RuntimeWarning: invalid value encountered in cast\n",
" _data = np.array(data, dtype=dtype, copy=copy,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training KNN...\n",
"\n",
"Model: LogisticRegression\n",
"Best Params: {'model__C': 10}\n",
"Accuracy: 0.8612199216564074\n",
"F1 Score: 0.9032383925087788\n",
"Confusion_matrix: [[ 763 303]\n",
" [ 193 2315]]\n",
"\n",
"Model: RandomForestClassifier\n",
"Best Params: {'model__n_estimators': 500, 'model__max_features': 'log2', 'model__max_depth': 20, 'model__criterion': 'gini'}\n",
"Accuracy: 0.802182428651371\n",
"F1 Score: 0.874800779174783\n",
"Confusion_matrix: [[ 397 669]\n",
" [ 38 2470]]\n",
"\n",
"Model: KNN\n",
"Best Params: {'model__weights': 'uniform', 'model__n_neighbors': 5}\n",
"Accuracy: 0.8718522663682149\n",
"F1 Score: 0.9082532051282052\n",
"Confusion_matrix: [[ 849 217]\n",
" [ 241 2267]]\n"
]
}
],
"source": [
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.metrics import accuracy_score, confusion_matrix, f1_score\n",
"\n",
"X = df.drop(columns=['Leather interior','ID']) # Признаки\n",
"# Целевая переменная для классификации\n",
"y_class = df['Leather interior'].map({'Yes': 1, 'No': 0}) # Преобразуем в 0/1\n",
"\n",
"# Разделение данных\n",
"X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X, y_class, test_size=0.2, random_state=42)\n",
"\n",
"# Модели и параметры\n",
"models_classification = {\n",
" \"LogisticRegression\": LogisticRegression(max_iter=1000),\n",
" \"RandomForestClassifier\": RandomForestClassifier(random_state=42),\n",
" \"KNN\": KNeighborsClassifier()\n",
"}\n",
"\n",
"param_grids_classification = {\n",
" \"LogisticRegression\": {\n",
" 'model__C': [0.1, 1, 10]\n",
" },\n",
" \"RandomForestClassifier\": {\n",
" \"model__n_estimators\": [10, 20, 30, 40, 50, 100, 150, 200, 250, 500],\n",
" \"model__max_features\": [\"sqrt\", \"log2\", 2],\n",
" \"model__max_depth\": [2, 3, 4, 5, 6, 7, 8, 9 ,10, 20],\n",
" \"model__criterion\": [\"gini\", \"entropy\", \"log_loss\"],\n",
" },\n",
" \"KNN\": {\n",
" 'model__n_neighbors': [3, 5, 7, 9, 11],\n",
" 'model__weights': ['uniform', 'distance']\n",
" }\n",
"}\n",
"\n",
"# Результаты\n",
"results_classification = {}\n",
"\n",
"# Перебор моделей\n",
"for name, model in models_classification.items():\n",
" print(f\"Training {name}...\")\n",
" pipeline = Pipeline(steps=[\n",
" ('preprocessor', preprocessor),\n",
" ('model', model)\n",
" ])\n",
" param_grid = param_grids_classification[name]\n",
" grid_search = RandomizedSearchCV(pipeline, param_grid, cv=5, scoring='f1', n_jobs=-1)\n",
" grid_search.fit(X_train_clf, y_train_clf)\n",
"\n",
" # Лучшая модель\n",
" best_model = grid_search.best_estimator_\n",
" y_pred = best_model.predict(X_test_clf)\n",
"\n",
" # Метрики\n",
" acc = accuracy_score(y_test_clf, y_pred)\n",
" f1 = f1_score(y_test_clf, y_pred)\n",
"\n",
" # Вычисление матрицы ошибок\n",
" c_matrix = confusion_matrix(y_test_clf, y_pred)\n",
"\n",
" # Сохранение результатов\n",
" results_classification[name] = {\n",
" \"Best Params\": grid_search.best_params_,\n",
" \"Accuracy\": acc,\n",
" \"F1 Score\": f1,\n",
" \"Confusion_matrix\": c_matrix\n",
" }\n",
"\n",
"# Печать результатов\n",
"for name, metrics in results_classification.items():\n",
" print(f\"\\nModel: {name}\")\n",
" for metric, value in metrics.items():\n",
" print(f\"{metric}: {value}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# отресуем красивые квадратики\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA/QAAAQTCAYAAADKw2LWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAADvaElEQVR4nOzdeVxU1f/H8feAMKBsogKSiFsuKK6VknsauGSZlmVmmktWWGml5rfcS8syM0ttMbW++rXS0lxSMS03tDTJJSM1zBXcQVDZ5v7+8MfUiAvojMDwej4e55Fz77lnzh2Izz1zNpNhGIYAAAAAAECR4lLQFQAAAAAAAPlHgx4AAAAAgCKIBj0AAAAAAEUQDXoAAAAAAIogGvQAAAAAABRBNOgBAAAAACiCaNADAAAAAFAElSjoCgAAYE8XL15URkaGXct0d3eXh4eHXcsEAKC4IDY7Dg16AIDTuHjxoiqHeinxeLZdyw0KClJCQgIPDgAA5BOx2bFo0AMAnEZGRoYSj2fr722V5ONtn1llKecsCm10QBkZGcX+oQEAgPwiNjsWDXoAgNPx8jbJy9tkl7Issk85AAAUZ8Rmx2BR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Hjxho3bpzS09NlNpsVFBSkpKQkmzxJSUny8fGhdx65XB5PUlJS9Omnn6p79+7q37+/zp07p5kzZyoqKko///xzruHe8+bN07lz5zRgwACZTCZNnDhRXbp00V9//WX93V2
"text/plain": [
"<Figure size 1200x1000 with 7 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.metrics import ConfusionMatrixDisplay\n",
"\n",
"\n",
"num_models = len(results_classification)\n",
"num_rows = (num_models // 2) + (num_models % 2) # Количество строк для подграфиков\n",
"_, ax = plt.subplots(num_rows, 2, figsize=(12, 10), sharex=False, sharey=False)\n",
"\n",
"for index, (name, metrics) in enumerate(results_classification.items()):\n",
" c_matrix = metrics[\"Confusion_matrix\"]\n",
" disp = ConfusionMatrixDisplay(\n",
" confusion_matrix=c_matrix, display_labels=[\"No\", \"Yes\"]\n",
" ).plot(ax=ax.flat[index])\n",
" disp.ax_.set_title(name)\n",
"\n",
"# Корректировка расположения графиков\n",
"plt.subplots_adjust(top=1, bottom=0, hspace=0.4, wspace=0.1)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style type=\"text/css\">\n",
"#T_93491_row0_col0 {\n",
" background-color: #21918c;\n",
" color: #f1f1f1;\n",
" background-color: #da5a6a;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_93491_row0_col1 {\n",
" background-color: #21918c;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_93491_row1_col0 {\n",
" background-color: #277e8e;\n",
" color: #f1f1f1;\n",
" background-color: #cb4679;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_93491_row1_col1 {\n",
" background-color: #277e8e;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_93491_row2_col0 {\n",
" background-color: #440154;\n",
" color: #f1f1f1;\n",
" background-color: #4e02a2;\n",
" color: #f1f1f1;\n",
"}\n",
"#T_93491_row2_col1 {\n",
" background-color: #440154;\n",
" color: #f1f1f1;\n",
"}\n",
"</style>\n",
"<table id=\"T_93491\">\n",
" <thead>\n",
" <tr>\n",
" <th class=\"blank level0\" >&nbsp;</th>\n",
" <th id=\"T_93491_level0_col0\" class=\"col_heading level0 col0\" >Accuracy</th>\n",
" <th id=\"T_93491_level0_col1\" class=\"col_heading level0 col1\" >F1 Score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th id=\"T_93491_level0_row0\" class=\"row_heading level0 row0\" >KNN</th>\n",
" <td id=\"T_93491_row0_col0\" class=\"data row0 col0\" >0.871852</td>\n",
" <td id=\"T_93491_row0_col1\" class=\"data row0 col1\" >0.908253</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_93491_level0_row1\" class=\"row_heading level0 row1\" >LogisticRegression</th>\n",
" <td id=\"T_93491_row1_col0\" class=\"data row1 col0\" >0.861220</td>\n",
" <td id=\"T_93491_row1_col1\" class=\"data row1 col1\" >0.903238</td>\n",
" </tr>\n",
" <tr>\n",
" <th id=\"T_93491_level0_row2\" class=\"row_heading level0 row2\" >RandomForestClassifier</th>\n",
" <td id=\"T_93491_row2_col0\" class=\"data row2 col0\" >0.802182</td>\n",
" <td id=\"T_93491_row2_col1\" class=\"data row2 col1\" >0.874801</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n"
],
"text/plain": [
"<pandas.io.formats.style.Styler at 0x29e94101340>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Формируем таблицу метрик классификации\n",
"clf_metrics = pd.DataFrame.from_dict(results_classification, orient=\"index\")[[\"Accuracy\", \"F1 Score\"]]\n",
"\n",
"# Визуализация результатов с помощью стилизации\n",
"styled_metrics_clf = (\n",
" clf_metrics.sort_values(by=\"F1 Score\", ascending=False) # Сортировка по F1 Score\n",
" .style.background_gradient(cmap=\"viridis\", low=0, high=1, subset=[\"F1 Score\", \"Accuracy\"]) # Стилизация столбцов\n",
" .background_gradient(cmap=\"plasma\", low=0.3, high=1, subset=[\"Accuracy\"])\n",
")\n",
"\n",
"styled_metrics_clf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"В итоге KNN и LogisticRegression выдали точность в 90% что я считаю весьма неплохо. RandomForestClassifier близко, но не так хорошо"
]
}
],
"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
}