Exemple #1
0
def test_classification_metrics():
    """Tests imbDRL.metrics.classification_metrics."""
    y_true = [1, 1, 1, 1, 1, 1]
    y_pred = [1, 1, 1, 0, 0, 0]

    with pytest.raises(ValueError) as exc:
        metrics.classification_metrics(1, y_pred)
    assert "`y_true` must be of type" in str(exc.value)

    with pytest.raises(ValueError) as exc:
        metrics.classification_metrics(y_true, -1)
    assert "`y_pred` must be of type" in str(exc.value)

    with pytest.raises(ValueError) as exc:
        metrics.classification_metrics(y_true, y_pred + [1])
    assert "must be of same length" in str(exc.value)

    stats = metrics.classification_metrics(y_true, y_pred)
    approx = rounded_dict(stats)
    assert approx == {"Gmean": 0.0, "F1": 0.666667, "Precision": 1.0, "Recall": 0.5, "TP": 3, "TN": 0, "FP": 0, "FN": 3}

    y_true = [1, 1, 1, 1, 1, 1]
    y_pred = [0, 0, 0, 0, 0, 0]
    stats = metrics.classification_metrics(y_true, y_pred)
    approx = rounded_dict(stats)
    assert approx == {"Gmean": 0.0, "F1": 0.0, "Precision": 0.0, "Recall": 0.0, "TP": 0, "TN": 0, "FP": 0, "FN": 6}

    y_true = [0, 0, 0, 0, 0, 0]
    y_pred = [1, 1, 1, 1, 1, 1]
    stats = metrics.classification_metrics(y_true, y_pred)
    approx = rounded_dict(stats)
    assert approx == {"Gmean": 0.0, "F1": 0.0, "Precision": 0.0, "Recall": 0.0, "TP": 0, "TN": 0, "FP": 6, "FN": 0}
Exemple #2
0
)  # Normalization should happen after splitting train and test sets

X_train, X_test, y_train, y_test = train_test_split(df.to_numpy(),
                                                    y,
                                                    stratify=y,
                                                    test_size=0.2)
X_train, y_train, X_test, y_test, X_val, y_val = get_train_test_val(
    X_train, y_train, X_test, y_test, min_class, maj_class, val_frac=0.2)

model = TrainDDQN(episodes,
                  warmup_steps,
                  learning_rate,
                  gamma,
                  min_epsilon,
                  decay_episodes,
                  target_update_period=target_update_period,
                  target_update_tau=target_update_tau,
                  batch_size=batch_size,
                  collect_steps_per_episode=collect_steps_per_episode,
                  memory_length=memory_length,
                  collect_every=collect_every,
                  n_step_update=n_step_update)

model.compile_model(X_train, y_train, layers)
model.q_net.summary()
model.train(X_val, y_val, "F1")

stats = model.evaluate(X_test, y_test, X_train, y_train)
print(rounded_dict(stats))
# {'Gmean': 0.824172, 'F1': 0.781395, 'Precision': 0.730435, 'Recall': 0.84, 'TP': 84, 'TN': 131, 'FP': 31, 'FN': 16}
Exemple #3
0
def test_rounded_dict():
    """Tests imbDRL.utils.rounded_dict."""
    d = {"A": 10.123456789, "B": 100}
    assert utils.rounded_dict(d) == {"A": 10.123457, "B": 100}