* @param k The number of folds.
* @param shuffle True if we should shuffle.
* @return The trained method.
*/
public MLMethod crossvalidate(int k, boolean shuffle) {
KFoldCrossvalidation cross = new KFoldCrossvalidation(
this.trainingDataset, k);
cross.process(shuffle);
int foldNumber = 0;
for (DataFold fold : cross.getFolds()) {
foldNumber++;
report.report(k, foldNumber, "Fold #" + foldNumber);
fitFold(k, foldNumber, fold);
}
double sum = 0;
double bestScore = Double.POSITIVE_INFINITY;
MLMethod bestMethod = null;
for (DataFold fold : cross.getFolds()) {
sum += fold.getScore();
if (fold.getScore() < bestScore) {
bestScore = fold.getScore();
bestMethod = fold.getMethod();
}
}
sum = sum / cross.getFolds().size();
report.report(k, k, "Cross-validated score:" + sum);
return bestMethod;
}