use of smile.data.AttributeDataset 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.data.AttributeDataset in project smile by haifengl.
the class NumericAttributeFeatureTest method testSTANDARDIZATION.
/**
* Test of f method, of class NumericAttributeFeature.
*/
@Test
public void testSTANDARDIZATION() {
System.out.println("STANDARDIZATION");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
double[][] x = data.toArray(new double[data.size()][]);
double[] mean = Math.colMean(x);
double[] sd = Math.colSd(x);
NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.STANDARDIZATION, x);
Attribute[] attributes = naf.attributes();
assertEquals(256, attributes.length);
for (int i = 0; i < x.length; i++) {
double[] y = new double[attributes.length];
for (int j = 0; j < y.length; j++) {
y[j] = naf.f(x[i], j);
assertEquals((x[i][j] - mean[j]) / sd[j], y[j], 1E-7);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class NumericAttributeFeatureTest method testNONE.
/**
* Test of f method, of class NumericAttributeFeature.
*/
@Test
public void testNONE() {
System.out.println("NONE");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
double[][] x = data.toArray(new double[data.size()][]);
NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.NONE);
Attribute[] attributes = naf.attributes();
assertEquals(256, attributes.length);
for (int i = 0; i < x.length; i++) {
double[] y = new double[attributes.length];
for (int j = 0; j < y.length; j++) {
y[j] = naf.f(x[i], j);
assertEquals(x[i][j], y[j], 1E-7);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class NumericAttributeFeatureTest method testAttributes.
/**
* Test of attributes method, of class NumericAttributeFeature.
*/
@SuppressWarnings("unused")
@Test
public void testAttributes() {
System.out.println("attributes");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
double[][] x = data.toArray(new double[data.size()][]);
NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.LOGARITHM);
Attribute[] attributes = naf.attributes();
assertEquals(256, attributes.length);
for (int i = 0; i < attributes.length; i++) {
System.out.println(attributes[i]);
assertEquals(Attribute.Type.NUMERIC, attributes[i].getType());
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class NumericAttributeFeatureTest method testROBUSTSTANDARDIZATION.
/**
* Test of f method, of class NumericAttributeFeature.
*/
@Test
public void testROBUSTSTANDARDIZATION() {
System.out.println("ROBUST STANDARDIZATION");
ArffParser parser = new ArffParser();
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
double[][] x = data.toArray(new double[data.size()][]);
NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), x);
Attribute[] attributes = naf.attributes();
assertEquals(data.attributes().length - 1, attributes.length);
for (int i = 0; i < x.length; i++) {
double[] y = new double[attributes.length];
for (int j = 0; j < y.length; j++) {
y[j] = naf.f(x[i], j);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations