Search in sources :

Example 31 with ArffParser

use of smile.data.parser.ArffParser in project smile by haifengl.

the class FLDTest method testPredict.

/**
     * Test of predict method, of class FDA.
     */
@Test
public void testPredict() {
    System.out.println("IRIS");
    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()]);
        int n = x.length;
        LOOCV loocv = new LOOCV(n);
        int error = 0;
        for (int i = 0; i < n; i++) {
            double[][] trainx = Math.slice(x, loocv.train[i]);
            int[] trainy = Math.slice(y, loocv.train[i]);
            FLD fisher = new FLD(trainx, trainy);
            if (y[loocv.test[i]] != fisher.predict(x[loocv.test[i]]))
                error++;
        }
        System.out.println("FLD error = " + error);
        assertEquals(5, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) LOOCV(smile.validation.LOOCV) Test(org.junit.Test)

Example 32 with ArffParser

use of smile.data.parser.ArffParser in project smile by haifengl.

the class GradientTreeBoostTest method testSegment.

/**
     * Test of learn method, of class GradientTreeBoost.
     */
@Test
public void testSegment() {
    System.out.println("Segment");
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(19);
    try {
        AttributeDataset train = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
        AttributeDataset test = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
        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()]);
        GradientTreeBoost boost = new GradientTreeBoost(train.attributes(), x, y, 100);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (boost.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("Gradient Tree Boost error rate = %.2f%%%n", 100.0 * error / testx.length);
    //assertEquals(28, error);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 33 with ArffParser

use of smile.data.parser.ArffParser in project smile by haifengl.

the class KNNTest method testSegment.

/**
     * Test of learn method, of class KNN.
     */
@Test
public void testSegment() throws ParseException {
    System.out.println("Segment");
    ArffParser parser = new ArffParser();
    parser.setResponseIndex(19);
    try {
        AttributeDataset train = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-challenge.arff"));
        AttributeDataset test = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/segment-test.arff"));
        double[][] x = train.toArray(new double[0][]);
        int[] y = train.toArray(new int[0]);
        double[][] testx = test.toArray(new double[0][]);
        int[] testy = test.toArray(new int[0]);
        KNN<double[]> knn = KNN.learn(x, y);
        int error = 0;
        for (int i = 0; i < testx.length; i++) {
            if (knn.predict(testx[i]) != testy[i]) {
                error++;
            }
        }
        System.out.format("Segment error rate = %.2f%%%n", 100.0 * error / testx.length);
        assertEquals(39, error);
    } catch (IOException ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) IOException(java.io.IOException) Test(org.junit.Test)

Example 34 with ArffParser

use of smile.data.parser.ArffParser in project smile by haifengl.

the class Nominal2SparseBinaryTest method testF.

/**
     * Test of f method, of class Nominal2SparseBinary.
     */
@Test
public void testF() {
    System.out.println("f");
    int[][] result = { { 0, 3, 6, 9 }, { 0, 3, 6, 8 }, { 1, 3, 6, 9 }, { 2, 4, 6, 9 }, { 2, 5, 7, 9 }, { 2, 5, 7, 8 }, { 1, 5, 7, 8 }, { 0, 4, 6, 9 }, { 0, 5, 7, 9 }, { 2, 4, 7, 9 }, { 0, 4, 7, 8 }, { 1, 4, 6, 8 }, { 1, 3, 7, 9 }, { 2, 4, 6, 8 } };
    ArffParser arffParser = new ArffParser();
    arffParser.setResponseIndex(4);
    try {
        AttributeDataset weather = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/weather.nominal.arff"));
        double[][] x = weather.toArray(new double[weather.size()][]);
        Nominal2SparseBinary n2sb = new Nominal2SparseBinary(weather.attributes());
        for (int i = 0; i < x.length; i++) {
            int[] y = n2sb.f(x[i]);
            assertEquals(result[i].length, y.length);
            for (int j = 0; j < y.length; j++) {
                assertEquals(result[i][j], y[j]);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Test(org.junit.Test)

Example 35 with ArffParser

use of smile.data.parser.ArffParser in project smile by haifengl.

the class NumericAttributeFeatureTest method testNORMALIZATIONWinsorization.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testNORMALIZATIONWinsorization() {
    System.out.println("NORMALIZATION  Winsorization");
    ArffParser parser = new ArffParser();
    try {
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), 0.05, 0.95, x);
        Attribute[] attributes = naf.attributes();
        assertEquals(data.attributes().length - 1, attributes.length);
        for (int i = 0; i < x.length; i++) {
            double[] y = new double[attributes.length];
            for (int j = 0; j < y.length; j++) {
                y[j] = naf.f(x[i], j);
                assertTrue(y[j] <= 1.0 && y[j] >= 0.0);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Aggregations

AttributeDataset (smile.data.AttributeDataset)75 ArffParser (smile.data.parser.ArffParser)75 Test (org.junit.Test)71 LOOCV (smile.validation.LOOCV)18 CrossValidation (smile.validation.CrossValidation)17 EuclideanDistance (smile.math.distance.EuclideanDistance)14 ClassifierTrainer (smile.classification.ClassifierTrainer)12 GaussianKernel (smile.math.kernel.GaussianKernel)10 Attribute (smile.data.Attribute)8 RadialBasisFunction (smile.math.rbf.RadialBasisFunction)8 RBFNetwork (smile.regression.RBFNetwork)8 KMeans (smile.clustering.KMeans)6 IOException (java.io.IOException)3 DecisionTree (smile.classification.DecisionTree)2 NominalAttribute (smile.data.NominalAttribute)2 PolynomialKernel (smile.math.kernel.PolynomialKernel)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 LinearKernel (smile.math.kernel.LinearKernel)1 Distribution (smile.stat.distribution.Distribution)1