generated from LufsX/Challenge-kNN
This commit is contained in:
+11
-3
@@ -23,8 +23,8 @@ import numpy as np
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
"""
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
id = "202210423217"
|
||||
name = "吴俊煯"
|
||||
|
||||
|
||||
"""
|
||||
@@ -50,7 +50,15 @@ def classify0(inX, dataSet, labels):
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
|
||||
distances = [np.sqrt(np.sum((inX - x) ** 2)) for x in dataSet]
|
||||
|
||||
k_indices = np.argsort(distances)[:k]
|
||||
k_nearest_labels = [labels[i] for i in k_indices]
|
||||
|
||||
from collections import Counter
|
||||
vote_result = Counter(k_nearest_labels).most_common(1)
|
||||
|
||||
return vote_result[0][0]
|
||||
|
||||
def load_data(file_path):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user