smile.validation.LOOCV Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2010 Haifeng Li
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package smile.validation;
/**
* Leave-one-out cross validation. LOOCV involves using a single observation
* from the original sample as the validation data, and the remaining
* observations as the training data. This is repeated such that each
* observation in the sample is used once as the validation data. This is
* the same as a K-fold cross-validation with K being equal to the number of
* observations in the original sample. Leave-one-out cross-validation is
* usually very expensive from a computational point of view because of the
* large number of times the training process is repeated.
*
* @author Haifeng Li
*/
public class LOOCV {
/**
* The index of training instances.
*/
public final int[][] train;
/**
* The index of testing instances.
*/
public final int[] test;
/**
* Constructor.
* @param n the number of samples.
*/
public LOOCV(int n) {
if (n < 0) {
throw new IllegalArgumentException("Invalid sample size: " + n);
}
train = new int[n][n-1];
test = new int[n];
for (int i = 0; i < n; i++) {
test[i] = i;
for (int j = 0, p = 0; j < n; j++) {
if (j != i) {
train[i][p++] = j;
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy