欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

K邻近回归算法

最编程 2024-02-10 20:26:43
...
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Fri Jul 13 10:40:22 2018 4 5 @author: zhen 6 """ 7 import mglearn 8 from sklearn.neighbors import KNeighborsRegressor 9 from sklearn.model_selection import train_test_split 10 import matplotlib.pyplot as plt 11 import numpy as np 12 13 x, y = mglearn.datasets.make_wave(n_samples=40) 14 # 将wave数据集分为训练集和测试集 15 x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0) 16 17 fig, axes = plt.subplots(1, 3, figsize=(15, 4)) 18 19 # 创建1000个数据点,在-3到3之间均匀分布 20 line = np.linspace(-3, 3, 1000).reshape(-1, 1) 21 for n_neighbors, ax in zip([1, 3, 9],axes): 22 # 利用1个,3个和9个邻居分别进行预测 23 reg = KNeighborsRegressor(n_neighbors=n_neighbors) 24 reg.fit(x_train, y_train) 25 ax.plot(line, reg.predict(line)) 26 ax.plot(x_train, y_train, '^', c=mglearn.cm2(0), markersize=8) 27 ax.plot(x_test, y_test, 'v', c=mglearn.cm2(1), markersize=8) 28 ax.set_title( 29 "{} neighbor(s)\n train score:{:.2f} test score:{:.2f}".format( 30 n_neighbors, reg.score(x_train, y_train), 31 reg.score(x_test, y_test))) 32 ax.set_xlabel("Feature") 33 ax.set_ylabel("Target") 34 axes[0].legend(["Model prediction", "Training data/target", "Test data/target"], loc="best")