K邻近回归算法

代码:

 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")

结果:

 总结:

  K-NN的优点之一就是模型很容易理解,通常不需要过多调节就可以得到不错的性能。
在考虑使用更高级的技术之前,尝试此算法是一种很好的基准方法。
构建模型的速度通常很快,但如果训练集很大(特征数很多或样本基数很大),预测速度可能会比较慢。
因此,使用此算法之前进行数据预处理是很重要的!
此算法对特征数很多(几百或更多)的数据集效果往往不好,特别是稀疏数据集!

posted @ 2018-07-13 11:23  云山之巅  阅读(783)  评论(0编辑  收藏  举报