protected static void predict(String[] args) { if (args.length != 3) { System.out.println( "Please specify the path for the test data as the second argument " + "and the path to save the predictions as the third argument."); } else { final String testDataFilename = args[1]; try { // Parse the sparse testing data. final Map.Entry<List<SparseLineAST>, Integer> instancesToMaxFeatureIndex = SparseLineAST.parseSparseEncodingFromInputFilename( testDataFilename, new SparseLineAST.NumericValidator()); final List<SparseLineAST> instances = instancesToMaxFeatureIndex.getKey(); final int maxFeatureIndex = instancesToMaxFeatureIndex.getValue(); // Initialize the classifier from the file data. DiscretizingNaiveBayesClassifier classifier = new DiscretizingNaiveBayesClassifier(); classifier.initializeFromFileData(loadTrainedModelFileData()); // Make predictions. // int numCorrectPredictions = 0; // TODO: Remove debug code. List<Integer> predictedClassIndices = new ArrayList<Integer>(instances.size()); for (SparseLineAST instance : instances) { final int predictedClassIndex = classifier.getPrediction(instance); predictedClassIndices.add(predictedClassIndex); /* // TODO: Remove debug code. final int correctClassIndex = Integer.parseInt(instance.getOutput()); if (predictedClassIndex == correctClassIndex) { numCorrectPredictions += 1; } //*/ } /* // TODO: Remove debug code. System.out.println(String.format("%d of %d predicted correctly (%f).", numCorrectPredictions, predictedClassIndices.size(), numCorrectPredictions / (double) predictedClassIndices.size())); //*/ // Save the predictions to file. final String predictionsFilename = args[2]; savePredictions(predictionsFilename, predictedClassIndices); } catch (RuntimeException x) { System.out.println(x.getMessage()); } } }
protected static void generate(String[] args) { if (args.length != 3) { System.out.println( "Please specify the number of instances as the second argument " + "and the path to save the instances as the third argument."); } else { final int numInstances = Integer.parseInt(args[1]); final String dataFilename = args[2]; // Initialize the classifier from the file data. DiscretizingNaiveBayesClassifier classifier = new DiscretizingNaiveBayesClassifier(); classifier.initializeFromFileData(loadTrainedModelFileData()); SparseLineAST.saveSparseEncoding( dataFilename, classifier.generateLabeledInstances(numInstances)); } }