List of usage examples for weka.classifiers Evaluation toSummaryString
public String toSummaryString(String title, boolean printComplexityStatistics)
From source file:com.guidefreitas.locator.main.Main.java
public static void main(String[] args) throws Exception { //String dataFile = "wifi_data.txt"; String dataFile = "wifi_data_university.txt"; DatabaseManager.createDatabase();/* w w w. j a v a 2 s . c om*/ DatabaseManager.seedLocations(dataFile); PredictionService predService = PredictionService.getInstance(); predService.generateTrainData(); Evaluation eval = predService.train(); System.out.println("================================================"); System.out.println(eval.toSummaryString("\nResults\n======\n", true)); System.out.println("================================================"); System.out.println(eval.toMatrixString("\nConfusion Matrix\n======\n")); System.out.println("================================================"); }
From source file:com.mycompany.id3classifier.ID3Shell.java
public static void main(String[] args) throws Exception { ConverterUtils.DataSource source = new ConverterUtils.DataSource("lensesData.csv"); Instances dataSet = source.getDataSet(); Discretize filter = new Discretize(); filter.setInputFormat(dataSet);/*w ww . j av a2 s.c o m*/ dataSet = Filter.useFilter(dataSet, filter); Standardize standardize = new Standardize(); standardize.setInputFormat(dataSet); dataSet = Filter.useFilter(dataSet, standardize); dataSet.setClassIndex(dataSet.numAttributes() - 1); dataSet.randomize(new Random(9001)); //It's over 9000!! int folds = 10; //Perform crossvalidation Evaluation eval = new Evaluation(dataSet); for (int n = 0; n < folds; n++) { int trainingSize = (int) Math.round(dataSet.numInstances() * .7); int testSize = dataSet.numInstances() - trainingSize; Instances trainingData = dataSet.trainCV(folds, n); Instances testData = dataSet.testCV(folds, n); ID3Classifier classifier = new ID3Classifier(); // Id3 classifier = new Id3(); classifier.buildClassifier(trainingData); eval.evaluateModel(classifier, testData); } System.out.println(eval.toSummaryString("\nResults:\n", false)); }
From source file:com.mycompany.knnclassifier.kNNShell.java
public static void main(String[] args) throws Exception { ConverterUtils.DataSource source = new ConverterUtils.DataSource("carData.csv"); Instances dataSet = source.getDataSet(); Standardize standardize = new Standardize(); standardize.setInputFormat(dataSet); dataSet = Filter.useFilter(dataSet, standardize); dataSet.setClassIndex(dataSet.numAttributes() - 1); dataSet.randomize(new Random(9001)); //It's over 9000!! int trainingSize = (int) Math.round(dataSet.numInstances() * .7); int testSize = dataSet.numInstances() - trainingSize; Instances trainingData = new Instances(dataSet, 0, trainingSize); Instances testData = new Instances(dataSet, trainingSize, testSize); kNNClassifier classifier = new kNNClassifier(3); classifier.buildClassifier(trainingData); //Used to compare to Weka's built in KNN algorithm //Classifier classifier = new IBk(1); //classifier.buildClassifier(trainingData); Evaluation eval = new Evaluation(trainingData); eval.evaluateModel(classifier, testData); System.out.println(eval.toSummaryString("\nResults:\n", false)); }
From source file:com.mycompany.neuralnetwork.NeuralNetworkShell.java
public static void main(String[] args) throws Exception { ConverterUtils.DataSource source = new ConverterUtils.DataSource("irisData.csv"); Instances dataSet = source.getDataSet(); Standardize standardize = new Standardize(); standardize.setInputFormat(dataSet); dataSet = Filter.useFilter(dataSet, standardize); dataSet.setClassIndex(dataSet.numAttributes() - 1); dataSet.randomize(new Random(9001)); //It's over 9000!! int trainingSize = (int) Math.round(dataSet.numInstances() * .7); int testSize = dataSet.numInstances() - trainingSize; Instances trainingData = new Instances(dataSet, 0, trainingSize); Instances testData = new Instances(dataSet, trainingSize, testSize); //MultilayerPerceptron classifier = new MultilayerPerceptron(); NeuralNetworkClassifier classifier = new NeuralNetworkClassifier(3, 20000, 0.1); classifier.buildClassifier(trainingData); Evaluation eval = new Evaluation(trainingData); eval.evaluateModel(classifier, testData); System.out.println(eval.toSummaryString("\nResults:\n", false)); }
From source file:core.classification.Classifiers.java
License:Open Source License
public void trainSC() throws Exception { String sql;// ww w.j a va2 s. c om // --- // Connect to the database // --- InstanceQuery query = new InstanceQuery(); query.setDatabaseURL(dbase); query.setUsername(""); query.setPassword(""); // --- // --- // SCA // --- // --- sql = "SELECT "; sql += "CR.ratio, CR.class "; sql += "FROM Class_ratio AS CR;"; query.setQuery(sql); Instances data = query.retrieveInstances(); // --- // Setting options // --- String[] options = Utils.splitOptions( "-D -Q weka.classifiers.bayes.net.search.local.K2 -- -P 1 -S BAYES -E weka.classifiers.bayes.net.estimate.SimpleEstimator -- -A 0.5"); SCA.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train the classifier // --- System.out.println("Building SCA ..."); SCA.buildClassifier(data); System.out.println("Done."); // --- // Classifier evaluation // --- System.out.println("Cross-validation for SCA..."); Evaluation eval = new Evaluation(data); eval.crossValidateModel(SCA, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for SCA: \n\n", false)); // --- // --- // SCB // --- // --- sql = "SELECT "; sql += "Data.H2, Data.D2, Data.DX, "; sql += "Data.PARENT_CHAR AS PCLASS, "; sql += "Data.CLASS "; sql += "FROM Data "; sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );"; query.setQuery(sql); data = query.retrieveInstances(); // --- // Setting options // --- options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a"); SCB.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train the classifier // --- System.out.println("Building SCB ..."); SCB.buildClassifier(data); System.out.println("Done."); // --- // Classifier evaluation // --- System.out.println("Cross-validation for SCB..."); eval = new Evaluation(data); eval.crossValidateModel(SCB, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for SCB: \n\n", false)); // --- // --- // SCC // --- // --- // ---- // SCC1 // ---- sql = "SELECT "; sql += "Data.LH, Data.LD, Data.LDX, Data.LCLASS, "; sql += "Data.CLASS "; sql += "FROM Data "; sql += "WHERE ( (Data.SEGERR)=0 AND ( (Data.LCLASS)<>'0' ) );"; query.setQuery(sql); data = query.retrieveInstances(); // --- // Setting options // --- options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a"); SCC1.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train the classifier // --- System.out.println("Building SCC1 ..."); SCC1.buildClassifier(data); System.out.println("Done."); // --- // Classifier evaluation // --- System.out.println("Cross-validation for SCC1..."); eval = new Evaluation(data); eval.crossValidateModel(SCC1, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for SCC1: \n\n", false)); // ---- // SCC2 // ---- sql = "SELECT "; sql += "Data.EH, Data.ED, Data.EDX, Data.ECLASS, "; sql += "Data.CLASS "; sql += "FROM Data "; sql += "WHERE ( (Data.SEGERR)=0 AND ( (Data.ECLASS)<>'0' ) );"; query.setQuery(sql); data = query.retrieveInstances(); // --- // Setting options // --- // options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a"); SCC2.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train the classifier // --- System.out.println("Building SCC2 ..."); SCC2.buildClassifier(data); System.out.println("Done."); // --- // Classifier evaluation // --- System.out.println("Cross-validation for SCC2..."); eval = new Evaluation(data); eval.crossValidateModel(SCC2, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for SCC2: \n\n", false)); // ---- // SCC3 // ---- sql = "SELECT "; sql += "Data.SH, Data.SD, Data.SDX, Data.SCLASS, "; sql += "Data.CLASS "; sql += "FROM Data "; sql += "WHERE ( (Data.SEGERR)=0 AND ( (Data.SCLASS)<>'0' ) );"; query.setQuery(sql); data = query.retrieveInstances(); // --- // Setting options // --- // options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a"); SCC3.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train the classifier // --- System.out.println("Building SCC3 ..."); SCC3.buildClassifier(data); System.out.println("Done."); // --- // Classifier evaluation // --- System.out.println("Cross-validation for SCC3..."); eval = new Evaluation(data); eval.crossValidateModel(SCC3, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for SCC3: \n\n", false)); }
From source file:core.classification.Classifiers.java
License:Open Source License
public void trainRC() throws Exception { // ---/*from w w w . j a v a2 s .c o m*/ // Retrieve the instances in the database // --- InstanceQuery query = new InstanceQuery(); query.setDatabaseURL(dbase); query.setUsername(""); query.setPassword(""); String sql = "SELECT "; sql += "Data.H2, Data.D2, Data.DX, "; sql += "Data.CLASS, Data.PARENT_CHAR AS PCLASS, "; sql += "Data.RELID "; sql += "FROM Data "; sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );"; query.setQuery(sql); Instances data = query.retrieveInstances(); // --- // Setting options // --- // String[] options = Utils.splitOptions("-L 0.2 -M 0.2 -N 50 -V 0 -S 0 -E 20 -H 5 "); String[] options = Utils.splitOptions( "-cost-matrix \"[0.0 1.0 1.0 0.1 0.1; 1.0 0.0 1.0 0.1 0.1; 1.0 1.0 0.0 0.1 0.1; 10.0 10.0 10.0 0.0 1.0; 10.0 10.0 10.0 1.0 0.0]\" -S 1 -W weka.classifiers.functions.MultilayerPerceptron -- -L 0.2 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a"); RC.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train // --- System.out.println("Building RC..."); RC.buildClassifier(data); System.out.println("Done."); // --- // Evaluation // --- System.out.println("Cross-validation for RC..."); Evaluation eval = new Evaluation(data); eval.crossValidateModel(RC, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for RC: \n\n", false)); }
From source file:core.classification.Classifiers.java
License:Open Source License
public void trainYNC() throws Exception { // ---/*from w w w. ja va 2 s . c om*/ // Retrieve the instances in the database // --- InstanceQuery query = new InstanceQuery(); query.setDatabaseURL(dbase); query.setUsername(""); query.setPassword(""); String sql = "SELECT "; sql += "YNCdata.PCLASS, YNCdata.CCLASS, YNCdata.RAREA, YNCdata.H, YNCdata.D, YNCdata.V, "; sql += "YNCdata.YN "; sql += "FROM YNCdata "; query.setQuery(sql); Instances data = query.retrieveInstances(); // --- // Setting options // --- String[] options = Utils.splitOptions("-R -N 3 -Q 1 -M 30"); YNC.setOptions(options); data.setClassIndex(data.numAttributes() - 1); // --- // Train // --- System.out.println("Building YC..."); YNC.buildClassifier(data); System.out.println("Done."); // --- // Evaluation // --- System.out.println("Cross-validation for YNC..."); Evaluation eval = new Evaluation(data); eval.crossValidateModel(YNC, data, 10, new Random(1)); System.out.println("Done."); System.out.println(eval.toSummaryString("\n Results for YNC: \n\n", false)); }
From source file:examples.Pair.java
License:Open Source License
/** * @param args the command line arguments *///from w w w . ja v a2 s .co m public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Requires path to the dataset as the first and only argument"); return; } final String datasetPath = args[0]; // Create classifiers MultiStageCascading msc = new MultiStageCascading(); J48 classifier1 = new J48(); IBk knn = new IBk(3); // Set sequence of classifiers msc.setClassifiers(new Classifier[] { classifier1, new NBTree() }); msc.setDebug(true); // Set a classifier that will classify an instance that is not classified by all other classifiers msc.setLastClassifier(knn); // First classifier will have confidence threshold 0.95 and the second one 0.97 msc.setConfidenceThresholds("0.95,0.97"); // 80% of instances in training set will be randomly selected to train j-th classifier msc.setPercentTrainingInstances(0.8); Instances dataset = DataSource.read(datasetPath); dataset.setClassIndex(dataset.numAttributes() - 1); // Create test and training sets Pair<Instances, Instances> sets = seprateTestAndTrainingSets(dataset, 0.7); Instances trainingSet = sets.getFirst(); Instances testSet = sets.getSecond(); // Build cascade classifier msc.buildClassifier(trainingSet); // Evaluate created classifier Evaluation eval = new Evaluation(trainingSet); eval.evaluateModel(msc, testSet); System.out.println(eval.toSummaryString("\nResults\n\n", false)); }
From source file:experimentalclassifier.ExperimentalClassifier.java
/** * @param args the command line arguments *//* w w w . j a v a2s.c om*/ public static void main(String[] args) throws Exception { DataSource source = new DataSource("data/iris.csv"); Instances data = source.getDataSet(); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } data.randomize(new Random()); String[] options = weka.core.Utils.splitOptions("-P 30"); RemovePercentage remove = new RemovePercentage(); remove.setOptions(options); remove.setInputFormat(data); Instances train = Filter.useFilter(data, remove); remove.setInvertSelection(true); remove.setInputFormat(data); Instances test = Filter.useFilter(data, remove); Classifier classifier = new HardCodedClassifier(); classifier.buildClassifier(train);//Currently, this does nothing Evaluation eval = new Evaluation(train); eval.evaluateModel(classifier, test); System.out.println(eval.toSummaryString("\nResults\n======\n", false)); }
From source file:expshell.ExpShell.java
/** * @param args the command line arguments * @throws java.lang.Exception/* w ww .jav a 2s . c o m*/ */ public static void main(String[] args) throws Exception { String file = "C:\\Users\\YH Jonathan Kwok\\Documents\\NetBeansProjects\\ExpShell\\src\\expshell\\iris.csv"; DataSource source = new DataSource(file); Instances data = source.getDataSet(); if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); //Randomize it data.randomize(new Random(1)); RemovePercentage rp = new RemovePercentage(); rp.setPercentage(70); rp.setInputFormat(data); Instances training = Filter.useFilter(data, rp); rp.setInvertSelection(true); rp.setInputFormat(data); Instances test = Filter.useFilter(data, rp); //standardize the data Standardize filter = new Standardize(); filter.setInputFormat(training); Instances newTest = Filter.useFilter(test, filter); Instances newTraining = Filter.useFilter(training, filter); //Part 5 - Now it's a knn Classifier knn = new NeuralClassifier(); knn.buildClassifier(newTraining); Evaluation eval = new Evaluation(newTraining); eval.evaluateModel(knn, newTest); System.out.println(eval.toSummaryString("***** Overall results: *****", false)); }