Search in sources :

Example 1 with GaussianRadialBasis

use of smile.math.rbf.GaussianRadialBasis in project smile by haifengl.

the class RBFInterpolationTest method testInterpolate.

/**
     * Test of interpolate method, of class RbfInterpolation.
     */
@Test
public void testInterpolate() {
    System.out.println("interpolate");
    double[][] x = { { 0, 0 }, { 1, 1 } };
    double[] y = { 0, 1 };
    RBFInterpolation instance = new RBFInterpolation(x, y, new GaussianRadialBasis());
    double[] x1 = { 0.5, 0.5 };
    assertEquals(0, instance.interpolate(x[0]), 1E-7);
    assertEquals(1, instance.interpolate(x[1]), 1E-7);
    assertEquals(0.569349, instance.interpolate(x1), 1E-6);
}
Also used : GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis) Test(org.junit.Test)

Example 2 with GaussianRadialBasis

use of smile.math.rbf.GaussianRadialBasis in project smile by haifengl.

the class SmileUtils method learnGaussianRadialBasis.

/**
     * Learns Gaussian RBF function and centers from data. The centers are
     * chosen as the centroids of K-Means. 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 k-means.
     * @param r the scaling parameter.
     * @return Gaussian RBF functions with parameter learned from data.
     */
public static GaussianRadialBasis[] learnGaussianRadialBasis(double[][] x, double[][] centers, double r) {
    if (r <= 0.0) {
        throw new IllegalArgumentException("Invalid scaling parameter: " + r);
    }
    int k = centers.length;
    KMeans kmeans = new KMeans(x, k, 10);
    System.arraycopy(kmeans.centroids(), 0, centers, 0, k);
    int n = x.length;
    int[] y = kmeans.getClusterLabel();
    double[] sigma = new double[k];
    for (int i = 0; i < n; i++) {
        sigma[y[i]] += Math.squaredDistance(x[i], centers[y[i]]);
    }
    int[] ni = kmeans.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 = Math.distance(centers[i], centers[j]);
                    if (d < sigma[i]) {
                        sigma[i] = d;
                    }
                }
            }
            sigma[i] /= 2.0;
        }
        rbf[i] = new GaussianRadialBasis(r * sigma[i]);
    }
    return rbf;
}
Also used : GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis) KMeans(smile.clustering.KMeans)

Example 3 with GaussianRadialBasis

use of smile.math.rbf.GaussianRadialBasis in project smile by haifengl.

the class SmileUtils method learnGaussianRadialBasis.

/**
     * Learns Gaussian RBF function and centers from data. The centers are
     * chosen as the centroids of K-Means. Let d<sub>max</sub> be the maximum
     * distance between the chosen centers, the standard deviation (i.e. width)
     * of Gaussian radial basis function is d<sub>max</sub> / sqrt(2*k), where
     * k is number of centers. This choice would be close to the optimal
     * solution if the data were uniformly distributed in the input space,
     * leading to a uniform distribution of centroids.
     * @param x the training dataset.
     * @param centers an array to store centers on output. Its length is used as k of k-means.
     * @return a Gaussian RBF function with parameter learned from data.
     */
public static GaussianRadialBasis learnGaussianRadialBasis(double[][] x, double[][] centers) {
    int k = centers.length;
    KMeans kmeans = new KMeans(x, k, 10);
    System.arraycopy(kmeans.centroids(), 0, centers, 0, k);
    double r0 = 0.0;
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < i; j++) {
            double d = Math.distance(centers[i], centers[j]);
            if (r0 < d) {
                r0 = d;
            }
        }
    }
    r0 /= Math.sqrt(2 * k);
    return new GaussianRadialBasis(r0);
}
Also used : GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis) KMeans(smile.clustering.KMeans)

Example 4 with GaussianRadialBasis

use of smile.math.rbf.GaussianRadialBasis 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;
}
Also used : CLARANS(smile.clustering.CLARANS) GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis)

Example 5 with GaussianRadialBasis

use of smile.math.rbf.GaussianRadialBasis 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;
}
Also used : CLARANS(smile.clustering.CLARANS) GaussianRadialBasis(smile.math.rbf.GaussianRadialBasis)

Aggregations

GaussianRadialBasis (smile.math.rbf.GaussianRadialBasis)7 KMeans (smile.clustering.KMeans)3 Test (org.junit.Test)2 CLARANS (smile.clustering.CLARANS)2 AttributeDataset (smile.data.AttributeDataset)1 NominalAttribute (smile.data.NominalAttribute)1 DelimitedTextParser (smile.data.parser.DelimitedTextParser)1 EuclideanDistance (smile.math.distance.EuclideanDistance)1 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)1