如何在分割数据集后使用hue参数绘制配对图

我在处理鸢尾花数据集的分类问题时,能够在原始数据集上创建一个配对图,设置hue='species'后效果如下:

enter image description here

但是,当我将数据集分割成X_train和y_train后,如何在种类已经被分离的情况下继续使用hue参数呢?

X = DATA.drop(['class'], axis = 'columns')y = DATA['class'].valuesX_train, X_test, y_train, y_test=train_test_split(X,y, test_size=0.20,random_state =42)gbl_pl=[]gbl_pl.append(('standard_scaler_gb',StandardScaler()))                     gblpq=Pipeline((gbl_pl))scaled_df=gblpq.fit_transform(X_train,y_train)sns.pairplot(data=scaled_df)plt.show()

输出

enter image description here

期望(类似于这种效果,但使用分割后的数据集且不包括测试数据)enter image description here


回答:

你可以将y_train作为一列拼接到X_train中。

from matplotlib import pyplot as pltfrom sklearn.model_selection import train_test_splitimport seaborn as snsimport pandas as pdimport numpy as npiris = sns.load_dataset('iris')X = iris.drop(columns='species')y = iris['species']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)sns.pairplot(data=pd.concat([X_train, y_train], axis=1), hue=y_train.name)

sns.pairplot of concatenated X_trains and y_train

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中创建了一个多类分类项目。该项目可以对…

发表回复

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