73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
"""Тесты метрик и статистических критериев."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
from stylometry.evaluation import (
|
|
bootstrap_ci,
|
|
classification_metrics,
|
|
confusion_matrix_frame,
|
|
per_class_report,
|
|
)
|
|
from stylometry.stats import (
|
|
bootstrap_difference,
|
|
holm_bonferroni,
|
|
mcnemar_exact,
|
|
wilcoxon_signed_rank,
|
|
)
|
|
|
|
|
|
def test_classification_metrics_perfect() -> None:
|
|
y_true = np.array(["a", "b", "c", "a", "b", "c"])
|
|
y_pred = y_true.copy()
|
|
metrics = classification_metrics(y_true, y_pred)
|
|
assert metrics["accuracy"] == 1.0
|
|
assert metrics["macro_f1"] == 1.0
|
|
assert metrics["balanced_accuracy"] == 1.0
|
|
|
|
|
|
def test_confusion_matrix_and_report() -> None:
|
|
y_true = np.array(["a", "a", "b", "b"])
|
|
y_pred = np.array(["a", "b", "b", "b"])
|
|
matrix = confusion_matrix_frame(y_true, y_pred, ["a", "b"])
|
|
assert matrix.loc["a", "a"] == 1
|
|
report = per_class_report(y_true, y_pred, ["a", "b"])
|
|
assert {"precision", "recall", "f1", "support"} <= set(report.columns)
|
|
|
|
|
|
def test_bootstrap_ci() -> None:
|
|
y_true = np.array(["a", "b", "a", "b", "a", "b"])
|
|
y_pred = np.array(["a", "b", "a", "a", "a", "b"])
|
|
interval = bootstrap_ci(y_true, y_pred, ["a", "b"], n_boot=50, seed=1)
|
|
assert interval["lower"] <= interval["upper"]
|
|
|
|
|
|
def test_mcnemar() -> None:
|
|
y_true = np.array([1, 1, 0, 0, 1, 0])
|
|
pred_a = np.array([1, 1, 0, 0, 1, 0])
|
|
pred_b = np.array([1, 0, 0, 1, 1, 0])
|
|
result = mcnemar_exact(y_true, pred_a, pred_b)
|
|
assert result["b"] >= 0 and result["c"] >= 0
|
|
assert 0 <= result["p_value"] <= 1
|
|
|
|
|
|
def test_holm() -> None:
|
|
adjusted = holm_bonferroni([0.01, 0.02, 0.03])
|
|
assert len(adjusted) == 3
|
|
assert all(0 <= value <= 1 for value in adjusted)
|
|
|
|
|
|
def test_wilcoxon() -> None:
|
|
result = wilcoxon_signed_rank([0.5, 0.6, 0.7], [0.4, 0.6, 0.5])
|
|
assert 0 <= result["p_value"] <= 1
|
|
|
|
|
|
def test_bootstrap_difference() -> None:
|
|
y_true = np.array([0, 1, 2, 0, 1, 2])
|
|
pred_a = np.array([0, 1, 2, 0, 1, 2])
|
|
pred_b = np.array([0, 1, 1, 0, 1, 1])
|
|
result = bootstrap_difference(y_true, pred_a, pred_b, labels=[0, 1, 2], n_boot=50)
|
|
assert result["lower"] <= result["upper"]
|