Search in sources :

Example 1 with ClassificationMeasure

use of smile.validation.ClassificationMeasure 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

Test (org.junit.Test)1 ClassifierTrainer (smile.classification.ClassifierTrainer)1 AttributeDataset (smile.data.AttributeDataset)1 NominalAttribute (smile.data.NominalAttribute)1 DelimitedTextParser (smile.data.parser.DelimitedTextParser)1 BitString (smile.gap.BitString)1 Accuracy (smile.validation.Accuracy)1 ClassificationMeasure (smile.validation.ClassificationMeasure)1