テキスト分類に単純ベイズ アルゴリズムを実装しています。トレーニング用に約 1000 個のドキュメント、テスト用に 400 個のドキュメントがあります。トレーニング部分は正しく実装したと思いますが、テスト部分で混乱しています。これが私が簡単にやったことです:
私のトレーニング機能では:
vocabularySize= GetUniqueTermsInCollection();//get all unique terms in the entire collection
spamModelArray[vocabularySize];
nonspamModelArray[vocabularySize];
for each training_file{
class = GetClassLabel(); // 0 for spam or 1 = non-spam
document = GetDocumentID();
counterTotalTrainingDocs ++;
if(class == 0){
counterTotalSpamTrainingDocs++;
}
for each term in document{
freq = GetTermFrequency; // how many times this term appears in this document?
id = GetTermID; // unique id of the term
if(class = 0){ //SPAM
spamModelArray[id]+= freq;
totalNumberofSpamWords++; // total number of terms marked as spam in the training docs
}else{ // NON-SPAM
nonspamModelArray[id]+= freq;
totalNumberofNonSpamWords++; // total number of terms marked as non-spam in the training docs
}
}//for
for i in vocabularySize{
spamModelArray[i] = spamModelArray[i]/totalNumberofSpamWords;
nonspamModelArray[i] = nonspamModelArray[i]/totalNumberofNonSpamWords;
}//for
priorProb = counterTotalSpamTrainingDocs/counterTotalTrainingDocs;// calculate prior probability of the spam documents
}
トレーニング部分を正しく理解して実装したと思いますが、テスト部分を正しく実装できたかどうかはわかりません。ここでは、各テスト ドキュメントを調べて、各ドキュメントの logP(spam|d) と logP(non-spam|d) を計算します。次に、クラス (スパム/非スパム) を決定するために、これら 2 つの量を比較します。
私のテスト機能では:
vocabularySize= GetUniqueTermsInCollection;//get all unique terms in the entire collection
for each testing_file:
document = getDocumentID;
logProbabilityofSpam = 0;
logProbabilityofNonSpam = 0;
for each term in document{
freq = GetTermFrequency; // how many times this term appears in this document?
id = GetTermID; // unique id of the term
// logP(w1w2.. wn) = C(wj)∗logP(wj)
logProbabilityofSpam+= freq*log(spamModelArray[id]);
logProbabilityofNonSpam+= freq*log(nonspamModelArray[id]);
}//for
// Now I am calculating the probability of being spam for this document
if (logProbabilityofNonSpam + log(1-priorProb) > logProbabilityofSpam +log(priorProb)) { // argmax[logP(i|ck) + logP(ck)]
newclass = 1; //not spam
}else{
newclass = 0; // spam
}
}//for
私の問題は; 正確な 1 と 0 (スパム/非スパム) ではなく、各クラスの確率を返したいと考えています。たとえば、 newclass = 0.8684212 を見たいので、後でしきい値を適用できます。しかし、私はここで混乱しています。各ドキュメントの確率を計算するにはどうすればよいですか? logProbabilities を使用して計算できますか?