Python:如何在多标签类的 SVM 文本分类器算法中找到准确度结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19629331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:13:48  来源:igfitidea点击:

Python : How to find Accuracy Result in SVM Text Classifier Algorithm for Multilabel Class

pythonmachine-learningsvmscikit-learnsvc

提问by user_az

I have used following set of code: And I need to check accuracy of X_train and X_test

我使用了以下代码集:我需要检查 X_train 和 X_test 的准确性

The following code works for me in my classification problem over multi-labeled class

以下代码在我的多标签类分类问题中对我有用

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier

X_train = np.array(["new york is a hell of a town",
                    "new york was originally dutch",
                    "the big apple is great",
                    "new york is also called the big apple",
                    "nyc is nice",
                    "people abbreviate new york city as nyc",
                    "the capital of great britain is london",
                    "london is in the uk",
                    "london is in england",
                    "london is in great britain",
                    "it rains a lot in london",
                    "london hosts the british museum",
                    "new york is great and so is london",
                    "i like london better than new york"])
y_train = [[0],[0],[0],[0]
            ,[0],[0],[1],[1]
            ,[1],[1],[1],[1]
            ,[2],[2]]
X_test = np.array(['nice day in nyc',
                   'the capital of great britain is london',
                   'i like london better than new york',
                   ])   
target_names = ['Class 1', 'Class 2','Class 3']

classifier = Pipeline([
    ('vectorizer', CountVectorizer(min_df=1,max_df=2)),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(LinearSVC()))])
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)
for item, labels in zip(X_test, predicted):
    print '%s => %s' % (item, ', '.join(target_names[x] for x in labels))

OUTPUT

输出

nice day in nyc => Class 1
the capital of great britain is london => Class 2
i like london better than new york => Class 3

I would like to check the accuracy between Training and Test Dataset. Score Function doesn't work for me, it shows an error stating that multilabel value can't accepted

我想检查训练和测试数据集之间的准确性。评分函数对我不起作用,它显示一个错误,指出不能接受多标签值

>>> classifier.score(X_train, X_test)

NotImplementedError:score is not supported for multilabel classifiers

NotImplementedError:多标签分类器不支持分数

Kindly help me get accuracy results for training and test data and choose an algorithm for our classification case.

请帮助我获得训练和测试数据的准确度结果,并为我们的分类案例选择一种算法。

采纳答案by mayhewsw

If you want to get an accuracy score for your test set, you'll need to create an answer key, which you can call y_test. You can't know if your predictions are correct unless you know the correct answers.

如果您想获得测试集的准确度分数,您需要创建一个答案键,您可以调用它y_test。除非您知道正确答案,否则您无法知道您的预测是否正确。

Once you have an answer key, you can get the accuracy. The method you want is sklearn.metrics.accuracy_score.

一旦你有了答案的关键,你就可以得到准确度。您想要的方法是sklearn.metrics.accuracy_score

I've written it out below:

我把它写在下面:

from sklearn.metrics import accuracy_score

# ... everything else the same ...

# create an answer key
# I hope this is correct!
y_test = [[1], [2], [3]]

# same as yours...
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)

# get the accuracy
print accuracy_score(y_test, predicted)

Also, sklearn has several other metrics besides accuracy. See them here: sklearn.metrics

此外,除了准确性之外,sklearn 还有其他几个指标。在此处查看它们:sklearn.metrics