Search in sources :

Example 1 with ClassifierTrainer

use of smile.classification.ClassifierTrainer in project smile by haifengl.

the class ValidationTest method testCv_5args_1.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_5args_1() {
    System.out.println("cv");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        ClassificationMeasure[] measures = { new Accuracy() };
        double[] results = Validation.cv(10, trainer, x, y, measures);
        for (int i = 0; i < measures.length; i++) {
            System.out.println(measures[i] + " = " + results[i]);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 2 with ClassifierTrainer

use of smile.classification.ClassifierTrainer in project smile by haifengl.

the class ValidationTest method testCv_4args_1.

/**
     * Test of cv method, of class Validation.
     */
@Test
public void testCv_4args_1() {
    System.out.println("cv");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        double accuracy = Validation.cv(10, trainer, x, y);
        System.out.println("10-fold CV accuracy = " + accuracy);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 3 with ClassifierTrainer

use of smile.classification.ClassifierTrainer in project smile by haifengl.

the class ValidationTest method testBootstrap_4args_1.

/**
     * Test of bootstrap method, of class Validation.
     */
@Test
public void testBootstrap_4args_1() {
    System.out.println("bootstrap");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        double[] accuracy = Validation.bootstrap(100, trainer, x, y);
        System.out.println("100-fold bootstrap accuracy average = " + Math.mean(accuracy));
        System.out.println("100-fold bootstrap accuracy std.dev = " + Math.sd(accuracy));
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 4 with ClassifierTrainer

use of smile.classification.ClassifierTrainer in project smile by haifengl.

the class ValidationTest method testLoocv_3args_1.

/**
     * Test of loocv method, of class Validation.
     */
@Test
public void testLoocv_3args_1() {
    System.out.println("loocv");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
        double[][] x = iris.toArray(new double[iris.size()][]);
        int[] y = iris.toArray(new int[iris.size()]);
        ClassifierTrainer<double[]> trainer = new LDA.Trainer();
        double accuracy = Validation.loocv(trainer, x, y);
        System.out.println("LOOCV accuracy = " + accuracy);
        assertEquals(0.8533, accuracy, 1E-4);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) ClassifierTrainer(smile.classification.ClassifierTrainer) Test(org.junit.Test)

Example 5 with ClassifierTrainer

use of smile.classification.ClassifierTrainer 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)5 ClassifierTrainer (smile.classification.ClassifierTrainer)5 AttributeDataset (smile.data.AttributeDataset)5 ArffParser (smile.data.parser.ArffParser)4 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