Java vs Matab code

Since Matlab uses JRE pretty extensively, it is very easy to write Matlab code which is equivalent to the java code.

This is the original java code.

int numInstances = 10000;
Classifierlearner = new HoeffdingTree();
RandomRBFGenerator stream = new RandomRBFGenerator();
stream.prepareForUse();
learner.setModelContext(stream.getHeader());
learner.prepareForUse();
int numberSamplesCorrect = 0;
int numberSamples = 0;
boolean isTesting = true;
while (stream.hasMoreInstances() && numberSamples < numInstances) {
	Instance trainInst = stream.nextInstance();
	if (isTesting) {
		if (learner.correctlyClassifies(trainInst)) {
			numberSamplesCorrect++;
		}
	}
	numberSamples++;
	learner.trainOnInstance(trainInst);
}
double accuracy = 100.0 * (double) numberSamplesCorrect / (double) numberSamples;
System.out.println(numberSamples + " instances processed with " + accuracy + "% accuracy ");

And here is the corresponding Matlab code.

clear; close all;clc;
javaclasspath('C:\Users\Ninad\Desktop\moa-release-2014 .11\moa.jar');

import moa.classifiers.trees.HoeffdingTree;
import moa.streams.generators.RandomRBFGenerator;

numInstances =10000;
learner=HoeffdingTree;
stream=RandomRBFGenerator;

stream.prepareForUse;
learner.setModelContext(stream.getHeader);
learner.prepareForUse;

numberSamplesCorrect=0;
numberSamples=0;
isTesting=true;

while(stream.hasMoreInstances && numberSamples<numInstances)
    trainInst=stream.nextInstance;
    if(isTesting)
        if(learner.correctlyClassifies(trainInst))
            numberSamplesCorrect=numberSamplesCorrect+1;
        end
    end
    numberSamples=numberSamples+1;
    learner.trainOnInstance(trainInst);
end

accuracy=numberSamplesCorrect/numberSamples;
fprintf('%d instances processed with %f accuracy\n',numberSamples,accuracy);

Leave a Reply

Your email address will not be published.