Search in sources :

Example 1 with HierarchicalClustering

use of smile.clustering.HierarchicalClustering in project smile by haifengl.

the class NeuralMap method partition.

/**
     * Clustering neurons into k clusters.
     * @param k the number of clusters.
     * @param minPts a neuron will be treated as outlier if the number of its
     * points is less than minPts.
     * @return the number of non-outlier leaves.
     */
public int partition(int k, int minPts) {
    List<Neuron> data = new ArrayList<>();
    for (Neuron neuron : neurons) {
        neuron.y = OUTLIER;
        if (neuron.n >= minPts) {
            data.add(neuron);
        }
    }
    double[][] proximity = new double[data.size()][];
    for (int i = 0; i < data.size(); i++) {
        proximity[i] = new double[i + 1];
        for (int j = 0; j < i; j++) {
            proximity[i][j] = Math.distance(data.get(i).w, data.get(j).w);
        }
    }
    Linkage linkage = new UPGMALinkage(proximity);
    HierarchicalClustering hc = new HierarchicalClustering(linkage);
    int[] y = hc.partition(k);
    for (int i = 0; i < data.size(); i++) {
        data.get(i).y = y[i];
    }
    return data.size();
}
Also used : Linkage(smile.clustering.linkage.Linkage) UPGMALinkage(smile.clustering.linkage.UPGMALinkage) ArrayList(java.util.ArrayList) HierarchicalClustering(smile.clustering.HierarchicalClustering) UPGMALinkage(smile.clustering.linkage.UPGMALinkage)

Example 2 with HierarchicalClustering

use of smile.clustering.HierarchicalClustering in project smile by haifengl.

the class SOM method partition.

/**
     * Clustering the neurons into k groups. And then assigns the samples in
     * each neuron to the corresponding cluster.
     * @param k the number of clusters.
     * @return the cluster label of samples.
     */
public int[] partition(int k) {
    int n = width * height;
    double[][] units = new double[n][d];
    for (int i = 0, l = 0; i < height; i++) {
        for (int j = 0; j < width; j++, l++) {
            units[l] = neurons[i][j];
        }
    }
    double[][] proximity = new double[n][];
    for (int i = 0; i < n; i++) {
        proximity[i] = new double[i + 1];
        for (int j = 0; j < i; j++) {
            proximity[i][j] = Math.distance(units[i], units[j]);
        }
    }
    Linkage linkage = new UPGMALinkage(proximity);
    HierarchicalClustering hc = new HierarchicalClustering(linkage);
    y = hc.partition(k);
    int[] cluster = new int[bmu.length];
    for (int i = 0; i < cluster.length; i++) {
        cluster[i] = y[bmu[i][0] * width + bmu[i][1]];
    }
    return cluster;
}
Also used : Linkage(smile.clustering.linkage.Linkage) UPGMALinkage(smile.clustering.linkage.UPGMALinkage) HierarchicalClustering(smile.clustering.HierarchicalClustering) UPGMALinkage(smile.clustering.linkage.UPGMALinkage)

Example 3 with HierarchicalClustering

use of smile.clustering.HierarchicalClustering in project smile by haifengl.

the class GrowingNeuralGas method partition.

/**
     * Clustering neurons into k clusters.
     * @param k the number of clusters.
     */
public void partition(int k) {
    double[][] reps = new double[nodes.size()][];
    int i = 0;
    for (Node neuron : nodes) reps[i++] = neuron.w;
    double[][] proximity = new double[nodes.size()][];
    for (i = 0; i < nodes.size(); i++) {
        proximity[i] = new double[i + 1];
        for (int j = 0; j < i; j++) proximity[i][j] = Math.distance(reps[i], reps[j]);
    }
    Linkage linkage = new UPGMALinkage(proximity);
    HierarchicalClustering hc = new HierarchicalClustering(linkage);
    y = hc.partition(k);
}
Also used : Linkage(smile.clustering.linkage.Linkage) UPGMALinkage(smile.clustering.linkage.UPGMALinkage) HierarchicalClustering(smile.clustering.HierarchicalClustering) UPGMALinkage(smile.clustering.linkage.UPGMALinkage)

Example 4 with HierarchicalClustering

use of smile.clustering.HierarchicalClustering in project smile by haifengl.

the class HierarchicalClusteringDemo method learn.

@Override
public JComponent learn() {
    long clock = System.currentTimeMillis();
    double[][] data = dataset[datasetIndex];
    int n = data.length;
    double[][] proximity = new double[n][];
    for (int i = 0; i < n; i++) {
        proximity[i] = new double[i + 1];
        for (int j = 0; j < i; j++) proximity[i][j] = Math.distance(data[i], data[j]);
    }
    HierarchicalClustering hac = null;
    switch(linkageBox.getSelectedIndex()) {
        case 0:
            hac = new HierarchicalClustering(new SingleLinkage(proximity));
            break;
        case 1:
            hac = new HierarchicalClustering(new CompleteLinkage(proximity));
            break;
        case 2:
            hac = new HierarchicalClustering(new UPGMALinkage(proximity));
            break;
        case 3:
            hac = new HierarchicalClustering(new WPGMALinkage(proximity));
            break;
        case 4:
            hac = new HierarchicalClustering(new UPGMCLinkage(proximity));
            break;
        case 5:
            hac = new HierarchicalClustering(new WPGMCLinkage(proximity));
            break;
        case 6:
            hac = new HierarchicalClustering(new WardLinkage(proximity));
            break;
        default:
            throw new IllegalStateException("Unsupported Linkage");
    }
    System.out.format("Hierarchical clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    int[] membership = hac.partition(clusterNumber);
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < membership.length; i++) {
        clusterSize[membership[i]]++;
    }
    JPanel pane = new JPanel(new GridLayout(1, 3));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.setTitle("Data");
    pane.add(plot);
    for (int k = 0; k < clusterNumber; k++) {
        double[][] cluster = new double[clusterSize[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (membership[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    plot = Dendrogram.plot("Dendrogram", hac.getTree(), hac.getHeight());
    plot.setTitle("Dendrogram");
    pane.add(plot);
    return pane;
}
Also used : WPGMCLinkage(smile.clustering.linkage.WPGMCLinkage) JPanel(javax.swing.JPanel) CompleteLinkage(smile.clustering.linkage.CompleteLinkage) WardLinkage(smile.clustering.linkage.WardLinkage) HierarchicalClustering(smile.clustering.HierarchicalClustering) GridLayout(java.awt.GridLayout) SingleLinkage(smile.clustering.linkage.SingleLinkage) WPGMALinkage(smile.clustering.linkage.WPGMALinkage) UPGMALinkage(smile.clustering.linkage.UPGMALinkage) UPGMCLinkage(smile.clustering.linkage.UPGMCLinkage) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

HierarchicalClustering (smile.clustering.HierarchicalClustering)4 UPGMALinkage (smile.clustering.linkage.UPGMALinkage)4 Linkage (smile.clustering.linkage.Linkage)3 GridLayout (java.awt.GridLayout)1 ArrayList (java.util.ArrayList)1 JPanel (javax.swing.JPanel)1 CompleteLinkage (smile.clustering.linkage.CompleteLinkage)1 SingleLinkage (smile.clustering.linkage.SingleLinkage)1 UPGMCLinkage (smile.clustering.linkage.UPGMCLinkage)1 WPGMALinkage (smile.clustering.linkage.WPGMALinkage)1 WPGMCLinkage (smile.clustering.linkage.WPGMCLinkage)1 WardLinkage (smile.clustering.linkage.WardLinkage)1 PlotCanvas (smile.plot.PlotCanvas)1