use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class NumericAttributeFeatureTest method testNORMALIZATION.
/**
* Test of f method, of class NumericAttributeFeature.
*/
@Test
public void testNORMALIZATION() {
System.out.println("NORMALIZATION");
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()][]);
double[] min = Math.colMin(x);
double[] max = Math.colMax(x);
NumericAttributeFeature naf = new NumericAttributeFeature(data.attributes(), NumericAttributeFeature.Scaling.NORMALIZATION, x);
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((x[i][j] - min[j]) / (max[j] - min[j]), y[j], 1E-7);
}
}
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.DelimitedTextParser 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);
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class SumSquaresRatioTest method testLearn.
/**
* Test of learn method, of class SumSquaresRatio.
*/
@Test
public void testLearn() {
System.out.println("USPS");
try {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setResponseIndex(new NominalAttribute("class"), 0);
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()]);
SumSquaresRatio ssr = new SumSquaresRatio();
double[] score = ssr.rank(x, y);
int[] index = QuickSort.sort(score);
int p = 135;
int n = x.length;
double[][] xx = new double[n][p];
for (int j = 0; j < p; j++) {
for (int i = 0; i < n; i++) {
xx[i][j] = x[i][index[255 - j]];
}
}
int testn = testx.length;
double[][] testxx = new double[testn][p];
for (int j = 0; j < p; j++) {
for (int i = 0; i < testn; i++) {
testxx[i][j] = testx[i][index[255 - j]];
}
}
LDA lda = new LDA(xx, y);
int[] prediction = new int[testn];
for (int i = 0; i < testn; i++) {
prediction[i] = lda.predict(testxx[i]);
}
double accuracy = new Accuracy().measure(testy, prediction);
System.out.format("SSR %.2f%%%n", 100 * accuracy);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.data.parser.DelimitedTextParser in project smile by haifengl.
the class VQDemo 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.parser.DelimitedTextParser in project smile by haifengl.
the class ValidationTest method testTest_3args_1.
/**
* Test of test method, of class Validation.
*/
@Test
public void testTest_3args_1() {
System.out.println("test");
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()]);
LDA lda = new LDA(x, y);
double accuracy = Validation.test(lda, testx, testy);
System.out.println("accuracy = " + accuracy);
assertEquals(0.8724, accuracy, 1E-4);
} catch (Exception ex) {
System.err.println(ex);
}
}
Aggregations