use of smile.clustering.CLARANS in project smile by haifengl.
the class CLARANSDemo method learn.
@Override
public JComponent learn() {
try {
numLocal = Integer.parseInt(numLocalField.getText().trim());
if (numLocal < 5) {
JOptionPane.showMessageDialog(this, "Toll smal NumLocal: " + numLocal, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid NumLocal: " + numLocalField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
try {
maxNeighbor = Integer.parseInt(maxNeighborField.getText().trim());
if (maxNeighbor < 5) {
JOptionPane.showMessageDialog(this, "Too small MaxNeighbor: " + maxNeighbor, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid MaxNeighbor: " + maxNeighborField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
CLARANS<double[]> clarans = new CLARANS<>(dataset[datasetIndex], new EuclideanDistance(), clusterNumber, maxNeighbor, numLocal);
System.out.format("CLARANS clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(clarans.medoids(), '@');
for (int k = 0; k < clusterNumber; k++) {
if (clarans.getClusterSize()[k] > 0) {
double[][] cluster = new double[clarans.getClusterSize()[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (clarans.getClusterLabel()[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(clarans.medoids(), '@');
return plot;
}
use of smile.clustering.CLARANS in project smile by haifengl.
the class SmileUtils method learnGaussianRadialBasis.
/**
* Learns Gaussian RBF function and centers from data. The centers are
* chosen as the medoids of CLARANS. The standard deviation (i.e. width)
* of Gaussian radial basis function is estimated as the width of each
* cluster multiplied with a given scaling parameter r.
* @param x the training dataset.
* @param centers an array to store centers on output. Its length is used as k of CLARANS.
* @param distance the distance functor.
* @param r the scaling parameter.
* @return Gaussian RBF functions with parameter learned from data.
*/
public static <T> GaussianRadialBasis[] learnGaussianRadialBasis(T[] x, T[] centers, Metric<T> distance, double r) {
if (r <= 0.0) {
throw new IllegalArgumentException("Invalid scaling parameter: " + r);
}
int k = centers.length;
CLARANS<T> clarans = new CLARANS<>(x, distance, k, Math.min(100, (int) Math.round(0.01 * k * (x.length - k))));
System.arraycopy(clarans.medoids(), 0, centers, 0, k);
int n = x.length;
int[] y = clarans.getClusterLabel();
double[] sigma = new double[k];
for (int i = 0; i < n; i++) {
sigma[y[i]] += Math.sqr(distance.d(x[i], centers[y[i]]));
}
int[] ni = clarans.getClusterSize();
GaussianRadialBasis[] rbf = new GaussianRadialBasis[k];
for (int i = 0; i < k; i++) {
if (ni[i] >= 5 || sigma[i] == 0.0) {
sigma[i] = Math.sqrt(sigma[i] / ni[i]);
} else {
sigma[i] = Double.POSITIVE_INFINITY;
for (int j = 0; j < k; j++) {
if (i != j) {
double d = distance.d(centers[i], centers[j]);
if (d < sigma[i]) {
sigma[i] = d;
}
}
}
sigma[i] /= 2.0;
}
rbf[i] = new GaussianRadialBasis(r * sigma[i]);
}
return rbf;
}
use of smile.clustering.CLARANS in project smile by haifengl.
the class SmileUtils method learnGaussianRadialBasis.
/**
* Learns Gaussian RBF function and centers from data. The centers are
* chosen as the medoids of CLARANS. The standard deviation (i.e. width)
* of Gaussian radial basis function is estimated by the p-nearest neighbors
* (among centers, not all samples) heuristic. A suggested value for
* p is 2.
* @param x the training dataset.
* @param centers an array to store centers on output. Its length is used as k of CLARANS.
* @param distance the distance functor.
* @param p the number of nearest neighbors of centers to estimate the width
* of Gaussian RBF functions.
* @return Gaussian RBF functions with parameter learned from data.
*/
public static <T> GaussianRadialBasis[] learnGaussianRadialBasis(T[] x, T[] centers, Metric<T> distance, int p) {
if (p < 1) {
throw new IllegalArgumentException("Invalid number of nearest neighbors: " + p);
}
int k = centers.length;
CLARANS<T> clarans = new CLARANS<>(x, distance, k, Math.min(100, (int) Math.round(0.01 * k * (x.length - k))));
System.arraycopy(clarans.medoids(), 0, centers, 0, k);
p = Math.min(p, k - 1);
double[] r = new double[k];
GaussianRadialBasis[] rbf = new GaussianRadialBasis[k];
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
r[j] = distance.d(centers[i], centers[j]);
}
Arrays.sort(r);
double r0 = 0.0;
for (int j = 1; j <= p; j++) {
r0 += r[j];
}
r0 /= p;
rbf[i] = new GaussianRadialBasis(r0);
}
return rbf;
}
Aggregations