Search in sources :

Example 1 with EuclideanDistance

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;
}
Also used : CLARANS(smile.clustering.CLARANS) EuclideanDistance(smile.math.distance.EuclideanDistance) PlotCanvas(smile.plot.PlotCanvas)

Example 2 with EuclideanDistance

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);
    }
}
Also used : RadialBasisFunction(smile.math.rbf.RadialBasisFunction) AttributeDataset(smile.data.AttributeDataset) RBFNetwork(smile.regression.RBFNetwork) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) Test(org.junit.Test)

Example 3 with EuclideanDistance

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);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) Test(org.junit.Test)

Example 4 with EuclideanDistance

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);
    }
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) Test(org.junit.Test)

Example 5 with EuclideanDistance

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);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) RBFNetwork(smile.regression.RBFNetwork) EuclideanDistance(smile.math.distance.EuclideanDistance) ArffParser(smile.data.parser.ArffParser) Test(org.junit.Test)

Aggregations

EuclideanDistance (smile.math.distance.EuclideanDistance)29 Test (org.junit.Test)22 AttributeDataset (smile.data.AttributeDataset)19 ArffParser (smile.data.parser.ArffParser)14 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)11 RBFNetwork (smile.regression.RBFNetwork)8 ClassifierTrainer (smile.classification.ClassifierTrainer)6 PlotCanvas (smile.plot.PlotCanvas)6 ArrayList (java.util.ArrayList)5 NominalAttribute (smile.data.NominalAttribute)5 DelimitedTextParser (smile.data.parser.DelimitedTextParser)5 CrossValidation (smile.validation.CrossValidation)4 CoverTree (smile.neighbor.CoverTree)3 KDTree (smile.neighbor.KDTree)3 LSH (smile.neighbor.LSH)3 LinearSearch (smile.neighbor.LinearSearch)3 MPLSH (smile.neighbor.MPLSH)3 Neighbor (smile.neighbor.Neighbor)2 AdjustedRandIndex (smile.validation.AdjustedRandIndex)2 LOOCV (smile.validation.LOOCV)2