use of smile.math.kernel.GaussianKernel in project smile by haifengl.
the class GaussianProcessRegressionTest method testPuma8nh.
/**
* Test of learn method, of class GaussianProcessRegression.
*/
@Test
public void testPuma8nh() {
System.out.println("puma8nh");
ArffParser parser = new ArffParser();
parser.setResponseIndex(8);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/puma8nh.arff"));
double[] y = data.toArray(new double[data.size()]);
double[][] x = data.toArray(new double[data.size()][]);
int[] perm = Math.permutate(x.length);
double[][] datax = new double[4000][];
double[] datay = new double[datax.length];
for (int i = 0; i < datax.length; i++) {
datax[i] = x[perm[i]];
datay[i] = y[perm[i]];
}
int n = datax.length;
int k = 10;
CrossValidation cv = new CrossValidation(n, k);
double rss = 0.0;
double sparseRSS30 = 0.0;
for (int i = 0; i < k; i++) {
double[][] trainx = Math.slice(datax, cv.train[i]);
double[] trainy = Math.slice(datay, cv.train[i]);
double[][] testx = Math.slice(datax, cv.test[i]);
double[] testy = Math.slice(datay, cv.test[i]);
GaussianProcessRegression<double[]> rkhs = new GaussianProcessRegression<>(trainx, trainy, new GaussianKernel(38.63), 0.1);
KMeans kmeans = new KMeans(trainx, 30, 10);
double[][] centers = kmeans.centroids();
double r0 = 0.0;
for (int l = 0; l < centers.length; l++) {
for (int j = 0; j < l; j++) {
r0 += Math.distance(centers[l], centers[j]);
}
}
r0 /= (2 * centers.length);
System.out.println("Kernel width = " + r0);
GaussianProcessRegression<double[]> sparse30 = new GaussianProcessRegression<>(trainx, trainy, centers, new GaussianKernel(r0), 0.1);
for (int j = 0; j < testx.length; j++) {
double r = testy[j] - rkhs.predict(testx[j]);
rss += r * r;
r = testy[j] - sparse30.predict(testx[j]);
sparseRSS30 += r * r;
}
}
System.out.println("Regular 10-CV MSE = " + rss / n);
System.out.println("Sparse (30) 10-CV MSE = " + sparseRSS30 / n);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.math.kernel.GaussianKernel in project smile by haifengl.
the class SVMTest method testUSPS.
/**
* Test of learn method, of class SVM.
*/
@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()]);
SVM<double[]> svm = new SVM<>(new GaussianKernel(8.0), 5.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ONE);
svm.learn(x, y);
svm.finish();
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (svm.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error < 95);
System.out.println("USPS one more epoch...");
for (int i = 0; i < x.length; i++) {
int j = Math.randomInt(x.length);
svm.learn(x[j], y[j]);
}
svm.finish();
error = 0;
for (int i = 0; i < testx.length; i++) {
if (svm.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error < 95);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of smile.math.kernel.GaussianKernel in project smile by haifengl.
the class SVMTest method testSegment.
/**
* Test of learn method, of class SVM.
*/
@Test
public void testSegment() {
System.out.println("Segment");
ArffParser parser = new ArffParser();
parser.setResponseIndex(19);
try {
AttributeDataset train = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
AttributeDataset test = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
System.out.println(train.size() + " " + test.size());
double[][] x = train.toArray(new double[0][]);
int[] y = train.toArray(new int[0]);
double[][] testx = test.toArray(new double[0][]);
int[] testy = test.toArray(new int[0]);
SVM<double[]> svm = new SVM<>(new GaussianKernel(8.0), 5.0, Math.max(y) + 1, SVM.Multiclass.ONE_VS_ALL);
svm.learn(x, y);
svm.finish();
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (svm.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error < 70);
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of smile.math.kernel.GaussianKernel in project smile by haifengl.
the class KPCADemo method learn.
@Override
public JComponent learn() {
JPanel pane = new JPanel(new GridLayout(2, 2));
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
if (names[0] == null) {
names = null;
}
if (gamma[datasetIndex] == 0.0) {
int n = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < i; j++, n++) {
gamma[datasetIndex] += Math.squaredDistance(data[i], data[j]);
}
}
gamma[datasetIndex] = Math.sqrt(gamma[datasetIndex] / n) / 4;
} else {
try {
gamma[datasetIndex] = Double.parseDouble(gammaNumberField.getText().trim());
if (gamma[datasetIndex] <= 0) {
JOptionPane.showMessageDialog(this, "Invalid parameter: " + gamma[datasetIndex], "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid parameter: " + gammaNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
gammaNumberField.setText(String.format("%.4f", gamma[datasetIndex]));
long clock = System.currentTimeMillis();
PCA pca = new PCA(data, true);
System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
pca.setProjection(2);
double[][] y = pca.project(data);
PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
if (names != null) {
plot.points(y, names);
} else if (dataset[datasetIndex].response() != null) {
int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
for (int i = 0; i < y.length; i++) {
plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
}
} else {
plot.points(y, pointLegend);
}
plot.setTitle("PCA");
pane.add(plot);
pca.setProjection(3);
y = pca.project(data);
plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
if (names != null) {
plot.points(y, names);
} else if (dataset[datasetIndex].response() != null) {
int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
for (int i = 0; i < y.length; i++) {
plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
}
} else {
plot.points(y, pointLegend);
}
plot.setTitle("PCA");
pane.add(plot);
KPCA<double[]> kpca = new KPCA<>(data, new GaussianKernel(gamma[datasetIndex]), 2);
y = kpca.getCoordinates();
plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
if (names != null) {
plot.points(y, names);
} else if (dataset[datasetIndex].response() != null) {
int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
for (int i = 0; i < y.length; i++) {
plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
}
} else {
plot.points(y, pointLegend);
}
plot.setTitle("KPCA");
pane.add(plot);
clock = System.currentTimeMillis();
kpca = new KPCA<>(data, new GaussianKernel(gamma[datasetIndex]), 3);
System.out.format("Learn KPCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
y = kpca.getCoordinates();
plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
if (names != null) {
plot.points(y, names);
} else if (dataset[datasetIndex].response() != null) {
int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
for (int i = 0; i < y.length; i++) {
plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
}
} else {
plot.points(y, pointLegend);
}
plot.setTitle("KPCA");
pane.add(plot);
return pane;
}
Aggregations