我有一张单子,单子是对应于单词句子的。

X = [
        ['John','has','house'],
        ['Mary','works','at','home']
    ]

你可以把每句话当作训练的样本。我的模型atagger,用一些标签标记每个单词:
y = [
        ['proper','verb','noun'],
        ['proper','verb','prep','noun']
    ]

我想用网格搜索我的标记符,以确保在单词级别上标记的准确性:
search = GridSearchCV(tagger, parameters, cv=10, scoring='accuracy')
search.fit(X, y)

然而,accuracy_score()方法抱怨说
{ValueError}您似乎正在使用旧的多标签数据表示。序列不再受支持;请改用二进制数组或稀疏矩阵-MultiLabelBinarizer转换器可以转换为此格式。
当列表列表y被展平时,不会发生这种情况,例如:
> y_pred = ['proper','verb','noun', 'proper','verb','prep','noun']
> y_true = ['proper','verb','noun', 'proper','verb','prep','noun']
> accuracy_score(y_pred, y_true)
1.0

我仍然希望我的标记器(Keras model)预测一个列表列表以保留文本结构(句子、单词),但我希望scorer在单词级别进行评估。
我怎样才能优雅地解决它呢?

最佳答案

我发现一个可能的解决方案是构建自定义评分函数:

from sklearn.metrics import make_scorer, accuracy_score


def flatten(l):
    return [item for sublist in l for item in sublist ]

def word_accuracy_score(y, y_pred):
    y = flatten(y)
    y_pred = flatten(y_pred)
    return accuracy_score(y, y_pred)

并传递给GridSearchCV
scorer = make_scorer(word_accuracy_score)
search = GridSearchCV(tagger, parameters, cv=10, scoring=scorer)

08-24 20:58