Search in sources :

Example 61 with AttributeDataset

use of smile.data.AttributeDataset 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 62 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class NumericAttributeFeatureTest method testNORMALIZATION.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testNORMALIZATION() {
    System.out.println("NORMALIZATION");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = data.toArray(new double[data.size()][]);
        double[] min = Math.colMin(x);
        double[] max = Math.colMax(x);
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.NORMALIZATION, x);
        Attribute[] attributes = naf.attributes();
        assertEquals(256, 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);
                assertEquals((x[i][j] - min[j]) / (max[j] - min[j]), y[j], 1E-7);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 63 with AttributeDataset

use of smile.data.AttributeDataset 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)

Example 64 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class NumericAttributeFeatureTest method testLOGARITHM.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testLOGARITHM() {
    System.out.println("LOGARITHM");
    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setResponseIndex(new NominalAttribute("class"), 0);
    try {
        AttributeDataset data = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = data.toArray(new double[data.size()][]);
        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < x[i].length; j++) {
                x[i][j] += 2.0;
            }
        }
        NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.LOGARITHM);
        Attribute[] attributes = naf.attributes();
        assertEquals(256, 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);
                assertEquals(Math.log(x[i][j]), y[j], 1E-7);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 65 with AttributeDataset

use of smile.data.AttributeDataset in project smile by haifengl.

the class SumSquaresRatioTest method testLearn.

/**
     * Test of learn method, of class SumSquaresRatio.
     */
@Test
public void testLearn() {
    System.out.println("USPS");
    try {
        DelimitedTextParser parser = new DelimitedTextParser();
        parser.setResponseIndex(new NominalAttribute("class"), 0);
        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()]);
        SumSquaresRatio ssr = new SumSquaresRatio();
        double[] score = ssr.rank(x, y);
        int[] index = QuickSort.sort(score);
        int p = 135;
        int n = x.length;
        double[][] xx = new double[n][p];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < n; i++) {
                xx[i][j] = x[i][index[255 - j]];
            }
        }
        int testn = testx.length;
        double[][] testxx = new double[testn][p];
        for (int j = 0; j < p; j++) {
            for (int i = 0; i < testn; i++) {
                testxx[i][j] = testx[i][index[255 - j]];
            }
        }
        LDA lda = new LDA(xx, y);
        int[] prediction = new int[testn];
        for (int i = 0; i < testn; i++) {
            prediction[i] = lda.predict(testxx[i]);
        }
        double accuracy = new Accuracy().measure(testy, prediction);
        System.out.format("SSR %.2f%%%n", 100 * accuracy);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset) Accuracy(smile.validation.Accuracy) NominalAttribute(smile.data.NominalAttribute) LDA(smile.classification.LDA) 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