多标签文本分类的准确性

时间:2018-12-08 11:25:45

标签: python scikit-learn svm naivebayes multilabel-classification

如何找到该程序的准确性,f1得分,准确性和召回率。 我想为此程序计算混淆矩阵,但使用此函数找不到这些矩阵:

metrics.accuracy_score(y_test, predicted)
print(classification_report(y_test,predictions)))


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
from sklearn.preprocessing import MultiLabelBinarizer

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_text = [["new york"],["new york"],["new york"],["new york"],["new york"],
            ["new york"],["london"],["berlin"],["london"],["london"],
            ["london"],["london"],["new york","london","berlin"],["new york","london"]]

print y_train_text[13:]

X_test = np.array(['it is raining in britian and nyc'])

target_names = ['New York', 'London']

mlb = MultiLabelBinarizer()
Y = mlb.fit_transform(y_train_text)

classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC()))])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)
all_labels = mlb.inverse_transform(predicted)


for item, labels in zip(X_test, all_labels):
print('{0} => {1}'.format(item, ', '.join(labels)))

0 个答案:

没有答案
相关问题