use of smile.data.AttributeDataset in project smile by haifengl.
the class GCTParserTest method testParse.
/**
* Test of parse method, of class GCTParser.
*/
@Test
public void testParse() throws Exception {
System.out.println("parse");
GCTParser parser = new GCTParser();
try {
AttributeDataset data = parser.parse("GCT", smile.data.parser.IOUtils.getTestDataFile("microarray/allaml.dataset.gct"));
double[][] x = data.toArray(new double[data.size()][]);
String[] id = data.toArray(new String[data.size()]);
for (Attribute attribute : data.attributes()) {
assertEquals(Attribute.Type.NUMERIC, attribute.getType());
System.out.println(attribute.getName());
}
assertEquals(12564, data.size());
assertEquals(48, data.attributes().length);
assertEquals("AFFX-MurIL2_at", id[0]);
assertEquals(-161.8, x[0][0], 1E-7);
assertEquals(-231.0, x[0][1], 1E-7);
assertEquals(-279.0, x[0][2], 1E-7);
assertEquals("128_at", id[12563]);
assertEquals(95.0, x[12563][45], 1E-7);
assertEquals(108.0, x[12563][46], 1E-7);
assertEquals(346.0, x[12563][47], 1E-7);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class ClusteringDemo method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if ("startButton".equals(e.getActionCommand())) {
try {
clusterNumber = Integer.parseInt(clusterNumberField.getText().trim());
if (clusterNumber < 2) {
JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumber, ERROR, JOptionPane.ERROR_MESSAGE);
return;
}
if (clusterNumber > dataset[datasetIndex].length / 2) {
JOptionPane.showMessageDialog(this, "Too large K: " + clusterNumber, ERROR, JOptionPane.ERROR_MESSAGE);
return;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid K: " + clusterNumberField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return;
}
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 ]+");
try {
AttributeDataset data = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
dataset[datasetIndex] = data.toArray(new double[data.size()][]);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
System.err.println(ex);
}
}
remove(canvas);
if (dataset[datasetIndex].length < 500) {
pointLegend = 'o';
} else {
pointLegend = '.';
}
canvas = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
add(canvas, BorderLayout.CENTER);
validate();
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class ValidationTest method testCv_5args_1.
/**
* Test of cv method, of class Validation.
*/
@Test
public void testCv_5args_1() {
System.out.println("cv");
ArffParser arffParser = new ArffParser();
arffParser.setResponseIndex(4);
try {
AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
double[][] x = iris.toArray(new double[iris.size()][]);
int[] y = iris.toArray(new int[iris.size()]);
ClassifierTrainer<double[]> trainer = new LDA.Trainer();
ClassificationMeasure[] measures = { new Accuracy() };
double[] results = Validation.cv(10, trainer, x, y, measures);
for (int i = 0; i < measures.length; i++) {
System.out.println(measures[i] + " = " + results[i]);
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class ValidationTest method testCv_4args_1.
/**
* Test of cv method, of class Validation.
*/
@Test
public void testCv_4args_1() {
System.out.println("cv");
ArffParser arffParser = new ArffParser();
arffParser.setResponseIndex(4);
try {
AttributeDataset iris = arffParser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/iris.arff"));
double[][] x = iris.toArray(new double[iris.size()][]);
int[] y = iris.toArray(new int[iris.size()]);
ClassifierTrainer<double[]> trainer = new LDA.Trainer();
double accuracy = Validation.cv(10, trainer, x, y);
System.out.println("10-fold CV accuracy = " + accuracy);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.AttributeDataset in project smile by haifengl.
the class ValidationTest method testTest_4args_2.
/**
* Test of test method, of class Validation.
*/
@Test
public void testTest_4args_2() {
System.out.println("test");
ArffParser parser = new ArffParser();
parser.setResponseIndex(6);
try {
AttributeDataset data = parser.parse(smile.data.parser.IOUtils.getTestDataFile("weka/cpu.arff"));
double[] datay = data.toArray(new double[data.size()]);
double[][] datax = data.toArray(new double[data.size()][]);
Math.standardize(datax);
int n = datax.length;
int m = 3 * n / 4;
double[][] x = new double[m][];
double[] y = new double[m];
double[][] testx = new double[n - m][];
double[] testy = new double[n - m];
int[] index = Math.permutate(n);
for (int i = 0; i < m; i++) {
x[i] = datax[index[i]];
y[i] = datay[index[i]];
}
for (int i = m; i < n; i++) {
testx[i - m] = datax[index[i]];
testy[i - m] = datay[index[i]];
}
double[][] centers = new double[20][];
RadialBasisFunction[] rbf = SmileUtils.learnGaussianRadialBasis(x, centers, 2);
RBFNetwork<double[]> rkhs = new RBFNetwork<>(x, y, new EuclideanDistance(), rbf, centers);
RegressionMeasure[] measures = { new RMSE(), new AbsoluteDeviation() };
double[] results = Validation.test(rkhs, testx, testy, measures);
System.out.println("RMSE = " + results[0]);
System.out.println("Absolute Deviation = " + results[1]);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations