Search in sources :

Example 26 with Attribute

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

the class NumericAttributeFeatureTest method testROBUSTSTANDARDIZATION.

/**
     * Test of f method, of class NumericAttributeFeature.
     */
@Test
public void testROBUSTSTANDARDIZATION() {
    System.out.println("ROBUST STANDARDIZATION");
    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(), 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);
            }
        }
    } 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 27 with Attribute

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

the class RESParser method parse.

/**
     * Parse a RES dataset from an input stream.
     * @param name the name of dataset.
     * @param stream the input stream of data.
     * @throws java.io.IOException
     */
public AttributeDataset parse(String name, InputStream stream) throws IOException, ParseException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String line = reader.readLine();
    if (line == null) {
        throw new IOException("Empty data source.");
    }
    String[] tokens = line.split("\t", -1);
    int p = (tokens.length - 2) / 2;
    line = reader.readLine();
    if (line == null) {
        throw new IOException("Premature end of file.");
    }
    String[] samples = line.split("\t", -1);
    if (samples.length != tokens.length - 1) {
        throw new IOException("Invalid sample description header.");
    }
    Attribute[] attributes = new Attribute[p];
    for (int i = 0; i < p; i++) {
        attributes[i] = new NumericAttribute(tokens[2 * i + 2], samples[2 * i + 1]);
    }
    line = reader.readLine();
    if (line == null) {
        throw new IOException("Premature end of file.");
    }
    int n = Integer.parseInt(line);
    if (n <= 0) {
        throw new IOException("Invalid number of rows: " + n);
    }
    AttributeDataset data = new AttributeDataset(name, attributes);
    for (int i = 0; i < n; i++) {
        line = reader.readLine();
        if (line == null) {
            throw new IOException("Premature end of file.");
        }
        tokens = line.split("\t", -1);
        if (tokens.length != samples.length + 1) {
            throw new IOException(String.format("Invalid number of elements of line %d: %d", i + 4, tokens.length));
        }
        double[] x = new double[p];
        for (int j = 0; j < p; j++) {
            x[j] = Double.valueOf(tokens[2 * j + 2]);
        }
        Datum<double[]> datum = new Datum<>(x);
        datum.name = tokens[1];
        datum.description = tokens[0];
        data.add(datum);
    }
    reader.close();
    return data;
}
Also used : AttributeDataset(smile.data.AttributeDataset) Datum(smile.data.Datum) InputStreamReader(java.io.InputStreamReader) Attribute(smile.data.Attribute) NumericAttribute(smile.data.NumericAttribute) IOException(java.io.IOException) NumericAttribute(smile.data.NumericAttribute) BufferedReader(java.io.BufferedReader)

Example 28 with Attribute

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

the class ArffParserTest method testParseString.

/**
     * Test of parse method, of class ArffParser.
     */
@Test
public void testParseString() throws Exception {
    System.out.println("string");
    try {
        ArffParser arffParser = new ArffParser();
        AttributeDataset string = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/string.arff"));
        double[][] x = string.toArray(new double[string.size()][]);
        for (Attribute attribute : string.attributes()) {
            assertEquals(Attribute.Type.STRING, attribute.getType());
        }
        Attribute[] attributes = string.attributes();
        assertEquals(5, string.size());
        assertEquals(2, attributes.length);
        assertEquals("AG5", attributes[0].toString(x[0][0]));
        assertEquals("Encyclopedias and dictionaries.;Twentieth century.", attributes[1].toString(x[0][1]));
        assertEquals("AS281", attributes[0].toString(x[4][0]));
        assertEquals("Astronomy, Assyro-Babylonian.;Moon -- Tables.", attributes[1].toString(x[4][1]));
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 29 with Attribute

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

the class DelimitedTextParserTest method testParse.

/**
     * Test of parse method, of class DelimitedTextParser.
     */
@Test
public void testParse() throws Exception {
    System.out.println("parse");
    try {
        DelimitedTextParser parser = new DelimitedTextParser();
        parser.setResponseIndex(new NominalAttribute("class"), 0);
        AttributeDataset usps = parser.parse("USPS Train", smile.data.parser.IOUtils.getTestDataFile("usps/zip.train"));
        double[][] x = usps.toArray(new double[usps.size()][]);
        int[] y = usps.toArray(new int[usps.size()]);
        assertEquals(Attribute.Type.NOMINAL, usps.response().getType());
        for (Attribute attribute : usps.attributes()) {
            assertEquals(Attribute.Type.NUMERIC, attribute.getType());
        }
        assertEquals(7291, usps.size());
        assertEquals(256, usps.attributes().length);
        assertEquals("6", usps.response().toString(y[0]));
        assertEquals("5", usps.response().toString(y[1]));
        assertEquals("4", usps.response().toString(y[2]));
        assertEquals(-1.0000, x[0][6], 1E-7);
        assertEquals(-0.6310, x[0][7], 1E-7);
        assertEquals(0.8620, x[0][8], 1E-7);
        assertEquals("1", usps.response().toString(y[7290]));
        assertEquals(-1.0000, x[7290][4], 1E-7);
        assertEquals(-0.1080, x[7290][5], 1E-7);
        assertEquals(1.0000, x[7290][6], 1E-7);
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : AttributeDataset(smile.data.AttributeDataset) NominalAttribute(smile.data.NominalAttribute) NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) Test(org.junit.Test)

Example 30 with Attribute

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

the class DateFeatureTest method testF.

/**
     * Test of f method, of class DateFeature.
     */
@Test
public void testF() {
    System.out.println("f");
    double[][] result = { { 2001.0, 3.0, 3.0, 2.0, 12.0, 12.0, 12.0 }, { 2001.0, 4.0, 3.0, 4.0, 12.0, 59.0, 55.0 } };
    try {
        ArffParser parser = new ArffParser();
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/date.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        DateFeature.Type[] features = { DateFeature.Type.YEAR, DateFeature.Type.MONTH, DateFeature.Type.DAY_OF_MONTH, DateFeature.Type.DAY_OF_WEEK, DateFeature.Type.HOURS, DateFeature.Type.MINUTES, DateFeature.Type.SECONDS };
        DateFeature df = new DateFeature(data.attributes(), features);
        Attribute[] attributes = df.attributes();
        assertEquals(features.length, 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] = df.f(x[i], j);
                assertEquals(result[i][j], y[j], 1E-7);
            }
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}
Also used : ArffParser(smile.data.parser.ArffParser) AttributeDataset(smile.data.AttributeDataset) Attribute(smile.data.Attribute) Test(org.junit.Test)

Aggregations

Attribute (smile.data.Attribute)35 AttributeDataset (smile.data.AttributeDataset)29 Test (org.junit.Test)24 NominalAttribute (smile.data.NominalAttribute)15 ArffParser (smile.data.parser.ArffParser)8 DelimitedTextParser (smile.data.parser.DelimitedTextParser)8 BufferedReader (java.io.BufferedReader)7 InputStreamReader (java.io.InputStreamReader)7 NumericAttribute (smile.data.NumericAttribute)7 IOException (java.io.IOException)6 Datum (smile.data.Datum)4 GridLayout (java.awt.GridLayout)3 ArrayList (java.util.ArrayList)3 JPanel (javax.swing.JPanel)3 DateAttribute (smile.data.DateAttribute)3 StringAttribute (smile.data.StringAttribute)3 PlotCanvas (smile.plot.PlotCanvas)3 Reader (java.io.Reader)2 StreamTokenizer (java.io.StreamTokenizer)2 ParseException (java.text.ParseException)1