seaborn residplot的参数应该是什么

我已经制作了一个简单的线性回归模型:

LR = LinearRegression()kfold = model_selection.KFold(n_splits=10, random_state=12)result_kfold = model_selection.cross_val_score(LR, X_train, Y_train, cv=kfold, scoring = 'r2')print("Accuracy: %.2f%%" % (result_kfold.mean()*100.0)) LR.fit(X_train,Y_train)Y_pred = LR.predict(X_test)print("Y_pred:", Y_pred)

我想绘制残差错误。我使用了’residplot’来实现这一点。但我不确定我是否传递了正确的参数。根据文档,我们需要使用预测变量和结果/响应变量。这是代码:

sns.set(style="whitegrid")sns.residplot(Y_test, Y_pred, lowess=True, color="g")

请问有人能告诉我这是对的吗…还有X轴和Y轴的标签应该是什么?

提前感谢您的帮助


回答:

你正在绘制一些非常奇怪的东西,所以让我们使用一个示例数据集:

from sklearn.linear_model import LinearRegressionfrom sklearn.model_selection import train_test_splitimport matplotlib. pyplot as pltimport seaborn as snsiris = sns.load_dataset('iris')X_train, X_test, Y_train, Y_test = train_test_split(iris.iloc[:,:3], iris.iloc[:,3],random_state=11)LR = LinearRegression()LR.fit(X_train,Y_train)Y_pred = LR.predict(X_test)

如果你只是想绘制残差,你可以这样做:

sns.set(style="whitegrid")fig, ax = plt.subplots(figsize =(5,5))sns.regplot(x=Y_pred,y=Y_test-Y_pred,ax=ax,lowess=True)ax.set(ylabel='residuals',xlabel='fitted values')

enter image description here

使用sns.regplot()你得到的是y变量回归到x变量上并绘制残差,这在你的情况下没有意义,我在下面说明了如何获得这个图表,首先你将预测值(y变量)拟合到实际值(x变量),然后获得残差:

plotfit = LinearRegression()plotfit.fit(Y_test.to_numpy().reshape(-1,1),Y_pred)residual = Y_pred - plotfit.predict(Y_test.to_numpy().reshape(-1,1))

然后绘制它会得到与你的sns.residplot完全相同的结果:

sns.set(style="whitegrid")fig, ax = plt.subplots(1,2,figsize =(10,5))sns.residplot(Y_test,Y_pred,lowess=True, color="g",ax=ax[0])ax[0].set_xlim(0,2.5)sns.regplot(x=Y_test,y=residual,lowess=True)ax[1].set_xlim(0,2.5)

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注