2017-11-27 20 views
1

カテゴリ別のテキスト例のニュースを分類しようとしています。私はデータベースにカテゴリのニューステキストの巨大なデータセットを持っています。マシンは訓練を受け、ニュースカテゴリを決定する必要があります。テキスト分類NaiveBayes

public static string[] Tokenize(string text) 
    { 
     StringBuilder sb = new StringBuilder(text); 

     char[] invalid = "!-;':'\",.?\n\r\t".ToCharArray(); 

     for (int i = 0; i < invalid.Length; i++) 
      sb.Replace(invalid[i], ' '); 

     return sb.ToString().Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     string strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = c:\\users\\158820\\Documents\\Database4.accdb"; 
     string strSQL = "SELECT * FROM NewsRepository"; 
     // create Objects of ADOConnection and ADOCommand 
     OleDbConnection myConn = new OleDbConnection(strDSN); 
     OleDbDataAdapter myCmd = new OleDbDataAdapter(strSQL, myConn); 
     myConn.Open(); 
     DataSet dtSet = new DataSet(); 
     myCmd.Fill(dtSet, "NewsRepository"); 
     DataTable dTable = dtSet.Tables[0]; 
     myConn.Close(); 

     StringBuilder sWords = new StringBuilder(); 
     string[][] swords = new string[dTable.Rows.Count][]; 
     int i = 0; 

     foreach (DataRowView dr in dTable.DefaultView) 
     { 
      swords[i] = Tokenize(dr[1].ToString()); 
      i++; 
     } 

     Codification codebook = new Codification(dTable, new string[] { "NewsTitle", "Category" }); 
     DataTable symbols = codebook.Apply(dTable); 
     int[][] inputs = symbols.ToJagged<int>(new string[] { "NewsTitle" }); 
     int[] outputs = symbols.ToArray<int>("Category"); 

     bagOfWords(inputs, outputs); 
    } 


    private static void bagOfWords(int[][] inputs, int[] outputs) 
    { 
     var bow = new BagOfWords<int>(); 
     var quantizer = bow.Learn(inputs); 
     string filenamebow = Path.Combine(Application.StartupPath, "News_BOW.accord"); 
     Serializer.Save(obj: bow, path: filenamebow); 
     double[][] histograms = quantizer.Transform(inputs); 

     // One way to perform sequence classification with an SVM is to use 
     // a kernel defined over sequences, such as DynamicTimeWarping. 

     // Create the multi-class learning algorithm as one-vs-one with DTW: 
     var teacher = new MulticlassSupportVectorLearning<ChiSquare, double[]>() 
     { 
      Learner = (p) => new SequentialMinimalOptimization<ChiSquare, double[]>() 
      { 
       // Complexity = 100 // Create a hard SVM 
      } 
     }; 

     // Learn a multi-label SVM using the teacher 
     var svm = teacher.Learn(histograms, outputs); 

     // Get the predictions for the inputs 
     int[] predicted = svm.Decide(histograms); 

     // Create a confusion matrix to check the quality of the predictions: 
     var cm = new GeneralConfusionMatrix(predicted: predicted, expected: outputs); 

     // Check the accuracy measure: 
     double accuracy = cm.Accuracy; 

     string filename = Path.Combine(Application.StartupPath, "News_SVM.accord"); 
     Serializer.Save(obj: svm, path: filename); 
    } 

私はaccord.netオブジェクトを訓練する方法についてちょっと混乱しています。私は訓練されたモデルを連載することができます(9つのカテゴリー内で3600のユニークなニュースの約106 MBです)

新しいモデルのニュースのカテゴリを予測するためにどのようにモデルを使用しますか?あなたは訓練されたモデルは、あなたがあるSerializer.Load<T>を使用してSVMオブジェクトをインスタンス化することができ連載しておりますので

svm.Decide(outofSampleData) 

答えて

0

あなたのトレーニングセットにないデータにモデルを使用するには、別の決定をするためにあなたのSVMを呼び出すのと同じくらい簡単です文書番号here

関連する問題