python编程实现 k 最近邻分类 编写 k-nearestN 函数,输入训练数据 X_train,其对应标签 y,近邻数 k,测试 集 X_test,以及距离度量,输出测试集中每个样本的标签。默认距离度量为欧 式距离,其他可选距离包括余弦相似度等。
时间: 2024-05-02 13:19:35 浏览: 77
python机器学习案例教程——K最近邻算法的实现
好的,这是一个关于python编程的问题。您可以通过以下代码来实现 k 最近邻分类:
```python
from collections import Counter
import numpy as np
def k_nearest_neighbor(X_train, y_train, X_test, k=3, distance_metric='euclidean'):
# Calculate pairwise distances between each test sample and training sample
if distance_metric == 'euclidean':
distances = np.sqrt(((X_train - X_test[:, np.newaxis])**2).sum(axis=2))
elif distance_metric == 'cosine':
dot_product = np.dot(X_test, X_train.T)
X_test_norms = np.linalg.norm(X_test, axis=1)
X_train_norms = np.linalg.norm(X_train, axis=1)
distances = 1 - dot_product / np.outer(X_test_norms, X_train_norms)
# Obtain k nearest neighbors for each test sample based on distances
k_nearest_idx = np.argsort(distances)[:, :k]
k_nearest_labels = y_train[k_nearest_idx]
# Predict class for each test sample based on majority vote among its k nearest neighbors
predicted_labels = [Counter(labels).most_common(1)[0][0] for labels in k_nearest_labels]
return predicted_labels
```
其中,X_train 是训练数据集,y_train 是 X_train 对应的标签,X_test 是测试集,k 是近邻数,distance_metric 是距离度量(默认为欧式距离)。这个函数将返回测试集中每个样本的标签。
希望这个代码能对您有所帮助!
阅读全文