OpenMOSS / OpenMOSS/FDU-AI-PRML-2024Fall
Is the testing code for knn in assignment1 right?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
I am a student in this course. When I am trying to finish the implement of knn in the assignment1, I always cannot pass the test. So I replace the KNN classifier to the one of sklearn package. However, the sklearn's implement still cannot pass the test.
I change the ./fduml/neighbors/tests/test_knn.py to the following codes
"""
test knn
"""
import numpy as np
from numpy.testing import assert_array_equal
# from fduml.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsClassifier
def test_kneighbors_classifier(
num_train=40,
n_features=5,
num_test=10,
n_neighbors=5,
random_state=0,
):
# Test k-neighbors classification
rng = np.random.RandomState(random_state)
X = 2 * rng.rand(num_train, n_features) - 1
y = ((X**2).sum(axis=1) < 2.).astype(int)
epsilon = 1e-5 * (2 * rng.rand(1, n_features) - 1)
# knn = KNeighborsClassifier(n_neighbors=n_neighbors,num_loops=2)
# knn.fit(X, y)
# y_pred = knn.predict(X[:num_test] + epsilon)
# assert_array_equal(y_pred, y[:num_test])
# knn = KNeighborsClassifier(n_neighbors=n_neighbors,num_loops=1)
# knn.fit(X, y)
# y_pred = knn.predict(X[:num_test] + epsilon)
# assert_array_equal(y_pred, y[:num_test])
# knn = KNeighborsClassifier(n_neighbors=n_neighbors,num_loops=0)
# knn.fit(X, y)
# y_pred = knn.predict(X[:num_test] + epsilon)
# assert_array_equal(y_pred, y[:num_test])
knn = KNeighborsClassifier(n_neighbors=n_neighbors)
knn.fit(X, y)
y_pred = knn.predict(X[:num_test] + epsilon)
assert_array_equal(y_pred, y[:num_test])
and then I run this cell in the notebook.
from fduml.neighbors.tests.test_knn import test_kneighbors_classifier
test_kneighbors_classifier()
But I get the following errors
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
[<ipython-input-8-68d934afc664>](https://localhost:8080/#) in <cell line: 2>()
1 from fduml.neighbors.tests.test_knn import test_kneighbors_classifier
----> 2 test_kneighbors_classifier()
2 frames
[/content/fduml/neighbors/tests/test_knn.py](https://localhost:8080/#) in test_kneighbors_classifier(num_train, n_features, num_test, n_neighbors, random_state)
40 knn.fit(X, y)
41 y_pred = knn.predict(X[:num_test] + epsilon)
---> 42 assert_array_equal(y_pred, y[:num_test])
[... skipping hidden 1 frame]
[/usr/lib/python3.10/contextlib.py](https://localhost:8080/#) in inner(*args, **kwds)
77 def inner(*args, **kwds):
78 with self._recreate_cm():
---> 79 return func(*args, **kwds)
80 return inner
81
[/usr/local/lib/python3.10/dist-packages/numpy/testing/_private/utils.py](https://localhost:8080/#) in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf, strict)
795 verbose=verbose, header=header,
796 names=('x', 'y'), precision=precision)
--> 797 raise AssertionError(msg)
798 except ValueError:
799 import traceback
AssertionError:
Arrays are not equal
Mismatched elements: 2 / 10 (20%)
Max absolute difference: 1
Max relative difference: 0.
x: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
y: array([1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with fduml/neighbors/tests/test_knn.py and reproduce test_kneighbors_classifier() using the shown parameters and sklearn comparison. Inspect the Assignment1 KNN implementation and the failing expected predictions; done means the mismatch is explained and the intended test or implementation behavior is corrected and verified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python, scikit-learn
- Domain
- machine-learning, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100