-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_nearest_neighbors copy.py
76 lines (55 loc) · 2.11 KB
/
k_nearest_neighbors copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Code based on Data Science from Scratch
# with corrections for Scripps College
# DS002, Spring 2022
# Professor Douglas Goodwin
# # # # # # # # # # # # # # # # # # # # # # # #
# Imports
# # # # # # # # # # # # # # # # # # # # # # # #
# python imports
from typing import List, NamedTuple
import math
from collections import Counter
import random
# local code imports
from .linear_algebra import Vector, distance
# other imports
import matplotlib.pyplot as plt
# # # # # # # # # # # # # # # # # # # # # # # #
# Let's go!
# # # # # # # # # # # # # # # # # # # # # # # #
def raw_majority_vote(labels: List[str]) -> str:
votes = Counter(labels)
winner, _ = votes.most_common(1)[0]
return winner
assert raw_majority_vote(['a', 'b', 'c', 'b']) == 'b'
def majority_vote(labels: List[str]) -> str:
"""Assumes that labels are ordered from nearest to farthest."""
vote_counts = Counter(labels)
winner, winner_count = vote_counts.most_common(1)[0]
num_winners = len([count
for count in vote_counts.values()
if count == winner_count])
if num_winners == 1:
return winner # unique winner, so return it
else:
return majority_vote(labels[:-1]) # try again without the farthest
# Tie, so look at first 4, then 'b'
assert majority_vote(['a', 'b', 'c', 'b', 'a']) == 'b'
class LabeledPoint(NamedTuple):
point: Vector
label: str
def knn_classify(k: int,
labeled_points: List[LabeledPoint],
new_point: Vector) -> str:
# Order the labeled points from nearest to farthest.
by_distance = sorted(labeled_points,
key=lambda lp: distance(lp.point, new_point))
# Find the labels for the k closest
k_nearest_labels = [lp.label for lp in by_distance[:k]]
# and let them vote.
return majority_vote(k_nearest_labels)
def random_point(dim: int) -> Vector:
return [random.random() for _ in range(dim)]
def random_distances(dim: int, num_pairs: int) -> List[float]:
return [distance(random_point(dim), random_point(dim))
for _ in range(num_pairs)]