diff --git a/src/mlpack/core/cv/k_fold_cv.hpp b/src/mlpack/core/cv/k_fold_cv.hpp index 9cb274eda7..bc3671cf8f 100644 --- a/src/mlpack/core/cv/k_fold_cv.hpp +++ b/src/mlpack/core/cv/k_fold_cv.hpp @@ -181,6 +181,9 @@ class KFoldCV //! The extended (by repeating the first k - 2 bins) weights. WeightsType weights; + //! The original size of the dataset. + size_t lastBinSize; + //! The size of each bin in terms of data points. size_t binSize; @@ -219,7 +222,7 @@ class KFoldCV /** * Train and run evaluation in the case of non-weighted learning. */ - template::type> double TrainAndEvaluate(const MLAlgorithmArgs& ...mlAlgorithmArgs); diff --git a/src/mlpack/core/cv/k_fold_cv_impl.hpp b/src/mlpack/core/cv/k_fold_cv_impl.hpp index 0864ad9ece..553dea17cc 100644 --- a/src/mlpack/core/cv/k_fold_cv_impl.hpp +++ b/src/mlpack/core/cv/k_fold_cv_impl.hpp @@ -207,6 +207,7 @@ void KFoldCV::ValidationSubsetFirstCol(const size_t i) { - return (i < k - 1) ? (binSize * i + trainingSubsetSize) : (binSize * (i - 1)); + // Use as close to the beginning of the dataset as we can. + return (i == 0) ? trainingSubsetSize : binSize * (i - 1); } template KFoldCV& m, const size_t i) { - return arma::Mat(m.colptr(binSize * i), m.n_rows, - trainingSubsetSize, false, true); + // If this is the last fold, we have to handle it a little bit differently, + // since the last fold may not contain 'binSize' points. + const size_t subsetSize = (i == k - 1) ? lastBinSize + (k - 2) * binSize : + trainingSubsetSize; + + return arma::Mat(m.colptr(binSize * i), m.n_rows, subsetSize, + false, true); } template KFoldCV& r, const size_t i) { - return arma::Row(r.colptr(binSize * i), trainingSubsetSize, - false, true); + // If this is the last fold, we have to handle it a little bit differently, + // since the last fold may not contain 'binSize' points. + const size_t subsetSize = (i == k - 1) ? lastBinSize + (k - 2) * binSize : + trainingSubsetSize; + + return arma::Row(r.colptr(binSize * i), subsetSize, false, true); } template KFoldCV& m, const size_t i) { + const size_t subsetSize = (i == 0) ? lastBinSize : binSize; return arma::Mat(m.colptr(ValidationSubsetFirstCol(i)), m.n_rows, - binSize, false, true); + subsetSize, false, true); } template KFoldCV& r, const size_t i) { - return arma::Row(r.colptr(ValidationSubsetFirstCol(i)), binSize, - false, true); + const size_t subsetSize = (i == 0) ? lastBinSize : binSize; + return arma::Row(r.colptr(ValidationSubsetFirstCol(i)), + subsetSize, false, true); } } // namespace cv