2017-06-12 4 views
0

私は、それらが陽性であるかどうかを判断する簡単な分類子を構築しています。これは私がtextblobを使って分類器を訓練する方法です。Textblobロジックヘルプ。 NaiveBayesClassifier

train = [ 
    'i love your website', 'pos', 
    'i really like your site', 'pos', 
    'i dont like your website', 'neg', 
    'i dislike your site', 'neg 
] 

cl.NaiveBayesClassifier(train) 

#im clasifying text from twitter using tweepy and it goes like this and 
stored into the databse and using the django to save me doing all the hassle 
of the backend 

class StdOutListener(StreamListener) 
def __init__(self) 
    self.raw_tweets = [] 
    self.raw_teets.append(jsin.loads(data) 
def on_data(self, data): 
    tweets = Htweets() # connection to the database 
    for x in self.raw_data: 
     tweets.tweet_text = x['text'] 

     cl.classify(x['text']) 

     if classify(x['text]) == 'pos' 
      tweets.verdict = 'pos' 
     elif classify(x['text]) == 'neg': 
      tweets.verdict = 'neg' 
     else: 
      tweets.verdict = 'normal' 

ロジックは非常に簡単に見えるが、私は1が正または負である分類器を訓練されたときには、データベースへのつぶやきと一緒に評決を保存しなければなりません。

しかし、これはケースではなく、私は多くの方法で論理を変更していて、まだ不十分です。問題は、ツイートが肯定的であるか否定的なものか、アルゴリズムがそれを認識しているかどうかです。

しかし、私はそれがそうでない場合、これをやっていない "正常な"を保存したい。分類器は正または負の2つのものしか認識しないと認識していますが、テキストがこのカテゴリに該当しないかどうかを確認する必要があります。

textblobを使用すると、これはどのように可能ですか?サンプルの代替ロジックと助言はすばらしいおかげです。

+0

:コンセプトに反映するように設定最小限のトレーニングで 例は、実際の使用のためにあなたは大きなトレーニングセットを使用しなければなりません。 –

+0

私はtextblobが3番目のクラスを受け入れると思っていません。それはあまりにも多くの値をアンパックエラー – johnobc

+1

を受け入れると、2つのバイナリクラシファイアを作成することができます。ニュートラルは、「感情は表現されていない」または「バランスのとれた感情」を表しています。したがって、同じインスタンスがそれぞれのクラシファイアによって正と負の両方に分類される可能性があります(中立か4番目のカテゴリかどうかを判断するまで) –

答えて

1

classifyは常に最大確率で答えを出すため、prob_classifyメソッドを使用してカテゴリラベルの確率分布を取得します。確率分布を観察し、適切な信頼限界を設定すると、適切な訓練セットで「中立」分類も開始されます。ニュートラル、例を挙げて:それは第三のクラスを作成することになる達成するための通常の方法

>>> train 
[('I love this sandwich.', 'pos'), ('this is an amazing place!', 'pos'), ('I feel very good about these beers.', 'pos'), ('this is my best work.', 'pos'), ('what an awesome view', 'pos'), ('I do not like this restaurant', 'neg'), ('I am tired of this stuff.', 'neg'), ("I can't deal with this", 'neg'), ('he is my sworn enemy!', 'neg'), ('my boss is horrible.', 'neg')] 
>>> from pprint import pprint 
>>> pprint(train) 
[('I love this sandwich.', 'pos'), 
('this is an amazing place!', 'pos'), 
('I feel very good about these beers.', 'pos'), 
('this is my best work.', 'pos'), 
('what an awesome view', 'pos'), 
('I do not like this restaurant', 'neg'), 
('I am tired of this stuff.', 'neg'), 
("I can't deal with this", 'neg'), 
('he is my sworn enemy!', 'neg'), 
('my boss is horrible.', 'neg')] 
>>> train2 = [('science is a subject','neu'),('this is horrible food','neg'),('glass has water','neu')] 
>>> train = train+train2 
>>> from textblob.classifiers import NaiveBayesClassifier 
>>> cl = NaiveBayesClassifier(train) 
>>> prob_dist = cl.prob_classify("I had a horrible day,I am tired") 
>>> (prob_dist.prob('pos'),prob_dist.prob('neg'),prob_dist.prob('neu')) 
(0.01085221171283812, 0.9746799258978173, 0.014467862389343378) 
>>> 
>>> prob_dist = cl.prob_classify("This is a subject") 
>>> (prob_dist.prob('pos'),prob_dist.prob('neg'),prob_dist.prob('neu')) 
(0.10789848368588585, 0.14908905046805337, 0.7430124658460614) 
関連する問題