2016-12-18 4 views
0

「トランプ」という言葉を含むつぶやきについて感情分析をしています。私の分類器が陽性クラスを予測しないのはなぜですか?

Date SentimentText Sentiment 
Mon Nov 28 23:24:12 +0000 2016 "@HillaryClinton Go ahead with your hypocritical recount. It's fun to watch Trump squirm." 0 
Mon Nov 28 23:39:06 +0000 2016 @SenSchumer & @SenGillibrand - Demand Trump rescind Steve Bannon's appointment. @MoveOn 0 
Mon Nov 28 23:30:34 +0000 2016 Democrats Demand Trump's Tax Returns And An Investigation Into His Conflicts Of Interest via @politicususa 0 
Mon Nov 28 23:54:43 +0000 2016 "Oh my god, how has this only been one day?" [email protected] on covering a day on the Trump Trail #girlsonthebus @gupolitics 0 
Mon Nov 28 23:18:16 +0000 2016 People are mad at GiGi for impersonating Melania Trump, saying "it's rude to bully and immigrant" OH?! THE FUCKING IRONY 0 
Mon Nov 28 23:50:10 +0000 2016 @dosdelimas @FoxNews mt @resnikoff For those who don't understand why Trump would lie about voter fraud .. 0 
Mon Nov 28 23:29:29 +0000 2016 @tanveerali Yo! Do you mind if I steal your awesome electoral map (giving credit where credit is due)? 1 
Mon Nov 28 23:19:39 +0000 2016 "Historic," as in lower 1/3 of all EV results in American History 1 
Mon Nov 28 23:41:40 +0000 2016 i thought this was gonna say trump before i opened it 0 
Mon Nov 28 23:13:31 +0000 2016 Hold on wait, I voted for trump is the new racial slur now? im dead 1 
Mon Nov 28 23:22:01 +0000 2016 O.K., well, if a mass of stuff was then taught, it was set up for. #SubhumanCheeto #NMP 0 
Mon Nov 28 23:44:13 +0000 2016 Woman goes on racist, pro-Trump tirade in Michaels store over $1 bag Trumpmerica ladies & gents. 0 

私は、ユーザーがトランプや彼について肯定掲載何かをサポートしているかどうかに基づいて、ツイートを標識してみました:私は手動で最初の13回の観測を、ここで、最初の200件のツイートをラベル付けされています。ここで、正確に正のラベルを分類するために失敗し

import numpy as np 
import pandas as pd 
import csv 
from sklearn import linear_model, naive_bayes 
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer 
from sklearn.preprocessing import FunctionTransformer 
from sklearn.pipeline import Pipeline 
from sklearn.metrics import precision_score 
from sklearn.metrics import recall_score 
from sklearn.metrics import f1_score 
from sklearn.base import TransformerMixin 
from sklearn import cross_validation 



logistic_clf = Pipeline([('vect', CountVectorizer(ngram_range=(1, 2))), 
        ('ft_vec', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), 
        ('tfidf', TfidfTransformer()), 
        ('ft_tfid', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), 
        ('clf', linear_model.LogisticRegression(penalty='l2',solver='lbfgs',max_iter=1000, multi_class='ovr',warm_start=True)), 
        ]) 

gnb_clf = Pipeline([('vect', CountVectorizer()), 
        ('ft_vec', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), 
        ('tfidf', TfidfTransformer()), 
        ('ft_tfid', FunctionTransformer(lambda x: x.todense(), accept_sparse=True)), 
        ('clf', naive_bayes.GaussianNB()), 
        ]) 


import csv 
from pandas import * 

df = read_excel('trump_labeled.xlsx') 

#Collect the output in y variable 

y = df['Sentiment'] 
X = df['SentimentText'] 
from sklearn.cross_validation import train_test_split 
#cross validation 
X_train, X_test,y_train, y_test = train_test_split(X,y,test_size=0.25, random_state=42) 
X_train = np.array(X_train) 
y_train = np.array(y_train) 
X_test = np.array(X_test) 
y_test = np.array(y_test) 



log_clf = logistic_clf.fit(X_train, y_train) 
gnb_clf = gnb_clf.fit(X_train, y_train) 

log_predicted = logistic_clf.predict(X_test) # predict labels for test data with logistic regression classifier 
gnb_predicted = gnb_clf.predict(X_test) # predict labels for test data with naive bayes classifier 

# PRINT SOME RESULTS FOR THE DATASETS PART 
print("\nDATASET RESULTS") 
print('\nLogistic Regression Results:\n\tNegative tweets: %.2f\n\tPositive tweets: %.2f' %(np.mean(log_predicted == 0), np.mean(log_predicted == 1))) 
print('\tAccuracy: %.2f'% (np.mean(log_predicted == y_test))) 
print('\tPositive Precision: %.2f' %(precision_score(y_test, log_predicted,pos_label=1))) 
print('\tPositive Recall: %.2f' %(recall_score(y_test, log_predicted,pos_label=1))) 
print('\tPositive F-measure: %.2f' %(f1_score(y_test, log_predicted,pos_label=1))) 
print('\tNegative Precision: %.2f' %(precision_score(y_test, log_predicted,pos_label=0))) 
print('\tNegative Recall: %.2f' %(recall_score(y_test, log_predicted,pos_label=0))) 
print('\tNegative F-measure: %.2f' %(f1_score(y_test, log_predicted,pos_label=0))) 

