Example #1
0
def test_classification_metrics():
    logger.info("Testeando métricas de evaluación para clasificación...")
    # Ejemplos para testear evaluación sobre 3 clases
    predict = [0, 1, 0, 2, 2, 1]
    labels = [0, 1, 1, 2, 1, 0]

    metrics = ClassificationMetrics(zip(predict, labels), 3)

    # Generales
    assert metrics.accuracy() == 0.5
    assert (metrics.confusion_matrix() == np.array([[1, 1, 0], [1, 1, 1], [0, 0, 1]])).all()
    # Por etiqueta
    assert metrics.precision(label=0) == metrics.precision(label=1) == metrics.precision(label=2) == 0.5
    assert (metrics.recall(label=0) == 0.5 and
            metrics.recall(label=1) == 0.3333333333333333 and
            metrics.recall(label=2) == 1.0)
    # Micro and macro
    assert metrics.precision(macro=True) == metrics.precision(macro=False) == 0.5
    assert (metrics.recall(macro=True) == 0.611111111111111 and
            metrics.recall(macro=False) == 0.5)

    # F-measure variando Beta
    assert metrics.f_measure(beta=1) == 0.5499999999999999  # F1-score, igual ponderación
    assert metrics.f_measure(beta=0.5) == 0.5188679245283019  # F0.5 score, prioriza precision en lugar de recall
    assert metrics.f_measure(beta=2) == 0.5851063829787233  # F2-score, prioriza recall en lugar de precision
    logger.info("OK")
Example #2
0
neural_net = NeuralNetwork(net_params)

logger.info("Entrenando red neuronal ...")
hits_valid = neural_net.fit(train,
                            valid,
                            valid_iters=1,
                            mini_batch=10,
                            parallelism=2,
                            stops=global_stops,
                            optimizer_params=optimizer_params,
                            keep_best=True,
                            reproducible=True)
hits_test, predict = neural_net.evaluate(test, predictions=True)

logger.info("Hits en test: %12.11f", hits_test)

# --4) Evaluacion de desempeño

logger.info("Metricas de evaluación en clasificacion: ")
labels = map(lambda lp: float(lp.label), test.collect())
metrics = ClassificationMetrics(zip(predict, labels), 3)
logger.info("F1-Score: %12.11f", metrics.f_measure(beta=1))
logger.info("Precision: %12.11f", metrics.precision())
logger.info("Recall: %12.11f", metrics.recall())
logger.info("Matriz de confusion: %s",
            os.linesep + str(metrics.confusion_matrix()))

# --5) Ploteos

plot_fitting(neural_net)
Example #3
0
# -- 3) Construcción y ajuste de red neuronal

neural_net = NeuralNetwork(net_params)

logger.info("Entrenando red neuronal ...")
hits_valid = neural_net.fit(train, valid, valid_iters=1, mini_batch=10, parallelism=2, stops=global_stops,
                            optimizer_params=optimizer_params, keep_best=True, reproducible=True)
hits_test, predict = neural_net.evaluate(test, predictions=True)

logger.info("Hits en test: %12.11f", hits_test)

# --4) Evaluacion de desempeño

logger.info("Metricas de evaluación en clasificacion: ")
labels = map(lambda lp: float(lp.label), test.collect())
metrics = ClassificationMetrics(zip(predict, labels), 3)
logger.info("F1-Score: %12.11f", metrics.f_measure(beta=1))
logger.info("Precision: %12.11f", metrics.precision())
logger.info("Recall: %12.11f", metrics.recall())
logger.info("Matriz de confusion: %s", os.linesep+str(metrics.confusion_matrix()))

# --5) Ploteos

plot_fitting(neural_net)