Skip to content

Commit b95aece

Browse files
committed
Fix bug in KNN predict_array(); improved readme
1 parent 3b8a75d commit b95aece

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

KNN/knn.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def classify(self, k: int, data_point: DP) -> str:
5353
neighbors = self.nearest(k, data_point)
5454
return Counter(neighbor.kind for neighbor in neighbors).most_common(1)[0][0]
5555

56-
# Predict a property of a data point based on the k nearest neighbors
56+
# Predict a numeric property of a data point based on the k nearest neighbors
5757
# Find the average of that property from the neighbors and return it
5858
def predict(self, k: int, data_point: DP, property_name: str) -> float:
5959
neighbors = self.nearest(k, data_point)
6060
return sum([getattr(neighbor, property_name) for neighbor in neighbors]) / len(neighbors)
6161

62-
# Predict a property of a data point based on the k nearest neighbors
62+
# Predict a NumPy array property of a data point based on the k nearest neighbors
6363
# Find the average of that property from the neighbors and return it
6464
def predict_array(self, k: int, data_point: DP, property_name: str) -> np.ndarray:
6565
neighbors = self.nearest(k, data_point)
66-
return np.sum([getattr(neighbor, property_name) for neighbor in neighbors]) / len(neighbors)
66+
return np.sum([getattr(neighbor, property_name) for neighbor in neighbors], axis=0) / len(neighbors)

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# FunComputerScienceProjectsInPython
2-
Source for the book Fun Computer Science Projects in Python by [David Kopec](https://davekopec.com).
1+
# Fun Computer Science Projects in Python
2+
3+
Source for the book *Fun Computer Science Projects in Python* by [David Kopec](https://davekopec.com).
34

45
## Get the Book
56

67
## Authorship and License
78

8-
The code in this repository is Copyright 2024 David Kopec and released under the terms of the Apache License 2.0. That means you can reuse the code, but you must give credit to David Kopec. Please read the license for details.
9+
The code in this repository is Copyright 2024 David Kopec and released under the terms of the Apache License 2.0. That means you can reuse the code, but you must give credit to David Kopec. Please read the license for details and other requirements.
910

1011
## Running and Testing Each Project
1112

@@ -114,7 +115,7 @@ A Chip8 virtual machine.
114115

115116
#### Requirements
116117

117-
- PyGame
118+
- Pygame
118119
- NumPy
119120

120121
#### Running
@@ -135,7 +136,7 @@ A simple [NES](https://en.wikipedia.org/wiki/Nintendo_Entertainment_System) emul
135136

136137
#### Requirements
137138

138-
- PyGame
139+
- Pygame
139140
- NumPy
140141

141142
#### Running
@@ -152,13 +153,13 @@ For example:
152153

153154
`python -m tests.test_nesemulator`
154155

155-
### KNN (Chapter 7)
156+
### KNN (Chapters 7 & 8)
156157

157158
A handwritten digit recognizer using the K-nearest neighbors algorithm.
158159

159160
#### Requirements
160161

161-
- PyGame
162+
- Pygame
162163
- NumPy
163164

164165
#### Running

0 commit comments

Comments
 (0)