java.lang.NullPointerException when trying MOA stream clustering algorithm denstream.WithDBSCAN (How to properly use it?)

173 views Asked by At

I am new into using moa and I am having a hard time trying to decode how the clustering algorithms have to be used. The documentation lacks of sample code for common usages, and the implementation is not well explained with comments ... have not found any tutorial either.

So, here is my code:

import com.yahoo.labs.samoa.instances.DenseInstance;
import moa.cluster.Clustering;
import moa.clusterers.denstream.WithDBSCAN;

public class TestingDenstream {
    static DenseInstance randomInstance(int size) {
        DenseInstance instance = new DenseInstance(size);
        for (int idx = 0; idx < size; idx++) {
            instance.setValue(idx, Math.random());
        }
        return instance;
    }
    public static void main(String[] args) {
        WithDBSCAN withDBSCAN = new WithDBSCAN();
        withDBSCAN.resetLearningImpl();
        for (int i = 0; i < 10; i++) {
            DenseInstance d = randomInstance(2);
            withDBSCAN.trainOnInstanceImpl(d);
        }
        Clustering clusteringResult = withDBSCAN.getClusteringResult();
        Clustering microClusteringResult = withDBSCAN.getMicroClusteringResult();

        System.out.println(clusteringResult);

    }
}

And here is the error I get:

enter image description here

Any insights into how the algorithm has to be used will be appreciated. Thanks!

1

There are 1 answers

4
Celik On BEST ANSWER

I have updated the code. It is working as I mentioned in the github, you have to assign header to your instance. See the github discussion

here is the updated code:

    static DenseInstance randomInstance(int size) {
        // generates the name of the features which is called as InstanceHeader
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        for (int i = 0; i < size; i++) {
            attributes.add(new Attribute("feature_" + i));
        }
        // create instance header with generated feature name
        InstancesHeader streamHeader = new InstancesHeader(
                new Instances("Mustafa Çelik Instance",attributes, size));

        // generates random data
        double[] data = new double[2];
        Random random = new Random();
        for (int i = 0; i < 2; i++) {
            data[i] = random.nextDouble();
        }

        // creates an instance and assigns the data
        DenseInstance inst = new DenseInstance(1.0, data);

        // assigns the instanceHeader(feature name)
        inst.setDataset(streamHeader);

        return inst;
    }

    public static void main(String[] args) {
        WithDBSCAN withDBSCAN = new WithDBSCAN();
        withDBSCAN.resetLearningImpl();
        withDBSCAN.initialDBScan();
        for (int i = 0; i < 1500; i++) {
            DenseInstance d = randomInstance(5);

            withDBSCAN.trainOnInstanceImpl(d);
        }
        Clustering clusteringResult = withDBSCAN.getClusteringResult();
        Clustering microClusteringResult = withDBSCAN.getMicroClusteringResult();

        System.out.println(clusteringResult);
    }

here is the screenshot of debug process, as you see the clustering result is generated:

debug screenshot image link is broken, you can find it on github github entry link