Seaborn regplot y_test和预测的样本量。

问题描述 投票:1回答:1

我对Python比较陌生。我试图在Seaborn regplot中绘制我的回归模型的y_test和预测,但结果是过度绘制。我试着从我的df(信用)中取样,但取样不起作用。这是我的代码。

# modeling
algo = XGBRegressor(n_estimators=50, max_depth=5)
model = algo.fit(X_train, y_train)

# predictions
preds = model.predict(X_test)

# sampling
data_sample = credit.sample(100)

# plotting results
sns.set_style('ticks')
sns.regplot(y_test, preds, data=data_sample, fit_reg=True, scatter_kws={'color': 'darkred', 'alpha': 0.3, 's': 100})

Overplotted regplot

有什么办法可以为y_test和preds调用样本?谢谢

plot random seaborn sample
1个回答
1
投票

你使用的对象是 y_test 当你不在sns.regplot里面引用它的时候,你需要子集包含两个变量的数据框,例如。

import xgboost as xgb
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import seaborn as sns 
import numpy as np

boston = load_boston() 
X_train, X_test, y_train, y_test=train_test_split(boston.data, boston.target, test_size=0.15)

# modeling
algo = xgb.XGBRegressor(n_estimators=50, max_depth=5)
model = algo.fit(X_train, y_train)

我创建了一个data. frame包含所有的测试和预测。

     preds = model.predict(X_test)
plotDa = pd.DataFrame({'y_test':y_test,'preds':preds})

sns.set_style('ticks')
sns.regplot(x='y_test',y='preds', data=plotDa.sample(10), fit_reg=True, scatter_kws={'color': 'darkred', 'alpha': 0.3, 's': 100})

enter image description here

或者你可以创建一个索引, 并使用它来绘制。

subsample = np.random.choice(len(preds),10)
sns.regplot(y_test[subsample],preds[subsample], fit_reg=True)
© www.soinside.com 2019 - 2024. All rights reserved.