Fix KNN Regressor weighted prediction - #419
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes incorrect weighted predictions in KNNRegressor by ensuring the per-neighbor weights remain aligned with the corresponding neighbor labels when distances are sorted by proximity.
Changes:
- Key the computed
$weightsby the original neighbor index (as preserved byasort(...)+array_slice(..., true)), instead of building a reindexed list. - Pass
$labelstoStats::weightedMean()without reindexing, so weights/labels are paired by key rather than by position. - Add regression tests covering the misalignment case, a boundary/edge case, and correct enforcement of the
klimit in weighted mode.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Regressors/KNNRegressor.php |
Fixes weighted prediction by aligning weights with label keys preserved through distance sorting/slicing. |
tests/Regressors/KNNRegressorTest.php |
Adds regression tests to ensure weighted predictions are correct and stable for key alignment + k limiting. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
apphp
approved these changes
Aug 16, 2026
|
LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
src/Regressors/KNNRegressor.php:211-224 (root cause :232-246) — HIGH In weighted mode, predictSample builds $weights in distance order but array_values($labels) in original-index order, and Stats::weightedMean pairs them positionally — so each label is multiplied by the weight of a different neighbor.
Verified: train [[1],[2],[3]], labels [0,10,30], k=3 weighted, predict 2.0 → returns 10.0, correct is 12.5.
Verified: [[1],[5],[10]], [0,5,100], k=3 weighted, predict 10 → 8.55, correct is 79.60.
The classifier sibling KNearestNeighbors.php:229-233 does this correctly (aligns by key: $weights[$labels[$i]] += ...).
Fix: build weights keyed by neighbor index and pass $labels + $weights un-reordered.