use of smile.validation.LOOCV in project smile by haifengl.
the class NaiveBayesTest method testPredict.
/**
* Test of predict method, of class NaiveBayes.
*/
@Test
public void testPredict() {
System.out.println("predict");
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()]);
int n = x.length;
LOOCV loocv = new LOOCV(n);
int error = 0;
for (int l = 0; l < n; l++) {
double[][] trainx = Math.slice(x, loocv.train[l]);
int[] trainy = Math.slice(y, loocv.train[l]);
int p = trainx[0].length;
int k = Math.max(trainy) + 1;
double[] priori = new double[k];
Distribution[][] condprob = new Distribution[k][p];
for (int i = 0; i < k; i++) {
priori[i] = 1.0 / k;
for (int j = 0; j < p; j++) {
ArrayList<Double> axi = new ArrayList<>();
for (int m = 0; m < trainx.length; m++) {
if (trainy[m] == i) {
axi.add(trainx[m][j]);
}
}
double[] xi = new double[axi.size()];
for (int m = 0; m < xi.length; m++) {
xi[m] = axi.get(m);
}
condprob[i][j] = new GaussianMixture(xi, 3);
}
}
NaiveBayes bayes = new NaiveBayes(priori, condprob);
if (y[loocv.test[l]] != bayes.predict(x[loocv.test[l]]))
error++;
}
System.out.format("Iris error rate = %.2f%%%n", 100.0 * error / x.length);
assertEquals(5, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class NeuralNetworkTest method testIris2.
/**
* Test of learn method, of class NeuralNetwork.
*/
@Test
public void testIris2() {
System.out.println("Iris binary");
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()]);
for (int i = 0; i < y.length; i++) {
if (y[i] == 2) {
y[i] = 1;
} else {
y[i] = 0;
}
}
int n = x.length;
int p = x[0].length;
double[] mu = Math.colMean(x);
double[] sd = Math.colSd(x);
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
x[i][j] = (x[i][j] - mu[j]) / sd[j];
}
}
LOOCV loocv = new LOOCV(n);
int error = 0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
NeuralNetwork net = new NeuralNetwork(NeuralNetwork.ErrorFunction.CROSS_ENTROPY, NeuralNetwork.ActivationFunction.LOGISTIC_SIGMOID, x[0].length, 10, 1);
for (int j = 0; j < 30; j++) {
net.learn(trainx, trainy);
}
if (y[loocv.test[i]] != net.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Neural network error = " + error);
assertTrue(error <= 8);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class DecisionTreeTest method testIris.
/**
* Test of learn method, of class DecisionTree.
*/
@Test
public void testIris() {
System.out.println("Iris");
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()]);
int n = x.length;
LOOCV loocv = new LOOCV(n);
int error = 0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
DecisionTree tree = new DecisionTree(iris.attributes(), trainx, trainy, 4);
if (y[loocv.test[i]] != tree.predict(x[loocv.test[i]]))
error++;
}
System.out.println("Decision Tree error = " + error);
assertEquals(7, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class FLDTest method testPredict.
/**
* Test of predict method, of class FDA.
*/
@Test
public void testPredict() {
System.out.println("IRIS");
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()]);
int n = x.length;
LOOCV loocv = new LOOCV(n);
int error = 0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
FLD fisher = new FLD(trainx, trainy);
if (y[loocv.test[i]] != fisher.predict(x[loocv.test[i]]))
error++;
}
System.out.println("FLD error = " + error);
assertEquals(5, error);
} catch (Exception ex) {
System.err.println(ex);
}
}
use of smile.validation.LOOCV in project smile by haifengl.
the class RidgeRegressionTest method testLearn.
/**
* Test of learn method, of class RidgeRegression.
*/
@Test
public void testLearn() {
System.out.println("learn");
RidgeRegression model = new RidgeRegression(longley, y, 0.0);
double rss = 0.0;
int n = longley.length;
for (int i = 0; i < n; i++) {
double r = y[i] - model.predict(longley[i]);
assertEquals(residuals[i], r, 1E-7);
rss += r * r;
}
System.out.println("Training MSE = " + rss / n);
model = new RidgeRegression(longley, y, 0.1);
assertEquals(-1.354007e+03, model.intercept(), 1E-3);
assertEquals(5.457700e-02, model.coefficients()[0], 1E-7);
assertEquals(1.198440e-02, model.coefficients()[1], 1E-7);
assertEquals(1.261978e-02, model.coefficients()[2], 1E-7);
assertEquals(-1.856041e-01, model.coefficients()[3], 1E-7);
assertEquals(7.218054e-01, model.coefficients()[4], 1E-7);
assertEquals(5.884884e-01, model.coefficients()[5], 1E-7);
LOOCV loocv = new LOOCV(n);
rss = 0.0;
for (int i = 0; i < n; i++) {
double[][] trainx = Math.slice(longley, loocv.train[i]);
double[] trainy = Math.slice(y, loocv.train[i]);
RidgeRegression ridge = new RidgeRegression(trainx, trainy, 0.1);
double r = y[loocv.test[i]] - ridge.predict(longley[loocv.test[i]]);
rss += r * r;
}
System.out.println("LOOCV MSE = " + rss / n);
}
Aggregations