Search in sources :

Example 1 with Linkage

use of smile.clustering.linkage.Linkage 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 Linkage

use of smile.clustering.linkage.Linkage 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 Linkage

use of smile.clustering.linkage.Linkage 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 Linkage

use of smile.clustering.linkage.Linkage in project smile by haifengl.

the class BIRCH method partition.

/**
     * Clustering leaves of CF tree into k clusters.
     * @param k the number of clusters.
     * @param minPts a CF leaf 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) {
    ArrayList<Leaf> leaves = new ArrayList<>();
    ArrayList<double[]> centers = new ArrayList<>();
    Queue<Node> queue = new LinkedList<>();
    queue.offer(root);
    for (Node node = queue.poll(); node != null; node = queue.poll()) {
        if (node.numChildren == 0) {
            if (node.n >= minPts) {
                double[] x = new double[d];
                for (int i = 0; i < d; i++) {
                    x[i] = node.sum[i] / node.n;
                }
                centers.add(x);
                leaves.add((Leaf) node);
            } else {
                Leaf leaf = (Leaf) node;
                leaf.y = OUTLIER;
            }
        } else {
            for (int i = 0; i < node.numChildren; i++) {
                queue.offer(node.children[i]);
            }
        }
    }
    int n = centers.size();
    centroids = centers.toArray(new double[n][]);
    if (n > k) {
        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(centroids[i], centroids[j]);
            }
        }
        Linkage linkage = new WardLinkage(proximity);
        HierarchicalClustering hc = new HierarchicalClustering(linkage);
        int[] y = hc.partition(k);
        for (int i = 0; i < n; i++) {
            leaves.get(i).y = y[i];
        }
    } else {
        for (int i = 0; i < n; i++) {
            leaves.get(i).y = i;
        }
    }
    return n;
}
Also used : Linkage(smile.clustering.linkage.Linkage) WardLinkage(smile.clustering.linkage.WardLinkage) ArrayList(java.util.ArrayList) WardLinkage(smile.clustering.linkage.WardLinkage) LinkedList(java.util.LinkedList)

Aggregations

Linkage (smile.clustering.linkage.Linkage)4 HierarchicalClustering (smile.clustering.HierarchicalClustering)3 UPGMALinkage (smile.clustering.linkage.UPGMALinkage)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)1 WardLinkage (smile.clustering.linkage.WardLinkage)1