Classifier cModel = (Classifier)new NaiveBayes(); cModel.buildClassifier(isTrainingSet); weka.core.SerializationHelper.write("/some/where/nBayes.model", cModel); Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/nBayes.model"); // Test the model Evaluation eTest = new Evaluation(isTrainingSet); eTest.evaluateModel(cls, isTrainingSet);
//load model String rootPath="/some/where/"; Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath+"tree.model"); //predict instance class values Instances originalTrain= //load or create Instances to predict //which instance to predict class value int s1=0; //perform your prediction double value=cls.classifyInstance(originalTrain.instance(s1)); //get the name of the class value String prediction=originalTrain.classAttribute().value((int)value); System.out.println("The predicted value of instance "+ Integer.toString(s1)+ ": "+prediction);
public Evaluation classify(Classifier model, Instances trainingSet, Instances testingSet) throws Exception { Evaluation evaluation = new Evaluation(trainingSet); model.buildClassifier(trainingSet); evaluation.evaluateModel(model, testingSet); return evaluation; }
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return preedicted class probability distribution * @throws Exception if distribution can't be computed successfully */ public double[] distributionForInstance(Instance instance) throws Exception { return m_Classifier.distributionForInstance(instance); }
/** * configures the CheckClassifier instance used throughout the tests * * @return the fully configured CheckClassifier instance used for testing */ protected CheckClassifier getTester() { CheckClassifier result; result = super.getTester(); result.setNumInstances(40); return result; }
private boolean replaceStrings(Instances dataset) throws Exception { boolean nonDouble = false; for (int i = 0; i < m_size; i++) { for (int j = 0; j < m_size; j++) { if (getCell(i, j) instanceof String) { setCell(i, j, new InstanceExpression((String) getCell(i, j), dataset)); nonDouble = true; } else if (getCell(i, j) instanceof InstanceExpression) { nonDouble = true; } } } return nonDouble; }
/** * tests whether the scheme declares a serialVersionUID. */ public void testSerialVersionUID() { boolean[] result; result = m_Tester.declaresSerialVersionUID(); if (!result[0]) { fail("Doesn't declare serialVersionUID!"); } }
/** * tests whether the toString method of the classifier works even though the * classifier hasn't been built yet. */ public void testToString() { boolean[] result; result = m_Tester.testToString(); if (!result[0]) { fail("Error in toString() method!"); } }
/** * returns a custom PostProcessor for the CheckClassifier datasets.. * * @return a custom PostProcessor * @see AbsPostProcessor */ protected PostProcessor getPostProcessor() { return new AbsPostProcessor(); }
/** * Same as size * * @return the number of rows */ public int numRows() { return size(); }
/** * returns a string for the class type * * @param type the class type * @return the class type as string */ protected String getClassTypeString(int type) { return CheckClassifier.attributeTypeToString(type); }
/** * Returns the distribution for the given instance. * * @param instance the test instance * @return the distribution array * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance instance) throws Exception { return m_Classifier.distributionForInstance(instance); }
/** * configures the CheckClassifier instance used throughout the tests * * @return the fully configured CheckClassifier instance used for testing */ protected CheckClassifier getTester() { CheckClassifier result; result = super.getTester(); result.setNumInstances(40); return result; }
/** * tests whether the scheme declares a serialVersionUID. */ public void testSerialVersionUID() { boolean[] result; result = m_Tester.declaresSerialVersionUID(); if (!result[0]) { fail("Doesn't declare serialVersionUID!"); } }
/** * tests whether the toString method of the classifier works even though the * classifier hasn't been built yet. */ public void testToString() { boolean[] result; result = m_Tester.testToString(); if (!result[0]) { fail("Error in toString() method!"); } }
/** * returns a custom PostProcessor for the CheckClassifier datasets.. * * @return a custom PostProcessor * @see AbsPostProcessor */ protected PostProcessor getPostProcessor() { return new AbsPostProcessor(); }
/** * Returns the distribution for the given instance. * * @param instance the test instance * @return the distribution array * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance instance) throws Exception { return m_Classifier.distributionForInstance(instance); }
/** * configures the CheckClassifier instance used throughout the tests * * @return the fully configured CheckClassifier instance used for testing */ protected CheckClassifier getTester() { CheckClassifier result; result = super.getTester(); result.setNumInstances(60); return result; }
/** * returns a custom PostProcessor for the CheckClassifier datasets.. * * @return a custom PostProcessor * @see AbsPostProcessor */ protected PostProcessor getPostProcessor() { return new AbsPostProcessor(); }
/** * configures the CheckClassifier instance used throughout the tests * * @return the fully configured CheckClassifier instance used for testing */ protected CheckClassifier getTester() { CheckClassifier result; result = super.getTester(); result.setNumInstances(40); return result; }