use of smile.data.NominalAttribute in project smile by haifengl.
the class ClassificationDemo method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if ("startButton".equals(e.getActionCommand())) {
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 ]+");
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
System.err.println(ex);
}
}
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
if (data.length < 500) {
pointLegend = 'o';
} else {
pointLegend = '.';
}
PlotCanvas canvas = ScatterPlot.plot(data, pointLegend);
for (int i = 0; i < data.length; i++) {
canvas.point(pointLegend, Palette.COLORS[label[i]], data[i]);
}
BorderLayout layout = (BorderLayout) getLayout();
remove(layout.getLayoutComponent(BorderLayout.CENTER));
add(canvas, BorderLayout.CENTER);
validate();
}
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class GrowingNeuralGasTest method testUSPS.
/**
* Test of learn method, of class GrowingNeuralGas.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
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()]);
GrowingNeuralGas gng = new GrowingNeuralGas(x[0].length);
for (int i = 0; i < 10; i++) {
int[] index = Math.permutate(x.length);
for (int j = 0; j < x.length; j++) {
gng.update(x[index[j]]);
}
}
gng.partition(10);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
int[] p = new int[x.length];
for (int i = 0; i < x.length; i++) {
p[i] = gng.predict(x[i]);
}
double r = rand.measure(y, p);
double r2 = ari.measure(y, p);
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.85);
assertTrue(r2 > 0.40);
p = new int[testx.length];
for (int i = 0; i < testx.length; i++) {
p[i] = gng.predict(testx[i]);
}
r = rand.measure(testy, p);
r2 = ari.measure(testy, p);
System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.85);
assertTrue(r2 > 0.40);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class NeuralMapTest method testUSPS.
/**
* Test of learn method, of class NeuralMap.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
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()]);
NeuralMap cortex = new NeuralMap(x[0].length, 8.0, 0.05, 0.0006, 5, 3);
for (int i = 0; i < 5; i++) {
for (double[] xi : x) {
cortex.update(xi);
}
}
cortex.purge(16);
cortex.partition(10);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
int[] p = new int[x.length];
for (int i = 0; i < x.length; i++) {
p[i] = cortex.predict(x[i]);
}
double r = rand.measure(y, p);
double r2 = ari.measure(y, p);
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
//assertTrue(r > 0.65);
//assertTrue(r2 > 0.18);
p = new int[testx.length];
for (int i = 0; i < testx.length; i++) {
p[i] = cortex.predict(testx[i]);
}
r = rand.measure(testy, p);
r2 = ari.measure(testy, p);
System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
//assertTrue(r > 0.65);
//assertTrue(r2 > 0.18);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class SOMTest method testUSPS.
/**
* Test of learn method, of class SOM.
*/
@Test
public void testUSPS() {
System.out.println("USPS");
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
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()]);
SOM som = new SOM(x, 10, 10);
int[] label = som.partition(10);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
double r = rand.measure(y, label);
double r2 = ari.measure(y, label);
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.88);
assertTrue(r2 > 0.45);
int[] p = new int[testx.length];
for (int i = 0; i < testx.length; i++) {
p[i] = som.predict(testx[i]);
}
r = rand.measure(testy, p);
r2 = ari.measure(testy, p);
System.out.format("Testing rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.88);
assertTrue(r2 > 0.45);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.NominalAttribute in project smile by haifengl.
the class ArffParser method parse.
/**
* Parse a dataset from given stream.
*/
public AttributeDataset parse(InputStream stream) throws IOException, ParseException {
try (Reader r = new BufferedReader(new InputStreamReader(stream))) {
StreamTokenizer tokenizer = new StreamTokenizer(r);
initTokenizer(tokenizer);
List<Attribute> attributes = new ArrayList<>();
String relationName = readHeader(tokenizer, attributes);
if (attributes.isEmpty()) {
throw new IOException("no header information available");
}
Attribute response = null;
Attribute[] attr = new Attribute[attributes.size()];
attributes.toArray(attr);
for (int i = 0; i < attributes.size(); i++) {
if (responseIndex == i) {
response = attributes.remove(i);
break;
}
}
AttributeDataset data = new AttributeDataset(relationName, attributes.toArray(new Attribute[attributes.size()]), response);
while (true) {
// Check if end of file reached.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
break;
}
// Parse instance
if (tokenizer.ttype == '{') {
data.add(getSparseInstance(tokenizer, attr));
} else {
data.add(getInstance(tokenizer, attr));
}
}
for (Attribute attribute : attributes) {
if (attribute instanceof NominalAttribute) {
NominalAttribute a = (NominalAttribute) attribute;
a.setOpen(false);
}
if (attribute instanceof StringAttribute) {
StringAttribute a = (StringAttribute) attribute;
a.setOpen(false);
}
}
return data;
}
}
Aggregations