use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class AdaBoostTest method testUSPS10.
/**
* Test of learn method, of class AdaBoost.
*/
@Test
public void testUSPS10() {
System.out.println("USPS 10 classes");
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()]);
AdaBoost forest = new AdaBoost(x, y, 100, 64);
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (forest.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.println("AdaBoost error = " + error);
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
double[] accuracy = forest.test(testx, testy);
for (int i = 1; i <= accuracy.length; i++) {
System.out.format("%d trees accuracy = %.2f%%%n", i, 100.0 * accuracy[i - 1]);
}
double[] importance = forest.importance();
int[] index = QuickSort.sort(importance);
for (int i = importance.length; i-- > 0; ) {
System.out.format("%s importance is %.4f%n", train.attributes()[index[i]], importance[i]);
}
assertTrue(error <= 170);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class NeuralNetworkTest method testUSPSLMS.
/**
* Test of learn method, of class NeuralNetwork.
*/
@Test
public void testUSPSLMS() {
System.out.println("USPS LMS");
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()]);
int p = x[0].length;
double[] mu = Math.colMean(x);
double[] sd = Math.colSd(x);
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < p; j++) {
x[i][j] = (x[i][j] - mu[j]) / sd[j];
}
}
for (int i = 0; i < testx.length; i++) {
for (int j = 0; j < p; j++) {
testx[i][j] = (testx[i][j] - mu[j]) / sd[j];
}
}
NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.LEAST_MEAN_SQUARES, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, x[0].length, 40, Math.max(y) + 1);
for (int j = 0; j < 30; j++) {
net.learn(x, y);
}
int error = 0;
for (int i = 0; i < testx.length; i++) {
if (net.predict(testx[i]) != testy[i]) {
error++;
}
}
System.out.format("USPS error rate = %.2f%%%n", 100.0 * error / testx.length);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class XMeansTest method testUSPS.
/**
* Test of learn method, of class XMeans.
*/
@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()]);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
XMeans xmeans = new XMeans(x, 10);
double r = rand.measure(y, xmeans.getClusterLabel());
double r2 = ari.measure(y, xmeans.getClusterLabel());
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.4);
int[] p = new int[testx.length];
for (int i = 0; i < testx.length; i++) {
p[i] = xmeans.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.4);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class MDSDemo method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if ("startButton".equals(e.getActionCommand())) {
datasetIndex = datasetBox.getSelectedIndex();
if (dataset[datasetIndex] == null) {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setDelimiter("[\t]+");
parser.setRowNames(true);
parser.setColumnNames(true);
if (datasetIndex == 2 || datasetIndex == 3) {
parser.setRowNames(false);
}
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);
}
}
Thread thread = new Thread(this);
thread.start();
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class ManifoldDemo method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if ("startButton".equals(e.getActionCommand())) {
datasetIndex = datasetBox.getSelectedIndex();
if (dataset[datasetIndex] == null) {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setDelimiter("[\t]+");
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()][]);
if (data.length < 500) {
pointLegend = 'o';
} else {
pointLegend = '.';
}
try {
k = Integer.parseInt(knnField.getText().trim());
if (k < 2 || k > 30) {
JOptionPane.showMessageDialog(this, "Invalid K: " + k, "Error", JOptionPane.ERROR_MESSAGE);
return;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid K: " + knnField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Thread thread = new Thread(this);
thread.start();
}
}
Aggregations