K最邻近(KNN,K-NearestNeighbor) 结果:
其中虚线就是拟合后的模型
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn import neighbors# 加载数据
amplitude = 10
num_points = 100
X = amplitude * np.random.rand(num_points, 1) - 0.5 * amplitude# 加噪声
y = np.sinc(X).ravel()
y += 0.2 * (0.5 - np.random.rand(y.size))# 画图
plt.figure()
plt.scatter(X, y, s=40, c='k', facecolors='none')
plt.title('Input data')# 用输入数据的10倍设置网格
x_values = np.linspace(-0.5*amplitude, 0.5*amplitude, 10*num_points)[:, np.newaxis]# 最近邻 8
n_neighbors = 8# 训练knn
knn_regressor = neighbors.KNeighborsRegressor(n_neighbors, weights='distance')
y_values = knn_regressor.fit(X, y).predict(x_values)# 画图
plt.figure()
plt.scatter(X, y, s=40, c='k', facecolors='none', label='input data')
plt.plot(x_values, y_values, c='k', linestyle='--', label='predicted values')
plt.xlim(X.min() - 1, X.max() + 1)
plt.ylim(y.min() - 0.2, y.max() + 0.2)
plt.axis('tight')
plt.legend()
plt.title('K Nearest Neighbors Regressor')plt.show()
标签:knn,neighbors,plt,0.5,240720,最近,values,amplitude,np
From: https://blog.51cto.com/u_15862653/11888531