Skip to content

Commit

Permalink
Fix bug in KNN predict_array(); improved readme
Browse files Browse the repository at this point in the history
  • Loading branch information
davecom committed May 12, 2024
1 parent 3b8a75d commit b95aece
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions KNN/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def classify(self, k: int, data_point: DP) -> str:
neighbors = self.nearest(k, data_point)
return Counter(neighbor.kind for neighbor in neighbors).most_common(1)[0][0]

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

# Predict a property of a data point based on the k nearest neighbors
# Predict a NumPy array property of a data point based on the k nearest neighbors
# Find the average of that property from the neighbors and return it
def predict_array(self, k: int, data_point: DP, property_name: str) -> np.ndarray:
neighbors = self.nearest(k, data_point)
return np.sum([getattr(neighbor, property_name) for neighbor in neighbors]) / len(neighbors)
return np.sum([getattr(neighbor, property_name) for neighbor in neighbors], axis=0) / len(neighbors)
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# FunComputerScienceProjectsInPython
Source for the book Fun Computer Science Projects in Python by [David Kopec](https://davekopec.com).
# Fun Computer Science Projects in Python

Source for the book *Fun Computer Science Projects in Python* by [David Kopec](https://davekopec.com).

## Get the Book

## Authorship and License

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.
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.

## Running and Testing Each Project

Expand Down Expand Up @@ -114,7 +115,7 @@ A Chip8 virtual machine.

#### Requirements

- PyGame
- Pygame
- NumPy

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

#### Requirements

- PyGame
- Pygame
- NumPy

#### Running
Expand All @@ -152,13 +153,13 @@ For example:

`python -m tests.test_nesemulator`

### KNN (Chapter 7)
### KNN (Chapters 7 & 8)

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

#### Requirements

- PyGame
- Pygame
- NumPy

#### Running
Expand Down

0 comments on commit b95aece

Please sign in to comment.