[英]How to create our own model for sentiment analysis using naive bayes classifier in mahout?
[英]Mahout for sentiment analysis
使用mahout我能夠分類數據的情緒。 但我陷入困惑矩陣。
我使用mahout 0.7朴素貝葉斯算法來分類推文的情緒。 我使用trainnb
和testnb
朴素貝葉斯分類器來訓練分類器並testnb
文的情緒分類為“正面”,“負面”或“中立”。
樣本積極訓練集
'positive','i love my i phone'
'positive' , it's pleasure to have i phone'
同樣地,我准備了負面和中性的訓練樣本,這是一個龐大的數據集。
我提供的樣本測試數據推文沒有包含情緒。
'it is nice model'
'simply fantastic '
我能夠運行mahout分類算法,並將分類實例的輸出作為混淆矩陣。
下一步我需要找出哪些推文表現出積極的情緒,哪些是否定的。 使用分類的預期輸出:用情緒標記文本。
'negative','very bad btr life time'
'positive' , 'i phone has excellent design features'
在mahout我需要實現哪種算法以獲得上述格式的輸出。 或者需要任何自定義源實現。
為了“善意”顯示數據,建議使用apache mahout提供的算法,這將適合我的Twitter數據情緒分析。
一般來說,要對一些文本進行分類,你需要運行具有不同先驗的朴素貝葉斯(在你的情況下為正面和負面)然后只選擇一個導致更大價值的文本。
Mahout書中的這段摘錄有一些例子。 參見清單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);
這里的結果應該是“正面”或“負面”標簽。
我不確定我是否能完全幫助你,但我希望我能給你一些切入點。 一般來說,我對你的建議是下載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)現在需要將一系列特征編碼為Mahout的Vector,因為分類器輸入僅在Vector中
5)將矢量傳遞給分類器 - 魔法。
這是第一部分。 現在,分類器返回包含具有概率的類(在您的情況下為情緒)的Vector。 你想要特定的輸出。 最直接的實現(但我認為不是最有效和最時尚)將是下一步:
1)您創建地圖縮減作業,該作業將遍歷您要分類的所有數據
2)對於每個實例,您調用classify方法(不要忘記進行一些更改,不要為每個實例創建StandardNaiveBayesClassifier)
3)具有分類結果向量,您可以以地圖縮減作業中的任何格式輸出數據
4)這里有用的設置是jC.set(“mapreduce.textoutputformat.separator”,“”); 其中jC是JobConf。 這允許您從mapreduce作業中為輸出文件選擇分隔符。 在你的情況下,這是“,”。
同樣,這一切都適用於Mahout 0.7。 不保證它會對你有用。 雖然它對我有用。
一般來說,我從未與命令行中的Mahout合作,而對於我來說,來自Java的Mahout是要走的路。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.