generated from LufsX/Experiment-kNN
This commit is contained in:
+1
-1
@@ -43,7 +43,7 @@ name = "陈垣豪"
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def classify(input_vector, dataSet, labels, k=3):
|
||||
distances = np.linalg.norm(dataSet - input_vector, axis=1)
|
||||
distances = np.sum((dataSet - input_vector) ** 2, axis=1)
|
||||
nearest_indices = np.argsort(distances)[:k]
|
||||
nearest_labels = [labels[i] for i in nearest_indices]
|
||||
most_common_label = Counter(nearest_labels).most_common(1)[0][0]
|
||||
|
||||
Reference in New Issue
Block a user