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 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 k-means.
* @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 GaussianRadialBasis[] learnGaussianRadialBasis(double[][] x, double[][] centers, int p) {
if (p < 1) {
throw new IllegalArgumentException("Invalid number of nearest neighbors: " + p);
}
int k = centers.length;
KMeans kmeans = new KMeans(x, k, 10);
System.arraycopy(kmeans.centroids(), 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] = Math.distance(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;
}
use of smile.math.rbf.GaussianRadialBasis in project smile by haifengl.
the class RBFNetworkTest method testUSPS.
/**
* Test of learn method, of class RBFNetwork.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
AttributeDataset train = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
AttributeDataset test = parser.parse("USPS Test", smile.data.parser.IOUtils.getTestDataFile("usps/zip.test"));
double[][] x = train.toArray(new double[train.size()][]);
int[] y = train.toArray(new int[train.size()]);
double[][] testx = test.toArray(new double[test.size()][]);
int[] testy = test.toArray(new int[test.size()]);
double[][] centers = new double[200][];
RadialBasisFunction basis = SmileUtils.learnGaussianRadialBasis(x, centers);
RBFNetwork<double[]> rbf = new RBFNetwork<>(x, y, new EuclideanDistance(), new GaussianRadialBasis(8.0), centers);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (rbf.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error <= 150);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations