IIS_2023_1/kurmyza_pavel_lab_4/main.py

48 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# Считываем данные датасета
data = pd.read_csv('hotel_bookings.csv')
# Удаляем столбцы, содержащие неопределенные данные
data = data.drop(['country', 'agent', 'company'], axis=1)
data = data.dropna()
# Находим категориальные признаки
list_1 = list(data.columns)
list_cate = []
for i in list_1:
if data[i].dtype == 'object':
list_cate.append(i)
# Производим кодирование признаков в числовой формат для того, чтобы модель могла с ними работать
le = LabelEncoder()
for i in list_cate:
data[i] = le.fit_transform(data[i])
# Исключаем тип отеля из набора входных данных
y = data['hotel']
x = data.drop('hotel', axis=1)
# Выделяем данные для обучения и тестирования
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, test_size=0.2)
# Создаем и обучаем модель логистической регрессии
reg = LogisticRegression()
reg.fit(x_train, y_train)
# Используем модель на тестовой выборке и оцениваем точность
y_pred_reg = reg.predict(x_test)
acc_reg = accuracy_score(y_test, y_pred_reg)
# Выводим результаты оценки точности и обучения
print("Classification Report is:\n", classification_report(y_test, y_pred_reg))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred_reg))
print("\nTraining Score:", reg.score(x_train, y_train) * 100)
print(f"Accuracy Score of Logistic Regression is {acc_reg}")