← Каталог
Алгоритмы ИИ — Пример вычисления метрик на Python
Фрагмент из «Алгоритмы ИИ»: Пример вычисления метрик на Python.
from sklearn.metrics import (
accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, confusion_matrix, classification_report
)
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# Генерация данных
X, y = make_classification(
n_samples=1000,
n_features=20,
n_classes=2,
weights=[0.85, 0.15], # Несбалансированные классы
random_state=42
)
# Разделение данных
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Обучение модели
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_proba = clf.predict_proba(X_test)[:, 1]
# Вычисление метрик
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_proba)
cm = confusion_matrix(y_test, y_pred)
print("Матрица ошибок:")
print(cm)
print(f"\nТочность: {accuracy:.4f}")
print(f"Точность (precision): {precision:.4f}")
print(f"Полнота (recall): {recall:.4f}")
print(f"F1-мера: {f1:.4f}")
print(f"ROC-AUC: {roc_auc:.4f}")
# Детальный отчёт по классам
print("\nДетальный отчёт:")
print(classification_report(y_test, y_pred, target_names=['Класс 0', 'Класс 1'])) from sklearn.metrics import (
accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, confusion_matrix, classification_report
)
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# Генерация данных
X, y = make_classification(
n_samples=1000,
n_features=20,
n_classes=2,
weights=[0.85, 0.15], # Несбалансированные классы
random_state=42
)
# Разделение данных
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Обучение модели
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_proba = clf.predict_proba(X_test)[:, 1]
# Вычисление метрик
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_proba)
cm = confusion_matrix(y_test, y_pred)
print("Матрица ошибок:")
print(cm)
print(f"\nТочность: {accuracy:.4f}")
print(f"Точность (precision): {precision:.4f}")
print(f"Полнота (recall): {recall:.4f}")
print(f"F1-мера: {f1:.4f}")
print(f"ROC-AUC: {roc_auc:.4f}")
# Детальный отчёт по классам
print("\nДетальный отчёт:")
print(classification_report(y_test, y_pred, target_names=['Класс 0', 'Класс 1']))