1

我是使用moa的新手,我很难尝试解码必须如何使用聚类算法。文档缺少常见用法的示例代码,并且没有用注释很好地解释实现......也没有找到任何教程。

所以,这是我的代码:

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);

    }
}

这是我得到的错误:

在此处输入图像描述

任何有关如何使用该算法的见解都将受到赞赏。谢谢!

4

1 回答 1

0

我已经更新了代码。正如我在 github 中提到的那样,它正在工作,您必须将标头分配给您的实例。见 github 讨论

这是更新的代码:

    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);
    }

这是调试过程的屏幕截图,您可以看到生成的聚类结果:

调试截图 图片链接坏了,你可以在github上找到它github入口链接

于 2019-11-18T07:54:38.610 回答