Search in sources :

Example 66 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 67 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)

Example 68 with AttributeDataset

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

the class VQDemo method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if ("startButton".equals(e.getActionCommand())) {
        try {
            clusterNumber = Integer.parseInt(clusterNumberField.getText().trim());
            if (clusterNumber < 2) {
                JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumber, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (clusterNumber > dataset[datasetIndex].length / 2) {
                JOptionPane.showMessageDialog(this, "Too large K: " + clusterNumber, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumberField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        Thread thread = new Thread(this);
        thread.start();
    } else if ("datasetBox".equals(e.getActionCommand())) {
        datasetIndex = datasetBox.getSelectedIndex();
        if (dataset[datasetIndex] == null) {
            DelimitedTextParser parser = new DelimitedTextParser();
            parser.setDelimiter("[\t ]+");
            try {
                AttributeDataset data = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
                dataset[datasetIndex] = data.toArray(new double[data.size()][]);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
                System.err.println(ex);
            }
        }
        remove(canvas);
        if (dataset[datasetIndex].length < 500) {
            pointLegend = 'o';
        } else {
            pointLegend = '.';
        }
        canvas = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
        add(canvas, BorderLayout.CENTER);
        validate();
    }
}
Also used : DelimitedTextParser(smile.data.parser.DelimitedTextParser) AttributeDataset(smile.data.AttributeDataset)

Example 69 with AttributeDataset

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

the class PCLParserTest method testParse.

/**
     * Test of parse method, of class PCLParser.
     */
@Test
public void testParse() throws Exception {
    System.out.println("parse");
    PCLParser parser = new PCLParser();
    try {
        AttributeDataset data = parser.parse("PCL", smile.data.parser.IOUtils.getTestDataFile("microarray/Dunham2002.pcl"));
        double[][] x = data.toArray(new double[data.size()][]);
        String[] id = data.toArray(new String[data.size()]);
        for (Attribute attribute : data.attributes()) {
            assertEquals(Attribute.Type.NUMERIC, attribute.getType());
            System.out.println(attribute.getName());
        }
        assertEquals(6694, data.size());
        assertEquals(16, data.attributes().length);
        assertEquals("YKR005C", id[0]);
        assertEquals(-0.43, x[0][0], 1E-7);
        assertEquals(-0.47, x[0][1], 1E-7);
        assertEquals(-0.39, x[0][2], 1E-7);
        assertEquals("YKR004C", id[6693]);
        assertEquals(0.03, x[6693][13], 1E-7);
        assertEquals(-0.53, x[6693][14], 1E-7);
        assertEquals(0.3, x[6693][15], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 70 with AttributeDataset

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

the class RESParserTest method testParse.

/**
     * Test of parse method, of class RESParser.
     */
@Test
public void testParse() throws Exception {
    System.out.println("parse");
    RESParser parser = new RESParser();
    try {
        AttributeDataset data = parser.parse("RES", smile.data.parser.IOUtils.getTestDataFile("microarray/all_aml_test.res"));
        double[][] x = data.toArray(new double[data.size()][]);
        String[] id = data.toArray(new String[data.size()]);
        for (Attribute attribute : data.attributes()) {
            assertEquals(Attribute.Type.NUMERIC, attribute.getType());
            System.out.println(attribute.getName() + "\t" + attribute.getDescription());
        }
        assertEquals(7129, data.size());
        assertEquals(35, data.attributes().length);
        assertEquals("AFFX-BioB-5_at", id[0]);
        assertEquals(-214, x[0][0], 1E-7);
        assertEquals(-342, x[0][1], 1E-7);
        assertEquals(-87, x[0][2], 1E-7);
        assertEquals("Z78285_f_at", id[7128]);
        assertEquals(16, x[7128][32], 1E-7);
        assertEquals(-73, x[7128][33], 1E-7);
        assertEquals(-60, x[7128][34], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) 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