2014-12-25 109 views
-1

我需要一個文本分類到多個類別的解決方案。這種方法似乎效果不錯:http://www.codeproject.com/Articles/14270/A-Naive-Bayesian-Classifier-in-C樸素貝葉斯分類百分比

我只有一個問題與返回的分數。目前最高分數意味着最適合這個類別。

但我想獲得每個類別的百分比值。

這是分數計算的一部分:

/// <summary> 
/// Classifies a text<\summary> 
/// <returns> 
/// returns classification values for the text, the higher, the better is the match.</returns> 
public Dictionary<string, double> Classify(System.IO.StreamReader tr) 
{ 
    Dictionary<string, double> score = new Dictionary<string, double>(); 
    foreach (KeyValuePair<string, ICategory> cat in m_Categories) 
    { 
     score.Add(cat.Value.Name, 0.0); 
    } 

    EnumerableCategory words_in_file = new EnumerableCategory("", m_ExcludedWords); 
    words_in_file.TeachCategory(tr); 

    foreach (KeyValuePair<string, PhraseCount> kvp1 in words_in_file) 
    { 
     PhraseCount pc_in_file = kvp1.Value; 
     foreach (KeyValuePair<string, ICategory> kvp in m_Categories) 
     { 
      ICategory cat = kvp.Value; 
      int count = cat.GetPhraseCount(pc_in_file.RawPhrase); 
      if (0 < count) 
      { 
       score[cat.Name] += System.Math.Log((double)count/(double)cat.TotalWords); 
      } 
      else 
      { 
       score[cat.Name] += System.Math.Log(0.01/(double)cat.TotalWords); 
      } 
      System.Diagnostics.Trace.WriteLine(pc_in_file.RawPhrase.ToString() + "(" + 
       cat.Name + ")" + score[cat.Name]); 
     } 


    } 
    foreach (KeyValuePair<string, ICategory> kvp in m_Categories) 
    { 
     ICategory cat = kvp.Value; 
     score[cat.Name] += System.Math.Log((double)cat.TotalWords/(double)this.CountTotalWordsInCategories()); 
    } 
    return score; 
} 

感謝您的幫助!

+0

那麼,您的問題是什麼?如果您的代碼無效,請告訴我們原因。告訴我們預期的產量和實際產量。只要在我們身上放幾行代碼,幾乎沒有任何解釋,就不可能爲您帶來任何好處。 –

回答

1

如果我理解正確,您需要總計Values,Dictionary,它會給你100%。然後將收到的總和除以每個Value。 在return score;前加入代碼:

​​