Search in sources :

Example 1 with GrowingNeuralGas

use of smile.vq.GrowingNeuralGas in project smile by haifengl.

the class GrowingNeuralGasDemo method learn.

@Override
public JComponent learn() {
    long clock = System.currentTimeMillis();
    GrowingNeuralGas gas = new GrowingNeuralGas(2, 0.05, 0.0006, 88, 200, 0.5, 0.9995);
    for (int loop = 0; loop < 25; loop++) {
        for (double[] x : dataset[datasetIndex]) {
            gas.update(x);
        }
    }
    gas.partition(clusterNumber);
    System.out.format("Growing Neural Gas clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    int[] membership = new int[dataset[datasetIndex].length];
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < dataset[datasetIndex].length; i++) {
        membership[i] = gas.predict(dataset[datasetIndex][i]);
        clusterSize[membership[i]]++;
    }
    GrowingNeuralGas.Neuron[] neurons = gas.neurons();
    double[][] x = new double[neurons.length][];
    for (int i = 0; i < x.length; i++) x[i] = neurons[i].w;
    PlotCanvas plot = ScatterPlot.plot(x, '@');
    for (int k = 0; k < clusterNumber; k++) {
        if (clusterSize[k] > 0) {
            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]);
        }
    }
    for (int i = 0; i < neurons.length; i++) {
        for (int j = 0; j < neurons[i].neighbors.length; j++) {
            plot.line(neurons[i].w, neurons[i].neighbors[j].w);
        }
    }
    plot.points(x, '@');
    return plot;
}
Also used : GrowingNeuralGas(smile.vq.GrowingNeuralGas) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

PlotCanvas (smile.plot.PlotCanvas)1 GrowingNeuralGas (smile.vq.GrowingNeuralGas)1