#Validation KNN algorithm

35 messages · Page 1 of 1 (latest)

scarlet parcel
#

I'm trying to validate my KNN function but following these instructions. I'm not sure if I've done any of it correct, could someone plese check my code for me?

dark orioleBOT
#

This post has been reserved for your question.

Hey @scarlet parcel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

zinc cobalt
#

shouldn't the method return boolean instead of Boolean?

#

and that doesn't seem to be valid Java code in your file with all the text outside comments and missing characters

#

Can you paste the actual code using a Discord codeblock?

scarlet parcel
zinc cobalt
#

maybe split it up?

scarlet parcel
#

yeah i'll do that now

zinc cobalt
#

like, your file seems to be missing all special characters

#

or many of them

scarlet parcel
#

o

#

i'll rewrite my file

zinc cobalt
#

like <>/

scarlet parcel
#
    static public Boolean validateDataFormat(ArrayList<String[]> data, Boolean predicted) {
        boolean formatCorrect = false;

        // Check that the first three column headers are "Path", "ActualClass", and "PredictedClass" if predicted is true
        // or just "Path" and "ActualClass" if predicted is false
        String[] expectedHeaders = predicted ? new String[]{"Path", "ActualClass", "PredictedClass"} : new String[]{"Path", "ActualClass"};
        String[] actualHeaders = data.get(0);
        if (actualHeaders.length < expectedHeaders.length) {
            return false;
        }
        for (int i = 0; i < expectedHeaders.length; i++) {
            if (!actualHeaders[i].equals(expectedHeaders[i])) {
                return false;
            }
        }
        
        // Check that all values in the "Path" column are file paths
        for (int i = 1; i < data.size(); i++) {
            String[] row = data.get(i);
            String path = row[0];
            if (!path.startsWith("/")) {
                return false;
            }
        }
#
        // Check that all values in the "ActualClass" and "PredictedClass" columns (if predicted is true) are classes from the scheme
        for (int i = 1; i < data.size(); i++) {
            String[] row = data.get(i);
            String actualClass = row[1];
            if (!Arrays.asList(classificationScheme).contains(actualClass)) {
                return false;
            }
            if (predicted) {
                String predictedClass = row[2];
                if (!Arrays.asList(classificationScheme).contains(predictedClass)) {
                    return false;
                }
            }
        }
        
        // Check that there are as many Path entries as ActualClass and PredictedClass entries (if predicted is true)
        for (int i = 1; i < data.size(); i++) {
            String[] row = data.get(i);
            if (predicted) {
                if (row.length != 3) {
                    return false;
                }
            } else {
                if (row.length != 2) {
                    return false;
                }
            }
        }
        
        return formatCorrect;
    }
zinc cobalt
#

if(actualHeaders.length<expectedHeaders.length)
Why did you use < instead of != here?

#

What is classificationScheme?

scarlet parcel
zinc cobalt
#

and is your definition of a valid path really just starting with /?

scarlet parcel
#

um

#

i think the max i can have is 3

zinc cobalt
#

your code would allow that for the title row

scarlet parcel
#

i'm guessing my code is very wrong

zinc cobalt
#

well, it seems pretty ok

#

just a few things I would do differently

#

like using != instead of < - that woukd forve it to be exactly 2/3 entries in the array

#

but overall, it seems fine

scarlet parcel
#

oh ok

zinc cobalt
#

but test it

scarlet parcel
#

great thank you

#

okie 😶

zinc cobalt
#

and good luck with k-nearest-neighbours

scarlet parcel
#

thank you