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);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class Nominal2BinaryTest method testAttributes.
/**
* Test of attributes method, of class Nominal2Binary.
*/
@SuppressWarnings("unused")
@Test
public void testAttributes() {
System.out.println("attributes");
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()][]);
Nominal2Binary n2b = new Nominal2Binary(weather.attributes());
Attribute[] attributes = n2b.attributes();
assertEquals(10, 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);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class Nominal2BinaryTest method testF.
/**
* Test of f method, of class Nominal2Binary.
*/
@Test
public void testF() {
System.out.println("f");
double[][] result = { { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 } };
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()][]);
Nominal2Binary n2b = new Nominal2Binary(weather.attributes());
Attribute[] attributes = n2b.attributes();
for (int i = 0; i < x.length; i++) {
double[] y = new double[attributes.length];
for (int j = 0; j < y.length; j++) {
y[j] = n2b.f(x[i], j);
assertEquals(result[i][j], y[j], 1E-7);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.Attribute in project smile by haifengl.
the class IsotonicMDSDemo 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();
IsotonicMDS isomds = new IsotonicMDS(data, 2);
System.out.format("Learn Kruskal's Nonmetric MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
plot.setTitle("Kruskal's Nonmetric MDS (k = 2)");
pane.add(plot);
clock = System.currentTimeMillis();
isomds = new IsotonicMDS(data, 3);
System.out.format("Learn Kruskal's Nonmetric MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
plot.setTitle("Kruskal's Nonmetric MDS (k = 3)");
pane.add(plot);
return pane;
}
use of smile.data.Attribute in project smile by haifengl.
the class MDSDemo 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();
MDS mds = new MDS(data, 2);
System.out.format("Learn MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(mds.getCoordinates(), labels);
plot.setTitle("MDS (k = 2)");
pane.add(plot);
clock = System.currentTimeMillis();
mds = new MDS(data, 3);
System.out.format("Learn MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
plot = ScatterPlot.plot(mds.getCoordinates(), labels);
plot.setTitle("MDS (k = 3)");
pane.add(plot);
return pane;
}
Aggregations