我已经运行了KNN模型。现在,我想绘制残差与预测值的关系图。来自不同网站的每个示例都表明,我必须首先运行线性回归模型。但是我不明白该怎么做。有人可以帮忙吗?提前致谢。 这是我的模特-
train, validate, test = np.split(df.sample(frac=1), [int(.6*len(df)), int(.8*len(df))])
x_train = train.iloc[:,[2,5]].values
y_train = train.iloc[:,4].values
x_validate = validate.iloc[:,[2,5]].values
y_validate = validate.iloc[:,4].values
x_test = test.iloc[:,[2,5]].values
y_test = test.iloc[:,4].values
clf=neighbors.KNeighborsRegressor(n_neighbors = 6)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_validate)
答案 0 :(得分:3)
残差不过是您的预测值与实际值相差多少。因此,它被计算为实际值预测值。您的情况是残差= y_test-y_pred 。现在进行绘图,只需使用它;
将matplotlib.pyplot导入为plt
plt.scatter(残差,y_pred)
plt.show()
答案 1 :(得分:1)
这是什么问题?残差只是y_test-y_pred
。现在使用seaborn's regplot.