Why does `kNearestNeighbours` return more neighbours than `m_kNN` specified?

Lorenzo Ferron <[email protected]> Wed, 14 Feb 2024 12:08:58 +0100
Newsgroups gmane.comp.ai.weka
Message-ID <[email protected]>
Dear WEKA Support Team,

I want to perform k-NN on Combined-Cycle Power Plant dataset 
<https://archive.ics.uci.edu/dataset/294/combined+cycle+power+plant>. I 
chose "Sheet 1", here <https://pastebin.com/raw/TH5uY094> you can find 
the CSV. The last column is the target.


        How to reproduce my issue?

 1. Load the dataset.
 2. Choose as filter weka.filters.unsupervised.attribute.Standardize to
    standardize it.
 3. Go to Classify tab.
 4. Set Percentage split to 80%.
 5. Click on More options... button.
     1. Enable Preserve order for % Split
     2. Paste weka.classifiers.evaluation.output.prediction.CSV
        -use-tabin Output predictions.
 6. Paste weka.classifiers.lazy.IBk -K 4 -W 0 -E -A
    "weka.core.neighboursearch.LinearNNSearch -A
    \"weka.core.EuclideanDistance -D -R first-4\" -S" -output-debug-info
    as classifier.
 7. Start the classification.

The second instance has a predicted value of 443.112. But 
KNeighborsRegressor from Scikit-Learn predicts 443.8775. I debugged WEKA 
and I discovered that m_NNSearch.kNearestNeighbours 
<https://git.cms.waikato.ac.nz/weka/weka/-/blob/3cbb52f2172daeab749aedd5afbe1463775ca361/trunk/weka/src/main/java/weka/classifiers/lazy/IBk.java#L585> 
sometimes returns 5 neighbours and not 4 even if m_kNN 
<https://git.cms.waikato.ac.nz/weka/weka/-/blob/3cbb52f2172daeab749aedd5afbe1463775ca361/trunk/weka/src/main/java/weka/classifiers/lazy/IBk.java#L132> 
is 4. But why? It seems that the size of m_Distances 
<https://git.cms.waikato.ac.nz/weka/weka/-/blob/3cbb52f2172daeab749aedd5afbe1463775ca361/trunk/weka/src/main/java/weka/core/neighboursearch/LinearNNSearch.java#L246> 
is sometimes 5 and not 4. Because heap.size() 
<https://git.cms.waikato.ac.nz/weka/weka/-/blob/3cbb52f2172daeab749aedd5afbe1463775ca361/trunk/weka/src/main/java/weka/core/neighboursearch/NearestNeighbourSearch.java#L80> 
is 4 while heap.noOfKthNearest() 
<https://git.cms.waikato.ac.nz/weka/weka/-/blob/3cbb52f2172daeab749aedd5afbe1463775ca361/trunk/weka/src/main/java/weka/core/neighboursearch/NearestNeighbourSearch.java#L160> 
is 1 and their sum is 5.

So this is right behaviour or not?

Kind regards,

Lorenzo

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html