use of smile.math.distance.EuclideanDistance 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.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testTest_4args_2.
/**
* Test of test method, of class Validation.
*/
@Test
public void testTest_4args_2() {
System.out.println("test");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
Math.standardize(datax);
int n = datax.length;
int m = 3 * n / 4;
double[][] x = new double[m][];
double[] y = new double[m];
double[][] testx = new double[n - m][];
double[] testy = new double[n - m];
int[] index = Math.permutate(n);
for (int i = 0; i < m; i++) {
x[i] = datax[index[i]];
y[i] = datay[index[i]];
}
for (int i = m; i < n; i++) {
testx[i - m] = datax[index[i]];
testy[i - m] = datay[index[i]];
}
double[][] centers = new double[20][];
RadialBasisFunction[] rbf = SmileUtils.learnGaussianRadialBasis(x, centers, 2);
RBFNetwork<double[]> rkhs = new RBFNetwork<>(x, y, new EuclideanDistance(), rbf, centers);
RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
double[] results = Validation.test(rkhs, testx, testy, measures);
System.out.println("RMSE = " + results[0]);
System.out.println("Absolute Deviation = " + results[1]);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testLoocv_4args_2.
/**
* Test of loocv method, of class Validation.
*/
@Test
public void testLoocv_4args_2() {
System.out.println("loocv");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
Math.standardize(x);
RBFNetwork.Trainer<double[]> trainer = new RBFNetwork.Trainer<>(new EuclideanDistance());
trainer.setNumCenters(20);
RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
double[] results = Validation.loocv(trainer, x, y, measures);
System.out.println("RMSE = " + results[0]);
System.out.println("Absolute Deviation = " + results[1]);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testLoocv_3args_2.
/**
* Test of loocv method, of class Validation.
*/
@Test
public void testLoocv_3args_2() {
System.out.println("loocv");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
Math.standardize(x);
RBFNetwork.Trainer<double[]> trainer = new RBFNetwork.Trainer<>(new EuclideanDistance());
trainer.setNumCenters(20);
double rmse = Validation.loocv(trainer, x, y);
System.out.println("RMSE = " + rmse);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.distance.EuclideanDistance in project smile by haifengl.
the class ValidationTest method testCv_5args_2.
/**
* Test of cv method, of class Validation.
*/
@Test
public void testCv_5args_2() {
System.out.println("cv");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
Math.standardize(x);
RBFNetwork.Trainer<double[]> trainer = new RBFNetwork.Trainer<>(new EuclideanDistance());
trainer.setNumCenters(20);
RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
double[] results = Validation.cv(10, trainer, x, y, measures);
System.out.println("RMSE = " + results[0]);
System.out.println("Absolute Deviation = " + results[1]);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations