30 lines
1.4 KiB
Python
30 lines
1.4 KiB
Python
import pandas as pd
|
|
from sklearn.metrics import accuracy_score, classification_report
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.neural_network import MLPClassifier
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
# Загрузка данных, выделение признаков и целевой переменной
|
|
data = pd.read_csv('stroke_prediction_ds.csv')
|
|
X = data[['hypertension', 'heart_disease', 'avg_glucose_level']]
|
|
y = pd.qcut(data['age'], q=3, labels=['18 лет', '18-55 лет', '55+ лет'])
|
|
|
|
# Разделение данных на обучающий и тестовый наборы
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
|
# Нормализация данных
|
|
scaler = MinMaxScaler()
|
|
X_train, X_test = scaler.fit_transform(X_train), scaler.transform(X_test)
|
|
|
|
# Создание и обучение MLPClassifier
|
|
mlp_classifier = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=1000, random_state=42)
|
|
mlp_classifier.fit(X_train, y_train)
|
|
|
|
# Предсказание на тестовых данных, оценка производительности модели и вывод результатов
|
|
y_pred = mlp_classifier.predict(X_test)
|
|
accuracy = accuracy_score(y_test, y_pred)
|
|
class_report = classification_report(y_test, y_pred)
|
|
|
|
print(f'Accuracy: {accuracy}')
|
|
print('Classification Report:')
|
|
print(class_report) |