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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值