2013-03-07 96 views
5

使用mahout我能夠分類數據的情緒。但是我陷入了一個混淆矩陣。Mahout的情緒分析

我使用象夫0.7樸素貝葉斯算法進行分類鳴叫的情緒。 我使用trainnbtestnb樸素貝葉斯分類器來訓練分類器,並將推文的情感分爲「正面」,「負面」或「中性」。

樣品陽性訓練集

 'positive','i love my i phone' 
     'positive' , it's pleasure to have i phone' 

的負 和中性同樣地,我已經準備訓練樣本,它是一個巨大的數據集。

我提供的樣本測試數據tweets沒有包含情緒。

'it is nice model' 
    'simply fantastic ' 

我能夠運行mahout分類算法,它將分類實例的輸出作爲混淆矩陣輸出。

下一步,我需要找出哪些鳴叫都出現了積極的情緒和爲負。 使用分類的預期輸出:用情緒標記文本。

 'negative','very bad btr life time' 
     'positive' , 'i phone has excellent design features' 

在mahout中,我需要實現哪種算法以獲得上述格式的輸出。或者需要任何自定義源實現。

要顯示的數據「好心」提示我說,阿帕奇象夫提供的算法,這將是適合我的Twitter數據的情感分析。

回答

3

一般到一些文字,您需要用不同的先驗(正和你的情況爲負),然後運行樸素貝葉斯分類只是選擇導致更大的價值之一。

This excerpt從亨利馬烏本書有一些例子。參見清單2:

Parameters p = new Parameters(); 
p.set("basePath", modelDir.getCanonicalPath());9 
Datastore ds = new InMemoryBayesDatastore(p); 
Algorithm a = new BayesAlgorithm(); 
ClassifierContext ctx = new ClassifierContext(a,ds); 
ctx.initialize(); 

.... 

ClassifierResult result = ctx.classifyDocument(tokens, defaultCategory); 

此處的結果應該包含「正」或「負」標籤。

1

我不知道我將能夠幫助您全面,但我希望我能夠給你一些切入點。一般來說,我對你的建議是下載Mahout的源代碼,看看實例和目標類是如何實現的。這不是那麼容易,但你應該準備好Mahout沒有簡單的進入門。但一旦你進入他們的學習曲線將會很快。

首先,它取決於你使用的Mahout版本。我自己使用0.7,所以我的解釋是關於0.7。

public void classify(String modelLocation, RawEntry unclassifiedInstanceRaw) throws IOException { 

    Configuration conf = new Configuration(); 

    NaiveBayesModel model = NaiveBayesModel.materialize(new Path(modelLocation), conf); 
    AbstractNaiveBayesClassifier classifier = new StandardNaiveBayesClassifier(model); 

    String unclassifiedInstanceFeatures = RawEntry.toNaiveBayesTrainingFormat(unclassifiedInstanceRaw); 

    FeatureVectorEncoder vectorEncoder = new AdaptiveWordValueEncoder("features"); 
    vectorEncoder.setProbes(1); // my features vectors are tiny 

    Vector unclassifiedInstanceVector = new RandomAccessSparseVector(unclassifiedInstanceFeatures.split(" ").length()); 

    for (String feature: unclassifiedInstanceFeatures) { 
     vectorEncoder.addToVector(feature, unclassifiedInstanceVector); 
    } 

    Vector classificationResult = classifier.classifyFull(unclassifiedInstanceVector); 

    System.out.println(classificationResult.asFormatString()); 

} 

這裏會發生什麼:

1)首先,你加載你做trainnb得到了模型。調用trainnb時,使用-o參數指定保存的模型。型號是.bin文件。

2)StandardNaiveBayesClassifier使用模型

3)RawEntry是我的自定義類,它只是在我的數據的原始字符串的包裝創建。 toNaiveBayesTrainingFormar需要字符串我想分類,根據我的需要從它移除噪音,並簡單地返回一串特徵'word1 word2 word3 word4'。所以,我的未分類的原始字符串被轉換成適用於分類的格式。

4)現在需要被編碼爲亨利馬烏的載體,因爲分類輸入僅在矢量

5)傳遞載體分類的特徵字符串 - 魔術。

這是第一部分。現在,分類器會返回Vector,其中包含類(在您的情況下爲情緒)和概率。你想要特定的輸出。最容易實現的(但我認爲不是最有效的和時尚的)會是下一步:

1)創建映射精簡工作,這能看穿你想

2分類的所有數據)對於每個比如你調用分類方法(不要忘記做一些改變不是創造StandardNaiveBayesClassifier爲每個實例)

3)有可以輸出數據的任何格式,你在你的地圖嘩嘩減少職業分類結果向量

4)這裏有用的設置是jC.set(「mapreduce.textoutputformat.separator」, 「」); jC是JobConf。這允許您從mapreduce作業中爲輸出文件選擇分隔符。在你的情況下,這是「,」。

同樣,這一切都適用於Mahout 0.7。沒有保證它會爲你工作。它雖然爲我工作。

總的來說,我從來沒有從命令行與Mahout一起工作,對我來說,Mahout是來自Java的。