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

时间:2017-01-31 14:39:07

标签: python machine-learning scikit-learn

我正在阅读这篇文章:use scikit-learn to classify into multiple categories我正在寻找哪种方式。

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

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%的概率它不应该显示。

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

1 个答案:

答案 0 :(得分:0)

我认为您需要的是predict_probadoc)。

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