📄 bnctester.java
字号:
/** * JBNC - Bayesian Network Classifiers Toolbox <p> * * Latest release available at http://sourceforge.net/projects/jbnc/ <p> * * Copyright (C) 1999-2003 Jarek Sacha <p> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. <p> * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. <p> * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307, USA. <br> * http://www.fsf.org/licenses/gpl.txt */package jbnc.util;import BayesianNetworks.BayesNet;import BayesianNetworks.ProbabilityFunction;import InferenceGraphs.InferenceGraph;import InferenceGraphs.InferenceGraphNode;import QuasiBayesianInferences.QBInference;import gnu.getopt.Getopt;import jbnc.dataset.AttributeType;import jbnc.dataset.Dataset;import jbnc.dataset.DatasetReader;import jbnc.dataset.NamesReader;import java.text.DecimalFormat;import java.text.NumberFormat;import java.util.HashMap;import java.util.Vector;/** * Description of the Class * * @author Jarek Sacha * @since */public class BNCTester { /* * Private and protected constants */ protected final static String classNameDefault = "class"; /* * Private and protected member variables */ private final static String programName = "BNCTester"; private final static String programVersion = "v.0.02"; // Command line configuration parameters protected boolean useTimer = false; protected boolean debugMode = false; protected String dataFileName = null; protected String namesFileName = null; protected String netFileName = null; protected String className = null; // Test data set protected Dataset dataset; // Network representation protected InferenceGraph graph; // Graph representing the network protected InferenceGraphNode[] attribNodes; // References to graph nodes, order as in cases protected InferenceGraphNode classNode; // Reference to class node in the graph // Classification statistics protected int nbPass; protected int nbFail; /** * @param argv Description of Parameter */ public static void main(String argv[]) { String usageMsg = "\n" + "USAGE: BNCTester <options> network_file [class_name]\n" + "\n" + "where:\n" + " -d Print debugging information.\n" + " -f <file_stem> Load test data in C4.5 format (.names + .test),\n" + " file_stem.names - file with specification of attributes,\n" + " file_stem.test - file with test cases.\n" + " -h <test_data> Load test data in comma delimited format. First row contains\n" + " attribute names.\n" + " -t Print execution time." + "\n" + " network_file Classifier network definition file.\n" + " class_name Name of the class variable. The default value is 'class'.\n" + "\n" + "EXAMPLE: BNCTester -tf monk1 monk1.bif"; try { Timer t = new Timer(); // Print header System.out.println("\nTester for Bayesian network classifiers.\n"); BNCTester tester = new BNCTester(); // Process command line arguments if (!tester.processCommandLine(argv)) { System.out.println(usageMsg); return; } // Run the test tester.run(); // Print execution time if (tester.useTimer) { t.println("\nExecution time = "); } } catch (Exception e) { e.printStackTrace(); } } /** * Sets the DebugMode attribute of the BNCTester object * * @param mode The new DebugMode value */ public void setDebugMode(boolean mode) { debugMode = mode; } /** * @param net Description of Parameter * @param dataset Description of Parameter * @return Confusion matrix. * @exception Exception Description of Exception */ public Result test(BayesNet net, Dataset dataset) throws Exception { clear(); this.graph = null; this.dataset = dataset; this.className = null;// verifyDatasetVsNetwork(); testClassifier(net); if (debugMode) { printClassificationReport(); } return new Result(nbPass, nbFail); } /** * @param net Description of Parameter * @param dataset Description of Parameter * @return Confusion matrix. * @exception Exception Description of Exception */ public Result test_old(BayesNet net, Dataset dataset) throws Exception { return test_old(new InferenceGraph(net), dataset); } /** * @param graph Description of Parameter * @param dataset Description of Parameter * @return Confusion matrix. * @exception Exception Description of Exception */ public Result test_old(InferenceGraph graph, Dataset dataset) throws Exception { clear(); this.graph = graph; this.dataset = dataset; this.className = dataset.names[dataset.names.length - 1].getName(); verifyDatasetVsNetwork(); testClassifier_old(); if (debugMode) { printClassificationReport(); } return new Result(nbPass, nbFail); } /** Print test results to the standard output. */ public void report() { printClassificationReport(); } /** * Print test results to the standard output in the short form (single line). * * @exception Exception Description of Exception */ public void reportShort() throws Exception { Result r = new Result(nbPass, nbFail); r.reportShort(); } /** * Process command line arguments. * * @param argv Description of Parameter * @return 'false' when command line is invalid. */ protected boolean processCommandLine(String[] argv) { try { Getopt g = new Getopt("ERROR", argv, "df:h:t"); int c; String arg = null; boolean is_f = false; boolean is_h = false; while ((c = g.getopt()) != -1) { switch (c) { case 'd': debugMode = true; break; case 'f': is_f = true; arg = g.getOptarg(); dataFileName = arg + ".test"; namesFileName = arg + ".names"; break; case 'h': is_h = true; dataFileName = g.getOptarg(); break; case 't': useTimer = true; break; case '?': break; default: System.out.println("\nERROR: getopt() returned: " + c); return false; } } if (is_f && is_h) { System.out.println("\nERROR: Options 'f' and 'h' cannot appear together."); return false; } if (!is_f && !is_h) { System.out.println("\nERROR: Either option 'f' or 'h' has to be specified."); return false; } // Classifier network file name int index = g.getOptind(); if (index >= argv.length) { System.out.println("\nERROR: Classifier network file not specified."); return false; } netFileName = argv[index]; // Class name ++index; if (index < argv.length) { className = argv[index]; } else { className = classNameDefault; } if (debugMode) { System.out.println("debug mode = " + debugMode); System.out.println("use timer = " + useTimer); System.out.println("data file = " + dataFileName); System.out.println("names file = " + namesFileName); System.out.println("network file = " + netFileName); System.out.println("class name = " + className); } ++index; if (index >= argv.length) { return true; } // Staff left on the command line System.out.print("\nERROR: Too many arguments in the command line. Extraneous: "); for (; index < argv.length; ++index) { System.out.print(argv[index] + ", "); } System.out.println(""); } catch (Exception e) { System.out.println("\nError: " + e); if (debugMode) { e.printStackTrace(); } } return false; } /* * */ protected void run() { try { Timer tLoadNet = new Timer(); loadNetwork(); tLoadNet.stop(); Timer tLoadData = new Timer(); loadDataset(); tLoadData.stop();// Timer tTest = new Timer();// test(graph, dataset);// tTest.stop(); Timer tVerify = new Timer(); verifyDatasetVsNetwork(); tVerify.stop(); Timer tClassify = new Timer(); testClassifier_old();// testClassifier(); tClassify.stop(); Timer tReport = new Timer(); printClassificationReport(); tReport.stop(); if (useTimer) { tLoadNet.println("\nLoad net = "); tLoadData.println("Load data = ");// tTest.println( "Test = "); tVerify.println("Verify = "); tClassify.println("Classify = "); tReport.println("Report = "); } } catch (Exception e) { System.out.println("\nError: " + e); if (debugMode) { e.printStackTrace(); } } } /* * */ protected void loadNetwork() throws Exception { if (debugMode) { System.out.println("\nLoading network: " + netFileName); } if (netFileName == null) { throw new Exception("Name of the network file is not set."); } // Load network graph = new InferenceGraph(netFileName); if (debugMode) { Vector graphNodes = graph.get_nodes();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -