Python 使用具有不同k值的k-nn绘制图形

Python 使用具有不同k值的k-nn绘制图形,python,machine-learning,knn,Python,Machine Learning,Knn,我想为k-nn分类器绘制具有不同k值的图形。 我的问题是,这些数字似乎有相同的k值。 到目前为止,我尝试的是更改循环中每次运行的k值: clf=KNeighborsClassifiern\u邻居=计数器+1 但所有的数字似乎都是k=1 所有绘图看起来相同的原因是,您每次都只是绘制测试集,而不是在测试集上绘制模型预测。您可能打算对k的每个值执行以下操作: 将模型与训练集相匹配,在这种情况下,应将clf.fitX_测试、c_测试替换为clf.fitX_训练、c_训练 在测试集上生成模型预测,在这种情

我想为k-nn分类器绘制具有不同k值的图形。 我的问题是,这些数字似乎有相同的k值。 到目前为止,我尝试的是更改循环中每次运行的k值:

clf=KNeighborsClassifiern\u邻居=计数器+1 但所有的数字似乎都是k=1


所有绘图看起来相同的原因是,您每次都只是绘制测试集,而不是在测试集上绘制模型预测。您可能打算对k的每个值执行以下操作:

将模型与训练集相匹配,在这种情况下,应将clf.fitX_测试、c_测试替换为clf.fitX_训练、c_训练

在测试集上生成模型预测,在这种情况下,您应该添加c_pred=clf.predictX_test

在测试集上绘制模型预测,在这种情况下,您应该在散点图中用c_pred替换c_test,即使用mglearn。离散_散点X_test[:,0],X_test[:,1],c_pred,ax=ax[counter],而不是mglearn。离散_散点X_test[:,0],X_test[:,1],c_test,ax=ax[counter]

我在下面包含了完整的代码

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import mglearn
import matplotlib.pyplot as plt

data = fetch_california_housing()

c = np.array([1 if y > np.median(data['target']) else 0 for y in data['target']])

X_train, X_test, c_train, c_test = train_test_split(data['data'], c, random_state=0)

fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(20, 6))

for counter in range(3):

    clf = KNeighborsClassifier(n_neighbors=counter+1)

    # fit the model to the training set
    clf.fit(X_train, c_train)

    # extract the model predictions on the test set
    c_pred = clf.predict(X_test)

    # plot the model predictions
    plt.tight_layout()
    mglearn.discrete_scatter(X_test[:,0], X_test[:,1], c_pred, ax=ax[counter])
    plt.legend(["Class 0", "Class 1"], loc=4)
    plt.xlabel("First feature")
    plt.ylabel("Second feature")
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import mglearn
import matplotlib.pyplot as plt

data = fetch_california_housing()

c = np.array([1 if y > np.median(data['target']) else 0 for y in data['target']])

X_train, X_test, c_train, c_test = train_test_split(data['data'], c, random_state=0)

fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(20, 6))

for counter in range(3):

    clf = KNeighborsClassifier(n_neighbors=counter+1)

    # fit the model to the training set
    clf.fit(X_train, c_train)

    # extract the model predictions on the test set
    c_pred = clf.predict(X_test)

    # plot the model predictions
    plt.tight_layout()
    mglearn.discrete_scatter(X_test[:,0], X_test[:,1], c_pred, ax=ax[counter])
    plt.legend(["Class 0", "Class 1"], loc=4)
    plt.xlabel("First feature")
    plt.ylabel("Second feature")