generated from LufsX/Experiment-kNN
This commit is contained in:
+20
-9
@@ -43,7 +43,7 @@ name = "李劲"
|
|||||||
|
|
||||||
def classify(input_vector, dataSet, labels, k=3):
|
def classify(input_vector, dataSet, labels, k=3):
|
||||||
"""
|
"""
|
||||||
KNN 分类算法(使用欧氏距离)。
|
KNN 分类算法(使用欧氏距离和加权多数表决法)。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- input_vector (numpy array): 输入待分类的样本。
|
- input_vector (numpy array): 输入待分类的样本。
|
||||||
@@ -58,16 +58,27 @@ def classify(input_vector, dataSet, labels, k=3):
|
|||||||
diffMat = dataSet - input_vector
|
diffMat = dataSet - input_vector
|
||||||
euclidean_distances = np.sqrt(np.sum(diffMat ** 2, axis=1))
|
euclidean_distances = np.sqrt(np.sum(diffMat ** 2, axis=1))
|
||||||
|
|
||||||
# 获取距离最近的 k 个样本的索引
|
# 获取距离最近的 k 个样本的索引及其距离
|
||||||
k_indices = np.argsort(euclidean_distances)[:k]
|
k_indices = np.argsort(euclidean_distances)[:k]
|
||||||
|
k_nearest_distances = euclidean_distances[k_indices]
|
||||||
# 获取最近邻样本的标签
|
|
||||||
k_nearest_labels = [labels[i] for i in k_indices]
|
k_nearest_labels = [labels[i] for i in k_indices]
|
||||||
|
|
||||||
# 找出出现次数最多的标签
|
# 计算每个标签的加权得分
|
||||||
most_common = max(set(k_nearest_labels), key=k_nearest_labels.count)
|
label_votes = {}
|
||||||
|
for i in range(k):
|
||||||
return most_common
|
label = k_nearest_labels[i]
|
||||||
|
distance = k_nearest_distances[i]
|
||||||
|
# 使用距离的倒数作为权重(或者可以使用其他加权方式,如距离的负指数)
|
||||||
|
weight = 1.0 / distance # 注意:这里使用距离的倒数可能会导致非常近的点权重过大,有时可能需要归一化
|
||||||
|
if label in label_votes:
|
||||||
|
label_votes[label] += weight
|
||||||
|
else:
|
||||||
|
label_votes[label] = weight
|
||||||
|
|
||||||
|
# 找出加权得分最高的标签
|
||||||
|
most_common = max(label_votes, key=label_votes.get)
|
||||||
|
|
||||||
|
return most_common
|
||||||
|
|
||||||
def load_dataset(directory):
|
def load_dataset(directory):
|
||||||
features = []
|
features = []
|
||||||
|
|||||||
Reference in New Issue
Block a user