use of smile.classification.LDA in project smile by haifengl.
the class LDADemo method learn.
@Override
public double[][] learn(double[] x, double[] y) {
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
LDA lda = new LDA(data, label);
for (int i = 0; i < label.length; i++) {
label[i] = lda.predict(data[i]);
}
double trainError = error(label, label);
System.out.format("training error = %.2f%%\n", 100 * trainError);
double[][] z = new double[y.length][x.length];
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++) {
double[] p = { x[j], y[i] };
z[i][j] = lda.predict(p);
}
}
return z;
}
use of smile.classification.LDA in project smile by haifengl.
the class SumSquaresRatioTest method testLearn.
/**
* Test of learn method, of class SumSquaresRatio.
*/
@Test
public void testLearn() {
System.out.println("USPS");
try {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
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()]);
SumSquaresRatio ssr = new SumSquaresRatio();
double[] score = ssr.rank(x, y);
int[] index = QuickSort.sort(score);
int p = 135;
int n = x.length;
double[][] xx = new double[n][p];
for (int j = 0; j < p; j++) {
for (int i = 0; i < n; i++) {
xx[i][j] = x[i][index[255 - j]];
}
}
int testn = testx.length;
double[][] testxx = new double[testn][p];
for (int j = 0; j < p; j++) {
for (int i = 0; i < testn; i++) {
testxx[i][j] = testx[i][index[255 - j]];
}
}
LDA lda = new LDA(xx, y);
int[] prediction = new int[testn];
for (int i = 0; i < testn; i++) {
prediction[i] = lda.predict(testxx[i]);
}
double accuracy = new Accuracy().measure(testy, prediction);
System.out.format("SSR %.2f%%%n", 100 * accuracy);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.classification.LDA in project smile by haifengl.
the class ValidationTest method testTest_3args_1.
/**
* Test of test method, of class Validation.
*/
@Test
public void testTest_3args_1() {
System.out.println("test");
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()]);
LDA lda = new LDA(x, y);
double accuracy = Validation.test(lda, testx, testy);
System.out.println("accuracy = " + accuracy);
assertEquals(0.8724, accuracy, 1E-4);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.classification.LDA in project smile by haifengl.
the class ValidationTest method testTest_4args_1.
/**
* Test of test method, of class Validation.
*/
@Test
public void testTest_4args_1() {
System.out.println("test");
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()]);
LDA lda = new LDA(x, y);
ClassificationMeasure[] measures = { new Accuracy() };
double[] accuracy = Validation.test(lda, testx, testy, measures);
System.out.println("accuracy = " + accuracy[0]);
assertEquals(0.8724, accuracy[0], 1E-4);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations