Search in sources :

Example 16 with Attribute

use of smile.data.Attribute 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 17 with Attribute

use of smile.data.Attribute 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 18 with Attribute

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

the class SammonMappingDemo method learn.

/**
     * Execute the MDS algorithm and return a swing JComponent representing
     * the clusters.
     */
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(1, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (labels[0] == null) {
        Attribute[] attr = dataset[datasetIndex].attributes();
        labels = new String[attr.length];
        for (int i = 0; i < labels.length; i++) {
            labels[i] = attr[i].getName();
        }
    }
    long clock = System.currentTimeMillis();
    SammonMapping sammon = new SammonMapping(data, 2);
    System.out.format("Learn Sammon's Mapping (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(sammon.getCoordinates(), labels);
    plot.setTitle("Sammon's Mapping (k = 2)");
    pane.add(plot);
    clock = System.currentTimeMillis();
    sammon = new SammonMapping(data, 3);
    System.out.format("Learn Sammon's Mapping (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    plot = ScatterPlot.plot(sammon.getCoordinates(), labels);
    plot.setTitle("Sammon's Mapping (k = 3)");
    pane.add(plot);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) SammonMapping(smile.mds.SammonMapping) Attribute(smile.data.Attribute) PlotCanvas(smile.plot.PlotCanvas)

Example 19 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)

Example 20 with Attribute

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

the class FeatureSetTest method testAttributes.

/**
     * Test of attributes method, of class FeatureSet.
     */
@Test
public void testAttributes() {
    System.out.println("attributes");
    try {
        ArffParser parser = new ArffParser();
        AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/regression/abalone.arff"));
        double[][] x = data.toArray(new double[data.size()][]);
        FeatureSet<double[]> features = new FeatureSet<>();
        features.add(new Nominal2Binary(data.attributes()));
        features.add(new NumericAttributeFeature(data.attributes(), 0.05, 0.95, x));
        Attribute[] attributes = features.attributes();
        assertEquals(11, attributes.length);
        for (int i = 0; i < attributes.length; i++) {
            System.out.println(attributes[i]);
            assertEquals(Attribute.Type.NUMERIC, attributes[i].getType());
        }
    } 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