python - 使用我自己的语料库了解 scikit-learn 的 accuracy_score?

标签 python machine-learning nlp scikit-learn

假设我已经准备好使用 scikit learn 和 SVC 进行一些文本分类。首先我将语料库矢量化,将数据分成测试集和训练集,然后将标签设置到训练集中。现在我想获得分类的准确性。

来自documentation我阅读了以下内容:

>>> import numpy as np
>>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> accuracy_score(y_true, y_pred)
0.5
>>> accuracy_score(y_true, y_pred, normalize=False)
2

问题是我不明白什么是:y_pred = [0, 2, 1, 3]y_true = [0, 1, 2, 3] 和一旦我对自己的语料库进行分类测试集,我如何才能“达到”或获得这些值。有人可以帮我解决这个问题吗?

让我们举个例子:

训练数据:

Pošto je EULEX obećao da će obaviti istragu o prošlosedmičnom izbijanju nasilja na sjeveru Kosova, taj incident predstavlja još jedan ispit kapaciteta misije da doprinese jačanju vladavine prava.
De todas as provações que teve de suplantar ao longo da vida, qual foi a mais difícil? O início. Qualquer começo apresenta dificuldades que parecem intransponíveis. Mas tive sempre a minha mãe do meu lado. Foi ela quem me ajudou a encontrar forças para enfrentar as situações mais decepcionantes, negativas, as que me punham mesmo furiosa.
Al parecer, Andrea Guasch pone que una relación a distancia es muy difícil de llevar como excusa. Algo con lo que, por lo visto, Alex Lequio no está nada de acuerdo. ¿O es que más bien ya ha conseguido la fama que andaba buscando?
Vo väčšine golfových rezortov ide o veľký komplex niekoľkých ihrísk blízko pri sebe spojených s hotelmi a ďalšími možnosťami trávenia voľného času – nie vždy sú manželky či deti nadšenými golfistami, a tak potrebujú iný druh vyžitia. Zaujímavé kombinácie ponúkajú aj rakúske, švajčiarske či talianske Alpy, kde sa dá v zime lyžovať a v lete hrať golf pod vysokými alpskými končiarmi.

测试数据:

Por ello, ha insistido en que Europa tiene que darle un toque de atención porque Portugal esta incumpliendo la directiva del establecimiento del peaje
Estima-se que o mercado homossexual só na Cidade do México movimente cerca de oito mil milhões de dólares, aproximadamente seis mil milhões de euros


import codecs, re, time
from itertools import chain

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

trainfile = 'train.txt'
testfile = 'test.txt'

# Vectorizing data.
train = []
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['bs','pt','es','sr']

# Training NB
mnb = MultinomialNB()
mnb.fit(trainset, tags)

# Tagging the documents
codecs.open(testfile,'r','utf8')
testset = word_vectorizer.transform(codecs.open(testfile,'r','utf8'))
results = mnb.predict(testset)

print results

最佳答案

希望对您有所帮助。你问:

The problem is i dont understand what are: y_pred = [0, 2, 1, 3] and y_true = [0, 1, 2, 3] and how can i "reach" or obtain these values once i Classified test set of my own corpus. Could anybody help me with this issue?.

答案:如您所知,分类器应该将数据分类到不同的类别。在上面的例子中,假设数据有四个不同的类别,分别用标签 0、1、2 和 3 指定。因此,如果我们的数据是关于对单色图像中的颜色进行分类,则标签将表示如下内容:蓝色,红色、黄色和绿色。上述示例显示的另一个问题是数据中只有四个样本。例如,他们只有四张图片,y_true 显示了他们的真实标签(或者我们称之为 groundtruth)。 y_pred 显示分类器的预测。现在,如果我们比较两个列表(如果它们相同),我们的准确度为 100%,但是,在这种情况下,您会看到预测标签中的两个标签与其基本事实不匹配。

现在,在您的示例代码中,您已经编写了:

tags = ['SPAM','HAM','another_class']

这就像我上面解释的那样,意味着首先,您的数据由 3 个不同的类组成;其次,它表明您的数据仅包含 3 个样本(这可能不是您真正想要的)。因此,此列表的长度应等于训练数据中的样本数。如果您还有其他问题,请告诉我。

关于python - 使用我自己的语料库了解 scikit-learn 的 accuracy_score?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558629/

相关文章:

python - 模块 'yaml' 没有属性 'FullLoader'

python - scikit-learn 的 DecisionTreeRegressor 是否进行真正的多输出回归?

python - 使用 scikit-learn 训练模型的更快方法

python - 依存解析器不工作 - 斯坦福核心 NLP

python - 可以将矩阵作为 Keras 嵌入层的输入吗?

Python 使用外键对列表进行排序

python - 如何在两个模型日期字段之间设置时间增量

python - 从文本文件中提取与输入单词最相似的前 N ​​个单词

matlab - LSTM Matlab中 `MiniBatchSize`参数的含义是什么

sql-server - 在位置索引中查找二元组