が、これは生成され、次の結果

DATASET RESULTS 

Logistic Regression Results: 
     Negative tweets: 1.00 
     Positive tweets: 0.00 
     Accuracy: 0.72 
     Positive Precision: 0.00 
     Positive Recall: 0.00 
     Positive F-measure: 0.00 
     Negative Precision: 0.72 
     Negative Recall: 1.00 
     Negative F-measure: 0.84 
C:\Users\My\Anaconda2\lib\sitepackages\sklearn\metrics\classification.py:1074: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 due to no predicted samples.'precision', 'predicted', average, warn_for) 
C:\Users\My\Anaconda2\lib\sitepackages\sklearn\metrics\classification.py:1074: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.'precision', 'predicted', average, warn_for) 

マイ分類(ロジスティック回帰やナイーブベイズ): これは私がこれまで使っていたコードです正の値= 1となり、評価メトリックが定義されていません。 200個のつぶやきのうち、43個が肯定的でしたが、私の分類器は200個すべてを否定的に分類しています。どうすればこの問題を解決できますか?私はまだデータを前処理していないことに注意してください。だから、私はまだURLをトークンのURLに変換する必要があります。空白スペースを考慮する必要があります。なぜなら、まだ私のつぶやきを前処理していないからですか?あるいは、私が手作業でつぶやきを付けたように、そのうちのいくつかは、彼らが肯定的であるか否かを判断することが難しかった。私はトランプの前のつぶやきを検索しようとしましたが、運がありませんでした。

また、私はL2正則化の会計処理に気付いたし、ロジスティック回帰のBFGS最適化メソッドは私の精度を変えることはありません。

+0

ネガティブなつぶやき:1.00正のつぶやき:0.00、それは私がwasntすることについて申し訳ありません。あなたが100%、0%を意味することに気づきます。 pos |ネガ。 – putonspectacles

+0

そうだが、どうしてそれはどんなつぶやきにもプラスにラベル付けすることができないのだろうか? 200個のつぶやきの – M3105

+0

、そのうち43個が陽性であった – M3105

答えて

0

私の推測では、この問題とこれらの分類子のデータセットが小さすぎるということです。状況を悪化させるために、データセットは強く不均衡になっています。したがって、これらの分類子と損失の選択(クロスエントロピーを使用している場合)では、このシナリオで支配的なクラスを予測するために最小のペナルティが発生します。

私の提案は、より多くのデータを取得することです(この場合にはより多くのツイートにラベルを付ける)

+0

あなたの答えに感謝します。また、私は半監督のアプローチからこれを攻撃できると思いますか? Naive Bayesとロジスティック回帰分類子を使って私のモデルを訓練し、テストし、評価するためにk = 2のkmeansを実行しますか? – M3105

0

私はあなたがここで起こって二つのものを持っていると思います。まず、上記のヒットのように、あなたのツイートをトレーニングするあなたのコーパスは、特に多くのセンチメント分析コードが使用されている単語をベクトル化したいと考えると、小さすぎます。これらのベクトルは非常に大きく、非常にまばらです。トレーニング用のラベル付きつぶやきの数が、トレーニングしている単語の数と同程度でない場合、あなたは不都合な解決策を取ります。

良いニュースは、すでにラベルが貼られているTwitter(少なくとも英語)のためのいくつかのまともなトレーニングセットがあることです。 Sentiment140にはいくつかの参照があります。

悪い知らせは、テキストを大幅にクリーンアップすることなく、感情分析を試みることはかなり悪い考えです。これは、ツイートを単語に分解して(リンク、絵文字、ハッシュタグなどを削除した後)、ストップワードを削除し、データをハッシュするか何らかの方法でベクトル化するかどうかを決定することを意味します。あなたがそれらのことをするまで、あなたの訓練セットに入れているごくわずかな言葉は、騒音で失われます。

+0

お返事ありがとうございます。私はもともとSentiment140を使っていましたが、彼らは全く異なるドメインを持っていることに気付きました。彼らのつぶやきはかなり無作為でしたが、私のつぶやきは回転するトランプの問題に特有です。私は前処理を終え、否定処理などの機能を追加しました。それによって私は2%の精度を上げることができましたが、現在は98%のマイナスと2%のプラスを分類しています。いくつのつぶやきを付ける必要がありますか? 500-1000のラベルされたつぶやきは十分でしょうか? – M3105

+0

トンとトンの言葉が書かれている文書を話していたら、より小さなトレーニングセットで逃げることができます。しかし、大きな文書には、感情スコアに貢献するために使用される多くの単語が含まれているためです。つぶやきは140文字しかないので、各ツイートには単語の数が非常に限られています。そういうわけで、たくさんのつぶやきを見て訓練する必要があります。より大きな、より一般的なものに比べて、トピックごとの、より小さいトレーニングセットを使用することに注意してください。結局のところ、これらの非常に大きなベクトルにはまだポピュレートする必要があります。 –

+0

また、[リンク](http://thinknook.com/twitter-sentiment-analysis-training-corpus-dataset-2012-09-22/)をチェックしてください。このページには、つぶやきの1.6Mのコーパスがあります。それは私のTwitterの感情分析で使っているものです。 –

関連する問題