Java WEKA HierarchycalClusterer类始终返回2个集群

Java WEKA HierarchycalClusterer类始终返回2个集群,java,cluster-analysis,weka,data-mining,hierarchical-clustering,Java,Cluster Analysis,Weka,Data Mining,Hierarchical Clustering,这是我的密码: import weka.clusterers.ClusterEvaluation; import weka.clusterers.HierarchicalClusterer; import weka.clusterers.EM; import weka.core.converters.CSVLoader; import weka.core.converters.ConverterUtils.DataSource; import weka.core.neighboursearch.

这是我的密码:

import weka.clusterers.ClusterEvaluation;
import weka.clusterers.HierarchicalClusterer;
import weka.clusterers.EM;
import weka.core.converters.CSVLoader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.neighboursearch.PerformanceStats;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Enumeration;

import weka.core.*;

public class WEKASample1 {

public static void main(String[] args) {

    Instances data = null;
    CSVLoader csvLoader = new CSVLoader();
    try {
        csvLoader.setSource(new File("D:\\WEKA\\numbers.csv"));

        data = csvLoader.getDataSet();
                HierarchicalClusterer h = new HierarchicalClusterer();

            DistanceFunction d = new DistanceFunction() {

        @Override
        public void setOptions(String[] arg0) throws Exception {

        }

        @Override
        public Enumeration listOptions() {
            return null;
        }

        @Override
        public String[] getOptions() {
            return null;
        }

        @Override
        public void update(Instance arg0) {

        }

        @Override
        public void setInvertSelection(boolean arg0) {

        }

        @Override
        public void setInstances(Instances arg0) {

        }

        @Override
        public void setAttributeIndices(String arg0) {

        }

        @Override
        public void postProcessDistances(double[] arg0) {

        }

        @Override
        public boolean getInvertSelection() {
            return false;
        }

        @Override
        public Instances getInstances() {
            return null;
        }

        @Override
        public String getAttributeIndices() {
            return null;
        }

        @Override
        public double distance(Instance arg0, Instance arg1, double arg2,
                PerformanceStats arg3) {
            return 0;
        }

        @Override
        public double distance(Instance arg0, Instance arg1, double arg2) {
            return 0;
        }

        @Override
        public double distance(Instance arg0, Instance arg1, PerformanceStats arg2)
                throws Exception {
            return 0;
        }

        @Override
        public double distance(Instance arg0, Instance arg1) {

            double s1 = arg0.value(0);
            double s2 = arg1.value(0);

            return Double.POSITIVE_INFINITY;
        }
    };

    h.setDistanceFunction(d);
    SelectedTag s = new SelectedTag(1, HierarchicalClusterer.TAGS_LINK_TYPE);
    h.setLinkType(s);

    h.buildClusterer(data);


//      double[] arr;
//      for(int i=0; i<data.size(); i++) {
//          
//          arr = h.distributionForInstance(data.get(i));
//          for(int j=0; j< arr.length; j++)
//              System.out.print(arr[j]+",");
//          System.out.println();
//          
//      }

        System.out.println(h.numberOfClusters());
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

尝试真实的数据集,而不是均匀分布的点阵列

因为他们都有相同的距离到下一个!对于单链路,这应该是单个集群,但可能存在一些舍入问题

另外,您使用的距离函数也是0/无穷大


首先尝试使用Weka UI。

谢谢。但是这里写的距离函数告诉WEKA不要通过返回一个无穷大的值来聚集任何两个实例,对吗?我打赌这不会对WEKA说“不要聚集”。它可能只是尽可能地在顶层对它们进行集群。他们可能不会测试无穷大。谢谢。我现在修改了WEKA HierarchycalClusterer代码,以便在不满足特定条件时停止聚类过程。WEKA Hierarchy Clusterer可以使用停止阈值。但我猜这是一个
O(n^3)
实现,即使是对于单链接、平均链接和完整链接,据我所知,
O(n^2)
算法是存在的。WEKA在聚类方面不是很强。
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10   10