⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 evalbayesian.java

📁 次代码是数据挖掘软件的一个小例子
💻 JAVA
字号:
import weka.classifiers.*;
import java.io.*;
import java.util.*;

import weka.classifiers.BayesianNaive;
import weka.classifiers.BayesianNetOrder;
import weka.classifiers.BayesianNetFixed;
import weka.classifiers.BayesianNetSearch;

/**
 * You are recommended to use this EvalBayesian class as the main class in your
 * project to evaluate all your Bayesian classifiers. It will be much convenient
 * for TA to run your program and make comparisions. However, you can define your
 * own main class while debugging for a particular classifier.
 *
 * All the classifier classes are passed to Weka's Evaluation class, The evaluation()
 * method in weka.classifiers.Evaluation could directly use the the generic scheme-
 * independent command-line options, such as specify the input training and test files.
 * For the need of adding specific options, you can use the OptionHandler interface in
 * weka.classifiers.
 *
 * We specify the name of the several class you will need to implement in this lab
 * assignment:
 * (1) "BayesianNaive" for Naive Bayes Classifier.
 * (2) "BayesianNetOrder" for Bayesian Network using statistics of order at most k.
 * (3) "BayesianNetFixed" for parameter estimation for Bayesian Network with fixed
 *     topology.
 * (4) "BayesianNetSearch" for Bayesian network algorithm by searching the space
 *     of candidate networks.
 *
 * For each testing process, you are required to input the command line arguments
 * according to the prompt. You also need to write a readme.txt file to tell TA
 * your running parameters for each of your classifiers.
 *
 */


class EvalBayesian {

  private static String[] argPro;

  private static String inputStr(String s) {
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
     String oneLine = null;
     System.out.print(s);
    try {
       oneLine = input.readLine();
       }
     catch (Exception e) {
       e.printStackTrace();
       }
     return oneLine;
  //   return ("-t arrhythmia.train.arff -T arrhythmia.test.arff");
 // return ("-t house-votes-84.train.arff -T house-votes-84.test.arff");
//     return ("-t weather.arff -T weather.arff");
   }

   private static int getArgSize(String s) {
     StringTokenizer st = new StringTokenizer(s);
     int index = 0;
     while (st.hasMoreTokens()) {
         st.nextToken();
         index++;
     }
     return index;
   }

   private static void assignargPro(String s) {
     StringTokenizer st = new StringTokenizer(s);
         
         String input=new String();
         String output1=new String("trainningSet.arff");
         String output2=new String("testSet.arff");
         String  m_arg[]=new String[8];
         
     int count = 0;
     while (st.hasMoreTokens()) {
         argPro[count] = st.nextToken();
                /**use filter to */
/**begin*/
 
                 //deal with numeric data
                 if (argPro[count].equals("-t") ){
                         count++;
                         input=new String(st.nextToken());
                         
                         Runtime r = Runtime.getRuntime();
             Process p = null;
             
                         m_arg[0]=new String("java");
                         m_arg[1]=new String("weka.filters.DiscretizeFilter");
                         m_arg[2]=new String("-c");
                         m_arg[3]=new String("last");
		         m_arg[4]=new String("-i");
                         m_arg[5]=new String(input);
                         m_arg[6]=new String("-o");
                         m_arg[7]=new String(output1);
             try {  
                                 p = r.exec(m_arg);
                                 p.waitFor();                                    
             }
             catch (Exception e) { }
                 argPro[count] = output1;
                 }
                 if (argPro[count].equals("-T") ){
                 	//weka.filters.DiscretizeFilter.main(m_arg );
                         count++;
                         input=new String(st.nextToken());
                         Runtime r = Runtime.getRuntime();
             Process p = null;
                         m_arg[0]=new String("java");
                         m_arg[1]=new String("weka.filters.DiscretizeFilter");
                         m_arg[2]=new String("-c");
                         m_arg[3]=new String("last");
		         m_arg[4]=new String("-i");
                         m_arg[5]=new String(input);
                         m_arg[6]=new String("-o");
                         m_arg[7]=new String(output2);
             try {                                    
             p = r.exec(m_arg);
                         p.waitFor();
/** end */
             }
             catch (Exception e) { }
                         argPro[count] = output2;
                 }               
         count++;
     }
//     weka.filters.ReplaceMissingValuesFilter.main() 
   }

  public static void main(String[] args) {

     System.out.println("###############################################################################");
     System.out.println("(1) Evaluation for Naive Bayes Classifier. ");
     String aLine = inputStr("Input your running parameters: ");
     argPro = new String[getArgSize(aLine)];
     assignargPro(aLine);
     try {
        System.out.println(Evaluation.evaluateModel(new BayesianNaive(), argPro));
         } catch (Exception e) {

             System.err.println(e.getMessage());
             }
     System.out.println("###############################################################################");
     System.out.println("(2) Evaluation for Bayesian Network using statistics of order at most k.");
     String bLine = inputStr("Input your running parameters: ");
     argPro = new String[getArgSize(bLine)];
     assignargPro(bLine);
     try {
        System.out.println(Evaluation.evaluateModel(new BayesianNetOrder(), argPro));
        } catch (Exception e) {

             System.err.println(e.getMessage());
             }
    System.out.println("###############################################################################");
     System.out.println("(3) Evaluation for parameter estimation for Bayesian Network with fixed topology.");
     String cLine = inputStr("Input your running parameters: ");
     argPro = new String[getArgSize(cLine)];
     assignargPro(cLine);
     try {
        System.out.println(Evaluation.evaluateModel(new BayesianNetFixed(), argPro));
        } catch (Exception e) {
             System.err.println(e.getMessage());
             }

     System.out.println("###############################################################################");
     System.out.println("(4) Evaluation for Bayesian network algorithm by searching the space of candidate networks.");
     String dLine = inputStr("Input your running parameters: ");
     argPro = new String[getArgSize(dLine)];
     assignargPro(dLine);
     try {
        System.out.println(Evaluation.evaluateModel(new BayesianNetSearch(), argPro));
        } catch (Exception e) {
             System.err.println(e.getMessage());
             }
     System.out.println("###############################################################################");
     System.out.println("                         Evaluation Process Completed.");
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -