如何使用烤宽面条计算F1-micro分数 [英] How to calculate F1-micro score using lasagne

查看:137
本文介绍了如何使用烤宽面条计算F1-micro分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import theano.tensor as T
import numpy as np
from nolearn.lasagne import NeuralNet

def multilabel_objective(predictions, targets):
    epsilon = np.float32(1.0e-6)
    one = np.float32(1.0)
    pred = T.clip(predictions, epsilon, one - epsilon)
    return -T.sum(targets * T.log(pred) + (one - targets) * T.log(one - pred), axis=1)

net = NeuralNet(
    # your other parameters here (layers, update, max_epochs...)
    # here are the one you're interested in:
    objective_loss_function=multilabel_objective,
    custom_score=("validation score", lambda x, y: np.mean(np.abs(x - y)))
)

我在线找到了此代码,并想对其进行测试.它确实起作用了,结果包括训练损失,测试损失,验证分数和时间等.

I found this code online and wanted to test it. It did work, the results include training loss, test loss, validation score and during time and so on.

但是我如何获得F1-micro分数?另外,如果在添加以下代码后尝试导入scikit-learn以计算F1:

But how can I get the F1-micro score? Also, if I was trying to import scikit-learn to calculate the F1 after adding the following code:

data = data.astype(np.float32) 
classes = classes.astype(np.float32)

net.fit(data, classes)

score = cross_validation.cross_val_score(net, data, classes, scoring='f1', cv=10)

print score

我收到此错误:

ValueError:无法处理multilabel-indicator和 连续多输出

ValueError: Can't handle mix of multilabel-indicator and continuous-multioutput

如何根据以上代码实现F1-micro计算?

How to implement F1-micro calculation based on above code?

推荐答案

假设测试集上的真实标签为y_true(形状:(n_samples, n_classes),仅由0和1组成),并且测试观察结果为X_test(形状:(n_samples, n_features)).

Suppose your true labels on the test set are y_true (shape: (n_samples, n_classes), composed only of 0s and 1s), and your test observations are X_test (shape: (n_samples, n_features)).

然后,您通过y_test = net.predict(X_test)在测试集上获得净预测值.

Then you get your net predicted values on the test set by y_test = net.predict(X_test).

如果要进行多类分类:

由于在网络中已将regression设置为False,因此它也应仅由0和1组成.

Since in your network you have set regression to False, this should be composed of 0s and 1s only, too.

您可以使用以下方法计算f1的微观平均得分:

You can compute the micro averaged f1 score with:

from sklearn.metrics import f1_score
f1_score(y_true, y_pred, average='micro')

用于说明此问题的小代码示例(使用伪数据,请使用实际的y_testy_true):

Small code sample to illustrate this (with dummy data, use your actual y_test and y_true):

from sklearn.metrics import f1_score
import numpy as np


y_true = np.array([[0, 0, 1], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 1, 0]])
y_pred = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1]])

t = f1_score(y_true, y_pred, average='micro')

如果要进行多标签分类:

您不是在输出0和1的矩阵,而是在输出概率的矩阵. y_pred [i,j]是观察值i属于类j的概率.

You are not outputting a matrix of 0 and 1, but a matrix of probabilities. y_pred[i, j] is the probability that observation i belongs to the class j.

您需要定义一个阈值,在该阈值以上您将说观察值属于给定的类别.然后,您可以对标签进行相应的属性设置,并与前面的情况一样进行操作.

You need to define a threshold value, above which you will say an observation belongs to a given class. Then you can attribute labels accordingly and proceed just the same as in the previous case.

thresh = 0.8  # choose your own value 
y_test_binary = np.where(y_test > thresh, 1, 0) 
# creates an array with 1 where y_test>thresh, 0 elsewhere

f1_score(y_true, y_pred_binary, average='micro')

这篇关于如何使用烤宽面条计算F1-micro分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