IIS_2023_1/ilbekov_dmitriy_lab_6/lab6.py

50 lines
2.0 KiB
Python
Raw Normal View History

2023-10-27 19:58:08 +04:00
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error
# Загрузка данных
data = pd.read_csv("F1DriversDataset.csv")
# Выбор признаков и целевой переменной
features = ['Race_Entries', 'Race_Starts', 'Pole_Positions', 'Race_Wins', 'Podiums', 'Fastest_Laps']
target = 'Championships'
X = data[features]
y = data[target]
# Разделение данных на обучающую и тестовую выборки
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=42)
# Обучение модели
model = MLPRegressor(
hidden_layer_sizes=(20, 20),
activation='relu',
solver='adam',
random_state=42
)
model.fit(X_train, y_train)
# Оценка качества модели на тестовой выборке
y_pred = model.predict(X_test)
score = model.score(X_test, y_test)
print("Коэффициент детерминации R^2 на тестовой выборке:", score)
# Предсказание количества чемпионских титулов для гонщика
new_features = []
for feature in features:
value = float(input(f"Введите значение для {feature}: "))
new_features.append(value)
new_features = [new_features]
predicted_championships = model.predict(new_features)
print("Предсказанное количество чемпионских титулов:", predicted_championships[0])
# Визуализация модели
plt.scatter(y_test, y_pred)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)
plt.xlabel('Фактическое количество чемпионских титулов')
plt.ylabel('Предсказанное количество чемпионских титулов')
plt.title('Предсказания модели MLPRegressor')
plt.show()