Search in sources :

Example 31 with AttributeDataset

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);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 32 with AttributeDataset

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);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 33 with AttributeDataset

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);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 34 with AttributeDataset

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);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 35 with AttributeDataset

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);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) ClassificationMeasure(smile.validation.ClassificationMeasure) ClassifierTrainer(smile.classification.ClassifierTrainer) Accuracy(smile.validation.Accuracy) NominalAttribute(smile.data.NominalAttribute) BitString(smile.gap.BitString) Test(org.junit.Test)

Aggregations

AttributeDataset (smile.data.AttributeDataset)140 Test (org.junit.Test)125 ArffParser (smile.data.parser.ArffParser)75 NominalAttribute (smile.data.NominalAttribute)50 DelimitedTextParser (smile.data.parser.DelimitedTextParser)48 Attribute (smile.data.Attribute)29 EuclideanDistance (smile.math.distance.EuclideanDistance)19 LOOCV (smile.validation.LOOCV)18 CrossValidation (smile.validation.CrossValidation)17 AdjustedRandIndex (smile.validation.AdjustedRandIndex)14 RandIndex (smile.validation.RandIndex)14 ClassifierTrainer (smile.classification.ClassifierTrainer)13 GaussianKernel (smile.math.kernel.GaussianKernel)11 IOException (java.io.IOException)10 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)9 RBFNetwork (smile.regression.RBFNetwork)8 ArrayList (java.util.ArrayList)6 KMeans (smile.clustering.KMeans)6 Datum (smile.data.Datum)6 NumericAttribute (smile.data.NumericAttribute)6