https://blog.csdn.net/qq_37879432/article/details/79831720
使用KNN算法进行回归拟合可以参考这篇博文:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets.samples_generator import make_blobs
# 生成数据
centers = [[-2, 2], [2, 2], [0, 4]]
X, y = make_blobs(n_samples=600, centers=centers, random_state=0, cluster_std=0.60)
# 画出数据
plt.figure(figsize=(16, 10), dpi=144)
c = np.array(centers)
plt.scatter(X[:, 0], X[:, 1], c=y, s=100, cmap='cool'); # 画出样本
plt.scatter(c[:, 0], c[:, 1], s=100, marker='^', c='orange'); # 画出中心点
from sklearn.neighbors import KNeighborsClassifier
from numpy as np
# 模型训练
k = 5
clf = KNeighborsClassifier(n_neighbors=k)
clf.fit(X, y);
# 进行预测
# X_sample = [[0,2],[1,1],[-1,3]]
X_sample = np.array([[0,2],[1,1],[-1,3]],dtype=int)
y_sample = clf.predict(X_sample);
neighbors = clf.kneighbors(X_sample, return_distance=False);
X_sample_disp_x = np.array(X_sample[:,0],dtype=int)
X_sample_disp_y = np.array(X_sample[:,1],dtype=int)
# 画出示意图
plt.figure(figsize=(16, 10), dpi=144)
plt.scatter(X[:, 0], X[:, 1], c=y, s=100, cmap='cool'); # 样本
plt.scatter(c[:, 0], c[:, 1], s=100, marker='^', c='k'); # 中心点
plt.scatter(X_sample_disp_x, X_sample_disp_y, marker="x",
c=y_sample, s=100, cmap='cool') # 待预测的点
for i in neighbors[0]:
plt.plot([X[i][0], X_sample[0][0]], [X[i][1], X_sample[0][1]],
'k--', linewidth=0.8); # 预测点与距离最近的 5 个样本的连线
for i in neighbors[1]:
plt.plot([X[i][0], X_sample[1][0]], [X[i][1], X_sample[1][1]],
'k--', linewidth=0.8);
for i in neighbors[2]:
plt.plot([X[i][0], X_sample[2][0]], [X[i][1], X_sample[2][1]],
'k--', linewidth=0.8);