https://github.com/brendan-rius/k-nearest-neighbors
Naive implementation of KNN algorithm
https://github.com/brendan-rius/k-nearest-neighbors
Last synced: 6 months ago
JSON representation
Naive implementation of KNN algorithm
- Host: GitHub
- URL: https://github.com/brendan-rius/k-nearest-neighbors
- Owner: brendan-rius
- Created: 2016-03-31T13:23:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-31T13:31:57.000Z (over 9 years ago)
- Last Synced: 2025-03-22T10:32:39.968Z (9 months ago)
- Language: Python
- Size: 1000 Bytes
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# k-nearest-neighbors
Naive implementation of KNN algorithm.
Example of code:
```python
clf = KNNClassifier(2)
# Training feature vectors
x = [
[1, 2, 3, 1, 2, 1],
[1, 4, 3, 1, 2, 3],
[5, 1, 3, 3, 2, 1],
[1, 2, 3, 1, 3, 1],
[1, 2, 3, 1, 2, 3],
[1, 2, 5, 1, 5, 1],
[5, 2, 8, 5, 2, 5],
[5, 4, 8, 5, 2, 8],
[5, 5, 8, 8, 2, 5],
[5, 2, 8, 5, 8, 5],
[5, 2, 8, 5, 2, 8],
[5, 2, 5, 5, 5, 5],
]
# Training labels
y = [
"A",
"A",
"A",
"A",
"A",
"A",
"B",
"B",
"B",
"B",
"B",
"B",
]
clf.fit(x, y) # "Learning" step
print(clf.predict([1, 13, 3, 1, 20, 1])) # Prediction
```
Output:
```
A
```