【问题标题】:Array Out of Bounds in K-Fold algorithmK-Fold 算法中的数组越界
【发布时间】:2020-08-24 10:40:11
【问题描述】:

我在运行我的项目时遇到问题,错误是数组超出范围,您能帮帮我吗? 这是我的方法

public double hitungAkurasi(TrainRecord[] trainRecords, TestRecord[] testRecords, int K) {
    if (K <= 0) {
        System.out.println("K should be larger than 0!");
        return -1;
    }

    //normalisasi
    TrainRecord[] trainingSet = trainRecords;
    TestRecord[] testingSet = testRecords;

    //System.out.println("Test" + testRecords);

    trainingSet = (TrainRecord[]) normalisasi(trainingSet);
    testingSet = (TestRecord[]) normalisasi(testingSet);

    Record[] join = trainingSet;


    //test those TestRecords one by one
    int numOfTestingRecord = testingSet.length;
    int numOfTrainingRecord = trainingSet.length;
    int sumNumOfRecord = numOfTestingRecord + numOfTrainingRecord;

    //train record
    for (int i = 0; i < (sumNumOfRecord) / K; i++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet, testingSet[i], K);
        int classLabel = classify(neighbors);
        testingSet[i].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }
    for (int j = ((numOfTrainingRecord + 1) * sumNumOfRecord) / K; j < sumNumOfRecord; j++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet, testingSet[j], K);
        int classLabel = classify(neighbors);
        testingSet[j].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }

    //test record
    for (int t = (numOfTestingRecord * sumNumOfRecord) / K; t < ((numOfTestingRecord + 1) * sumNumOfRecord) / K; t++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet, testingSet[t], K);
        int classLabel = classify(neighbors);
        testingSet[t].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }

    int correctPrediction = 0;
    for (int l = 0; l < numOfTestingRecord; l++) {

        if (testingSet[l].predictedLabel == testingSet[l].classLabel) {
            correctPrediction++;
        }
    }

    return ((double) correctPrediction / numOfTestingRecord) * 100;
}

当我尝试禁用循环程序成功成功的测试记录时,但是当我启用该循环时,我有一个错误 Array out of bounds in here

TrainRecord[] neighbors = findKNearestNeighbors(trainingSet, testingSet[t], K);

你能帮我解决这个问题吗?

【问题讨论】:

    标签: java arrays spring intellij-idea k-fold


    【解决方案1】:

    如果它在你的“测试记录”循环中抛出一个“数组索引越界”,这意味着你的索引 t 在某些时候取的值大于你设置的数组的大小正在该循环中访问。您可以检查此修改循环以打印这些值和/或检查 t 是否超出范围:

    for (int t = (numOfTestingRecord * sumNumOfRecord) / K; t < ((numOfTestingRecord + 1) * sumNumOfRecord) / K; t++){
            System.out.println("Array size: "+testingSet.length);
            System.out.println("Current index: "+t);   
            if (t > testingSet.length) {
                System.out.println("If you see this message you are in trouble");    
            }    
            TrainRecord[] neighbors = findKNearestNeighbors(trainingSet, testingSet[t], K);
            int classLabel = classify(neighbors);
            testingSet[t].predictedLabel = classLabel; //assign the predicted label to TestRecord
        }
    

    您可能需要更改 t 的初始化或结束循环的条件。

    【讨论】:

    • 我看到了消息先生,那我应该怎么做才能解决这个问题?
    • 嗯,你应该已经看到 t 比你的数组的大小要大。正如我所说,t 的初始化或终止循环的条件允许 t 具有超过数组大小的值,您应该更改它们以确保它不会发生。在您看到消息之前,循环运行了多少次?看到消息时数组的大小和t的值是多少?
    • 数组大小为 122,索引为 14859 先生。索引怎么可能超出数组,即使我手动计算没有超出数组,
    • 这意味着在某一时刻 (numOfTestingRecord*sumNumOfRecord)/K 是 14859。如果我正确地遵循了你的代码,numOfTestingRecord 已经保存了你用 t 索引的数组的大小。然后你将它进一步乘以 sumNumOfRecord ,这是你的两个数组的长度之和。即使你将它除以 K,它显然会给你带来问题,所以你应该再次验证你的计算。因为你的问题的根本原因是 t 的值超过了数组的大小,所以你必须确保当 t 取值时它不能大于数组的大小。
    • 哦,我知道了,先生,好的,我会再试一次,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2011-06-23
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多