use of smile.data.AttributeDataset in project smile by haifengl.
the class SpectralClusteringTest method testUSPSNystrom.
/**
* Test of learn method, of class SpectralClustering.
*/
@Test
public void testUSPSNystrom() {
System.out.println("USPS Nystrom approximation");
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"));
double[][] x = train.toArray(new double[train.size()][]);
int[] y = train.toArray(new int[train.size()]);
SpectralClustering spectral = new SpectralClustering(x, 10, 100, 8.0);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
double r = rand.measure(y, spectral.getClusterLabel());
double r2 = ari.measure(y, spectral.getClusterLabel());
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.8);
assertTrue(r2 > 0.35);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class SpectralClusteringTest method testUSPS.
/**
* Test of learn method, of class SpectralClustering.
*/
@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"));
double[][] x = train.toArray(new double[train.size()][]);
int[] y = train.toArray(new int[train.size()]);
SpectralClustering spectral = new SpectralClustering(x, 10, 8.0);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
double r = rand.measure(y, spectral.getClusterLabel());
double r2 = ari.measure(y, spectral.getClusterLabel());
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.85);
assertTrue(r2 > 0.45);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset 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.AttributeDataset 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.AttributeDataset in project smile by haifengl.
the class GAFeatureSelectionTest method testLearn.
/**
* Test of learn method, of class GAFeatureSelection.
*/
@Test
public void testLearn() {
System.out.println("learn");
int size = 100;
int generation = 20;
ClassifierTrainer<double[]> trainer = new LDA.Trainer();
ClassificationMeasure measure = new Accuracy();
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()]);
GAFeatureSelection instance = new GAFeatureSelection();
BitString[] result = instance.learn(size, generation, trainer, measure, x, y, testx, testy);
for (BitString bits : result) {
System.out.format("%.2f%% %d ", 100 * bits.fitness(), Math.sum(bits.bits()));
for (int i = 0; i < x[0].length; i++) {
System.out.print(bits.bits()[i] + " ");
}
System.out.println();
}
assertTrue(result[result.length - 1].fitness() > 0.88);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations