2017-01-31 10 views
0

私はこの投稿を読んでいました:use scikit-learn to classify into multiple categories私が探していることはなんですか?scikitで複数のカテゴリを分類する - 閾値を使用して

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%の確率がある場合、それは示されてはならない。

どうすればいいですか?

+2

質問はここにトピックオフになっているのに役立ちます願っています。この質問は[SO]のトピックにあるはずです。あなたが待っているなら、私たちはそれをそこに移そうとします。 – gung

答えて

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

関連する問題