使用用java编写的k-fold进行交叉验证

时间:2018-06-08 15:23:31

标签: java data-science cross-validation

当我尝试使用java创建一个简单的k-folds应用程序时,它使用表示数据索引的数字数组,并且还需要折叠次数 它返回一个包含折叠数组的数组(第J次训练和第J次测试索引分别存储为2 * J和2 * J + 1个元素) 例如,指数= [1,2,3]和K = 2 输出应为[[1,2],[3],[3],[1,2]] 所以我做了这段代码:

private static int[][] solution(int[] indices, int K){
    int rowLimit = (int)Math.ceil((double)indices.length/K);
    int[][] output = new int[rowLimit*2][];
    int indicesLength = indices.length;
    int countIndices = 0;

    for(int i=0; i<rowLimit; i++){
        if (indicesLength<K){
            int lastArrayLimit = indicesLength;
            output[i] = new int[indicesLength];
            for(int l=0; l<lastArrayLimit; l++){
                output[i][l]=indices[countIndices];
                countIndices++;
                indicesLength--;
            }
        }
        else{
            output[i] = new int[K];
            for(int j=0; j<K; j++){
                output[i][j]=indices[countIndices];
                countIndices++;
                indicesLength--;
            }
        }
    }

    int limit = output.length;
    for (int i = 0; i<rowLimit; i++){
        output[limit-1] = output[i];
        limit--;
    }

    return output;
}

但是当我尝试提交时,它不被接受。 任何人都可以告诉我它有什么问题,因为它会按顺序生成所需的输出。 谢谢。

0 个答案:

没有答案
相关问题