使用scikit对多个类别进行分类 - 使用阈值



我正在阅读这篇文章:使用scikit-learn将分类为多个类别,这些类别可以满足您的需求。

在本练习中,我使用相同的数据集:

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 import preprocessing
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"],["london"],["london"],["london"],
            ["london"],["london"],["new york","london"],["new york","london"]]
X_test = np.array(['nice day in nyc',
               'welcome to london',
               'london is rainy',
               'it is raining in britian',
               'it is raining in britian and the big apple',
               'it is raining in britian and nyc',
               'hello welcome to new york. enjoy it here and london too'])
target_names = ['New York', 'London']

并以相同的方式处理数据:

lb = preprocessing.MultiLabelBinarizer()
Y = lb.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 = lb.inverse_transform(predicted)
for item, labels in zip(X_test, all_labels):
 print '%s => %s' % (item, ', '.join(labels))

这一切都有效,并给了我我想要的东西。但是,我真的很想设置一个手动阈值来确定将出现哪些标签。因此,例如,如果我将阈值设置为 70%,那么我希望所有概率高于 70% 的标签都显示为标签。如果(例如)伦敦有 69% 的概率,则不应显示。

关于我该如何做到这一点的任何想法?

我认为你需要的是predict_proba(doc)。

predict为每个样本提供分类器预测的类。另一方面,predict_proba将为您提供与每个类相关的概率,每个样本。

我建议在下面使用代码,请注意我没有运行它,因此它可能包含微不足道的错误,重点是举例说明如何做到这一点。它可能以某种方式有更好的解决方案

# Get a [n_samples, n_classes] array with each class probabilites for each samples
predicted_proba = classifier.predict_proba(X_test)
labels = []
threshold = 0.7
# Iterating over results
for probs in predicted_proba:
   labels.append([])
   # Iterating over class probabilities
   for i in range(len(probs)):
      if probs[i] >= threshold:
         # We add the class
         labels[-1].append(i)

希望对你有帮助

pltrdy

相关内容

  • 没有找到相关文章