use of smile.data.NominalAttribute in project smile by haifengl.
the class LinearSearchSpeedTest method testUSPS.
/**
* Test of nearest method, of class LinearSearch.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
double[][] x = null;
double[][] testx = null;
long start = System.currentTimeMillis();
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"));
x = train.toArray(new double[train.size()][]);
testx = test.toArray(new double[test.size()][]);
} catch (Exception ex) {
System.err.println(ex);
}
double time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Loading USPS: %.2fs%n", time);
LinearSearch<double[]> naive = new LinearSearch<>(x, new EuclideanDistance());
start = System.currentTimeMillis();
for (int i = 0; i < testx.length; i++) {
naive.nearest(testx[i]);
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("NN: %.2fs%n", time);
start = System.currentTimeMillis();
for (int i = 0; i < testx.length; i++) {
naive.knn(testx[i], 10);
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("10-NN: %.2fs%n", time);
start = System.currentTimeMillis();
List<Neighbor<double[], double[]>> n = new ArrayList<>();
for (int i = 0; i < testx.length; i++) {
naive.range(testx[i], 8.0, n);
n.clear();
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Range: %.2fs%n", time);
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class MPLSHSpeedTest method testUSPS.
/**
* Test of nearest method, of class KDTree.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
double[][] x = null;
double[][] testx = null;
long start = System.currentTimeMillis();
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"));
x = train.toArray(new double[train.size()][]);
testx = test.toArray(new double[test.size()][]);
} catch (Exception ex) {
System.err.println(ex);
}
double time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Loading USPS: %.2fs%n", time);
start = System.currentTimeMillis();
MPLSH<double[]> lsh = new MPLSH<>(256, 100, 3, 4.0);
for (double[] xi : x) {
lsh.put(xi, xi);
}
double[][] train = new double[500][];
int[] index = Math.permutate(x.length);
for (int i = 0; i < train.length; i++) {
train[i] = x[index[i]];
}
LinearSearch<double[]> naive = new LinearSearch<>(x, new EuclideanDistance());
lsh.learn(naive, train, 8.0);
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Building LSH: %.2fs%n", time);
start = System.currentTimeMillis();
for (int i = 0; i < testx.length; i++) {
lsh.nearest(testx[i]);
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("NN: %.2fs%n", time);
start = System.currentTimeMillis();
for (int i = 0; i < testx.length; i++) {
lsh.knn(testx[i], 10);
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("10-NN: %.2fs%n", time);
start = System.currentTimeMillis();
List<Neighbor<double[], double[]>> n = new ArrayList<>();
for (int i = 0; i < testx.length; i++) {
lsh.range(testx[i], 8.0, n);
n.clear();
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Range: %.2fs%n", time);
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class DelimitedTextParserTest method testParse.
/**
* Test of parse method, of class DelimitedTextParser.
*/
@Test
public void testParse() throws Exception {
System.out.println("parse");
try {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
AttributeDataset usps = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
double[][] x = usps.toArray(new double[usps.size()][]);
int[] y = usps.toArray(new int[usps.size()]);
assertEquals(Attribute.Type.NOMINAL, usps.response().getType());
for (Attribute attribute : usps.attributes()) {
assertEquals(Attribute.Type.NUMERIC, attribute.getType());
}
assertEquals(7291, usps.size());
assertEquals(256, usps.attributes().length);
assertEquals("6", usps.response().toString(y[0]));
assertEquals("5", usps.response().toString(y[1]));
assertEquals("4", usps.response().toString(y[2]));
assertEquals(-1.0000, x[0][6], 1E-7);
assertEquals(-0.6310, x[0][7], 1E-7);
assertEquals(0.8620, x[0][8], 1E-7);
assertEquals("1", usps.response().toString(y[7290]));
assertEquals(-1.0000, x[7290][4], 1E-7);
assertEquals(-0.1080, x[7290][5], 1E-7);
assertEquals(1.0000, x[7290][6], 1E-7);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.NominalAttribute 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);
}
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class NeuralGasTest method testUSPS.
/**
* Test of learn method, of class NeuralGas.
*/
@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()]);
NeuralGas gas = new NeuralGas(x, 10);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
double r = rand.measure(y, gas.getClusterLabel());
double r2 = ari.measure(y, gas.getClusterLabel());
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.88);
assertTrue(r2 > 0.45);
int[] p = new int[testx.length];
for (int i = 0; i < testx.length; i++) {
p[i] = gas.predict(testx[i]);
}
r = rand.measure(testy, p);
r2 = ari.measure(testy, p);
System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.88);
assertTrue(r2 > 0.45);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations