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")
推荐阅读
-
基于 K-means 聚类算法的 MATLAB 图像分割
-
PSO-SVM,基于 PSO 的粒子群算法优化 SVM 支持向量机回归预测(多输入单输出)--带代码
-
机器学习] 机器学习创建算法编号:K-近邻算法,学习目标[含代码文档]。
-
月薪 20k-50k 中原三维机器视觉算法、语音识别、DSP 软件工程师招聘
-
k 均值聚类算法 初级
-
上机学习 K 最近邻算法 + 练习
-
分析回归算法和分类算法的异同
-
机器学习的 K-means 算法
-
利用凸几何和 K-means 的高光谱端元提取算法(Matlab 代码实现)
-
预测模型] 基于随机蛙跳算法的 SFLA 优化神经网络,用 matlab 代码实现数据回归预测