use of smile.data.Attribute in project smile by haifengl.
the class AdaBoostTest method testUSPSNominal.
/**
* Test of learn method, of class AdaBoost.
*/
@Test
public void testUSPSNominal() {
System.out.println("USPS nominal");
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()]);
for (double[] xi : x) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(255 * (xi[i] + 1) / 2);
}
}
for (double[] xi : testx) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(255 * (xi[i] + 1) / 2);
}
}
Attribute[] attributes = new Attribute[256];
String[] values = new String[attributes.length];
for (int i = 0; i < attributes.length; i++) {
values[i] = String.valueOf(i);
}
for (int i = 0; i < attributes.length; i++) {
attributes[i] = new NominalAttribute("V" + i, values);
}
for (int i = 0; i < y.length; i++) {
if (y[i] != 0)
y[i] = 1;
}
for (int i = 0; i < testy.length; i++) {
if (testy[i] != 0)
testy[i] = 1;
}
AdaBoost forest = new AdaBoost(attributes, x, y, 100, 6);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (forest.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.println("AdaBoost error = " + error);
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
assertTrue(error <= 25);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class DateFeatureTest method testAttributes.
/**
* Test of attributes method, of class DateFeature.
*/
@Test
public void testAttributes() {
System.out.println("attributes");
try {
ArffParser parser = new ArffParser();
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/date.arff"));
DateFeature.Type[] features = { DateFeature.Type.YEAR, DateFeature.Type.MONTH, DateFeature.Type.DAY_OF_MONTH, DateFeature.Type.DAY_OF_WEEK, DateFeature.Type.HOURS, DateFeature.Type.MINUTES, DateFeature.Type.SECONDS };
DateFeature df = new DateFeature(data.attributes(), features);
Attribute[] attributes = df.attributes();
assertEquals(features.length, 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.Attribute in project smile by haifengl.
the class FeatureSetTest method testF.
/**
* Test of f method, of class FeatureSet.
*/
@Test
public void testF() {
System.out.println("f");
try {
ArffParser parser = new ArffParser();
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
double[][] x = data.toArray(new double[data.size()][]);
FeatureSet<double[]> features = new FeatureSet<>();
features.add(new Nominal2Binary(data.attributes()));
features.add(new NumericAttributeFeature(data.attributes(), 0.05, 0.95, x));
AttributeDataset dataset = features.f(data);
assertEquals(data.size(), dataset.size());
assertEquals(data.getName(), dataset.getName());
assertEquals(data.getDescription(), dataset.getDescription());
Attribute[] attributes = features.attributes();
for (int i = 0; i < attributes.length; i++) {
assertEquals(attributes[i].getName(), dataset.attributes()[i].getName());
assertEquals(attributes[i].getType(), dataset.attributes()[i].getType());
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class RandomForestTest method testUSPSNominal.
/**
* Test of learn method, of class RandomForest.
*/
@Test
public void testUSPSNominal() {
System.out.println("USPS nominal");
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()]);
for (double[] xi : x) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(255 * (xi[i] + 1) / 2);
}
}
for (double[] xi : testx) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(255 * (xi[i] + 1) / 2);
}
}
Attribute[] attributes = new Attribute[256];
String[] values = new String[attributes.length];
for (int i = 0; i < attributes.length; i++) {
values[i] = String.valueOf(i);
}
for (int i = 0; i < attributes.length; i++) {
attributes[i] = new NominalAttribute("V" + i, values);
}
RandomForest forest = new RandomForest(attributes, x, y, 200);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (forest.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.println(error);
System.out.format("USPS OOB error rate = %.2f%%%n", 100.0 * forest.error());
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
double[] accuracy = forest.test(testx, testy);
for (int i = 1; i <= accuracy.length; i++) {
System.out.format("%d trees accuracy = %.2f%%%n", i, 100.0 * accuracy[i - 1]);
}
double[] importance = forest.importance();
int[] index = QuickSort.sort(importance);
for (int i = importance.length; i-- > 0; ) {
System.out.format("%s importance is %.4f%n", train.attributes()[index[i]], importance[i]);
}
assertTrue(error <= 150);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class DecisionTreeTest method testUSPSNominal.
/**
* Test of learn method, of class DecisionTree.
*/
@Test
public void testUSPSNominal() {
System.out.println("USPS nominal");
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()]);
for (double[] xi : x) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(255 * (xi[i] + 1) / 2);
}
}
for (double[] xi : testx) {
for (int i = 0; i < xi.length; i++) {
xi[i] = Math.round(127 + 127 * xi[i]);
}
}
Attribute[] attributes = new Attribute[256];
String[] values = new String[attributes.length];
for (int i = 0; i < attributes.length; i++) {
values[i] = String.valueOf(i);
}
for (int i = 0; i < attributes.length; i++) {
attributes[i] = new NominalAttribute("V" + i, values);
}
DecisionTree tree = new DecisionTree(attributes, x, y, 350, 2, DecisionTree.SplitRule.ENTROPY);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (tree.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
double[] importance = tree.importance();
int[] index = QuickSort.sort(importance);
for (int i = importance.length; i-- > 0; ) {
System.out.format("%s importance is %.4f%n", train.attributes()[index[i]], importance[i]);
}
assertEquals(324, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations