列表<簇<doublepoint>&GT;设置<doublepoint>

时间:2017-02-02 15:02:04

标签: java data-conversion dbscan

我正在尝试使用DBSCANClusterer(apache.math3)对我生成的一组点进行排序并将其写入文件。此时,我被困在这里:

public Set<DoublePoint> DBSCAN(Set<DoublePoint> set2) { 
        Set<DoublePoint> points = new Set<DoublePoint>();
        DBSCANClusterer<DoublePoint> dbscan = new DBSCANClusterer<DoublePoint>(1, 15);
        //run dbscan on set of points
        List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
        **sorted = clusters???**

如何将List<Cluster<DoublePoint>> clusters分配给Set<DoublePoint> sorted?我猜它应该像2D-&gt; 1D!

以下是我的其余代码:

    import java.awt.Point;
    import java.io.*;
    import java.util.*;
    import org.apache.commons.math3.ml.clustering.Cluster;
    import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
    import org.apache.commons.math3.ml.clustering.DoublePoint;
    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;


public class Main {

    public static void main(final String[] args) throws Exception {
        new Main().run(); 
    }

    public void run() {
        Set<DoublePoint> set = generateSetPoints();
        try {
            writeToFile(set, "points");
        } catch(IOException ex) {
            System.out.println("IO Exception while writing to file");
        }

        Set<DoublePoint> set_by_dbscan = dbScan(set);//
        try {
            writeToFile(set_by_dbscan, "by_dbscan");
        } catch(IOException ex) {
            System.out.println("IO Exception while writing to file"); 
        }

    }

    public Set<DoublePoint> generateSetPoints() { 
        int xx=100;
        int yy=100;
        Set<DoublePoint> set = new HashSet<>(); 
        Random rnd = new Random(); 
        int number=100;
        do{
            int tmp[] = new int[2];

            tmp[0] =   rnd.nextInt(xx); 
            tmp[1] =   rnd.nextInt(yy); 
            DoublePoint rndpoint    =   new DoublePoint(tmp);
            set.add(rndpoint);
        }
        while (set.size()<number);
        return set;
    }

    public void writeToFile(Set<DoublePoint> set, String filename) throws IOException { 
        File fout = new File( filename + ".txt");
        FileOutputStream fos = new FileOutputStream(fout);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

        for (DoublePoint p: set) {
            bw.write(p.getPoint()[0] + "," + p.getPoint()[1]);
            bw.newLine();
        }

        bw.close();
    }

    public Set<DoublePoint> dbScan(Set<DoublePoint> set2) { 
        Set<DoublePoint> points = new Set<DoublePoint>();
        DBSCANClusterer dbscan = new DBSCANClusterer(1, 15);
        List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
        return clusters;
    }
}

1 个答案:

答案 0 :(得分:0)

HashSet是未排序的数据结构。

如果您希望对<ComboBox x:Name="ExampleComboBox" Height="24" Width="200" HorizontalContentAlignment="Stretch"> <ComboBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="10" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding FirstName}" /> <TextBlock Grid.Column="2" Text="{Binding LastName}" /> <TextBlock Grid.Column="4" HorizontalAlignment="Right" Text="{Binding Country}" /> </Grid> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 进行排序,请使用保留订单的内容。

sorted是一个真正的WTF ...

此外,DBSCAN不适用于排序。改为使用OPTICS聚类。