MOA Notebook Example

This is an example of a MOA Notebook in Java.

Prequential Evaluation Example

Let’s run a very simple experiment: using a decision tree (Hoeffding Tree) with data generated from an artificial stream generator (RandomRBFGenerator).

We should start importing the classes that we need, and defining the stream and the learner.

In [7]:
%maven nz.ac.waikato.cms.moa:moa:2018.6.0

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

HoeffdingTree learner = new HoeffdingTree();
RandomRBFGenerator stream = new RandomRBFGenerator();

Now, we need to initialize the stream and the classifier:

In [9]:
stream.prepareForUse();
learner.setModelContext(stream.getHeader());
learner.prepareForUse();

And finally, let’s run a prequential evaluation, as in Tutorial 2 (Introduction to the API of MOA).

In [21]:
%maven org.knowm.xchart:xchart:3.5.2
import org.knowm.xchart.*;
import moa.core.TimingUtils;
import com.yahoo.labs.samoa.instances.Instance;

int numInstances = 1000000;
int sampleSize = 1000;
boolean isTesting = true;
double[] xData = new double[numInstances/sampleSize];
double[] yData = new double[numInstances/sampleSize];

int numberSamplesCorrect = 0;
int numberSamples = 0;
boolean preciseCPUTiming = TimingUtils.enablePreciseTiming();
long evaluateStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
while (stream.hasMoreInstances() && numberSamples < numInstances) {
    Instance trainInst = stream.nextInstance().getData();
    if (isTesting) {
            if (learner.correctlyClassifies(trainInst)){
                    numberSamplesCorrect++;
            }
    }
    if (numberSamples % sampleSize == 0){
        xData[numberSamples / sampleSize] = numberSamples / sampleSize;
        yData[numberSamples / sampleSize] = 100.0 * (double) numberSamplesCorrect/ (double) numberSamples;
    }
    numberSamples++;
    learner.trainOnInstance(trainInst);
}
double accuracy = 100.0 * (double) numberSamplesCorrect/ (double) numberSamples;
double time = TimingUtils.nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread()- evaluateStartTime);
System.out.println(numberSamples + " instances processed with " + accuracy + "% accuracy in "+time+" seconds.");

XYChart chart = QuickChart.getChart("Prequential Evaluation", "#Instances", "Accuracy", "y(x)", xData, yData);
BitmapEncoder.getBufferedImage(chart);
1000000 instances processed with 91.0458% accuracy in -2.857338446 seconds.
Out[21]:

And, we can also run a prequential Evaluation task directly.

In [13]:
import moa.DoTask;
DoTask.main("EvaluatePrequential -l trees.HoeffdingTree -i 1000000".split(" "));
{M}assive {O}nline {A}nalysis
Version:  18.06 June 2018
Copyright: (C) 2007-2018 University of Waikato, Hamilton, New Zealand
Web: http://moa.cms.waikato.ac.nz/

                                                                               
Task completed in 4.50s (CPU time)

learning evaluation instances,evaluation time (cpu seconds),model cost (RAM-Hours),classified instances,classifications correct (percent),Kappa Statistic (percent),Kappa Temporal Statistic (percent),Kappa M Statistic (percent),model training instances,model serialized size (bytes),tree size (nodes),tree size (leaves),active learning leaves,tree depth,active leaf byte size estimate,inactive leaf byte size estimate,byte size estimate overhead
100000.0,0.514551649,0.0,100000.0,92.10000000000001,84.09118369648397,82.93736501079914,82.63736263736264,100000.0,0.0,187.0,118.0,118.0,5.0,0.0,0.0,1.0
200000.0,0.898160024,0.0,200000.0,93.2,86.13619960610498,85.15283842794761,84.29561200923789,200000.0,0.0,290.0,180.0,180.0,6.0,0.0,0.0,1.0
300000.0,1.260566831,0.0,300000.0,93.7,87.0415165128104,86.76470588235296,85.14150943396228,300000.0,0.0,368.0,228.0,228.0,6.0,0.0,0.0,1.0
400000.0,1.676657529,0.0,400000.0,95.1,90.00701548300785,90.18036072144288,88.57808857808857,400000.0,0.0,489.0,311.0,311.0,7.0,0.0,0.0,1.0
500000.0,2.12164797,0.0,500000.0,94.8,89.19332313626387,88.9596602972399,87.12871287128712,500000.0,0.0,598.0,370.0,370.0,7.0,0.0,0.0,1.0
600000.0,2.569584639,0.0,600000.0,95.6,91.0510537384223,91.6030534351145,89.69555035128805,600000.0,0.0,687.0,428.0,428.0,7.0,0.0,0.0,1.0
700000.0,2.984601928,0.0,700000.0,96.1,91.99382498090834,91.82389937106917,90.8450704225352,700000.0,0.0,792.0,497.0,497.0,8.0,0.0,0.0,1.0
800000.0,3.430721144,0.0,800000.0,95.39999999999999,90.62666835114945,90.49586776859503,89.37644341801385,800000.0,0.0,924.0,584.0,584.0,8.0,0.0,0.0,1.0
900000.0,3.861493675,0.0,900000.0,96.8,93.50965438909621,93.73776908023483,92.72727272727272,900000.0,0.0,1020.0,647.0,647.0,8.0,0.0,0.0,1.0
1000000.0,4.413296784,0.0,1000000.0,96.7,93.26222599719055,93.25153374233128,92.28971962616822,1000000.0,0.0,1124.0,720.0,720.0,9.0,0.0,0.0,?
In [ ]:

In [ ]: