你好,我不知道为什么我在 jupyterlab 中无法运行你的代码,请帮助我

作者: lin9178618872创建于 2023年2月10日更新于 2023年2月14日

from typing import List from collections import Counter 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' from typing import NamedTuple from scratch.linear_algebra import Vector, distance 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) import random 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)] def main(): from typing import Dict import csv from collections import defaultdict def parse_iris_row(row: List[str]) -> LabeledPoint: """ sepal_length, sepal_width, petal_length, petal_width, class """ measurements = [float(value) for value in row[:-1]]

class is e.g. "Iris-virginica"; we just want "virginica"

label = row[-1].split("-")[-1] return LabeledPoint(measurements, label) with open('iris.data') as f: reader = csv.reader(f) iris_data = [parse_iris_row(row) for row in reader]

We'll also group just the points by species/label so we can plot them.

points_by_species: Dict[str, List[Vector]] = defaultdict(list) for iris in iris_data: points_by_species[iris.label].append(iris.point) from matplotlib import pyplot as plt metrics = ['sepal length', 'sepal width', 'petal length', 'petal width'] pairs = [(i, j) for i in range(4) for j in range(4) if i < j] marks = ['+', '.', 'x'] # we have 3 classes, so 3 markers fig, ax = plt.subplots(2, 3) for row in range(2): for col in range(3): i, j = pairs[3 * row + col] ax[row, col].scatter(points_by_species[marks[i]][:, 0], points_by_species[marks[i]][:, 1], marker=marks[i], label=marks[i]) ax[row, col].scatter(points_by_species[marks[j]][:, 0], points_by_species[marks[j]][:, 1], marker=marks[j], label=marks[j]) ax[row, col].set_xlabel(metrics[i]) ax[row, col].set_ylabel(metrics[j]) ax[row, col].legend() plt.show()

内容来源: joelgrus/data-science-from-scratch