use of smile.classification.FLD in project smile by haifengl.
the class FLDDemo method learn.
@Override
public double[][] learn(double[] x, double[] y) {
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
FLD fisher = new FLD(data, label);
for (int i = 0; i < label.length; i++) {
label[i] = fisher.predict(data[i]);
}
double trainError = error(label, label);
System.out.format("training error = %.2f%%\n", 100 * trainError);
double[][] z = new double[y.length][x.length];
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++) {
double[] p = { x[j], y[i] };
z[i][j] = fisher.predict(p);
}
}
return z;
}
use of smile.classification.FLD in project smile by haifengl.
the class LDADemo method learn.
/**
* Execute the projection algorithm and return a swing JComponent representing
* the clusters.
*/
public JComponent learn() {
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
if (names[0] == null) {
names = null;
}
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
int min = Math.min(label);
for (int i = 0; i < label.length; i++) {
label[i] -= min;
}
long clock = System.currentTimeMillis();
FLD lda = new FLD(data, label, Math.unique(label).length > 3 ? 3 : 2);
System.out.format("Learn LDA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
double[][] y = lda.project(data);
PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
if (names != null) {
plot.points(y, names);
} else if (dataset[datasetIndex].response() != null) {
int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
for (int i = 0; i < y.length; i++) {
plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
}
} else {
plot.points(y, pointLegend);
}
plot.setTitle("Linear Discriminant Analysis");
return plot;
}
Aggregations