KNN回归

KNN回归

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mglearn

from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor

X, y = mglearn.datasets.make_wave(n_samples=40)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

#模型实例化

reg = KNeighborsRegressor(n_neighbors=3).fit(X_train, y_train)
print("test accuracyL{:.2f}".format(reg.score(X_test, y_test)))

#得到分数0.83

#可视化
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
#创建1000个数据点,在-3,3之间均匀分布
line = np.linspace(-3, 3, 1000).reshape(-1,1)
for n_neighbors, ax in zip([1,3,9], axes):
    reg = KNeighborsRegressor(n_neighbors=n_neighbors).fit(X_train,y_train)
    #画出1000个数据点的预测曲线
    ax.plot(line, reg.predict(line))
    ax.plot(X_train, y_train, '^',c=mglearn.cm2(0), markersize=8)
    ax.plot(X_test, y_test, 'v',c=mglearn.cm2(1), markersize=8)
    ax.set_title(
        "{} neighbors\n train score:{:.2f} test score:{:.2f}".format(
            n_neighbors, reg.score(X_train, y_train), reg.score(X_test, y_test)
        )
    )
    ax.set_xlabel("Feature")
    ax.set_ylabel("Target")
axes[0].legend(["Model predictions", "Traning data/target", "Test data/target"], loc="best")
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43454167/article/details/84543586
kNN