使用seaborn绘制多个列的catplots

我有一个包含93个特征和9个类别标签的数据框。我希望为每个特征绘制其值,并显示相应的类别标签,但我希望生成一个包含93个子图的子图集,每个子图代表数据集中一个特征。我可以生成一个图表,示例如下:

sns.catplot(x="feat_1", y="target", data=train)

enter image description here

现在我基本上想重复同样的操作,但以分面网格的形式进行93次。我尝试创建一个有5列和19行的子图,然后循环遍历轴,但失败了…感谢您的帮助,我的数据看起来像这样(93个特征列和一个目标列):

    feat_1  feat_2  feat_3  feat_4  feat_5  feat_6  feat_7  feat_8  feat_9  feat_10 ... feat_85 feat_86 feat_87 feat_88 feat_89 feat_90 feat_91 feat_92 feat_93 targetid                                                                                  32518   0   0   0   1   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   0   0   Class_631734   0   1   7   5   0   0   0   0   0   1   ... 0   0   0   1   2   0   1   4   0   Class_657027   0   0   0   0   0   0   0   2   0   0   ... 0   0   0   0   0   0   1   0   0   Class_931629   0   1   0   0   0   0   0   1   1   0   ... 0   0   0   1   2   0   0   0   0   Class_614216   2   0   0   0   0   0   0   0   0   0   ... 0   0   0   1   0   0   0   0   0   Class_217376   0   0   0   0   0   0   0   0   0   0   ... 0   2   0   1   0   0   0   0   0   Class_210520   1   0   0   0   0   0   0   0   0   0   ... 0   3   0   0   0   0   0   0   0   Class_27665    0   0   0   0   0   0   0   0   0   0   ... 0   2   0   3   0   0   0   0   0   Class_226692   0   0   0   0   0   0   0   0   0   0   ... 4   0   0   0   0   0   0   0   0   Class_436809   0   0   3   4   0   0   0   0   0   0   ... 0   0   0   0   0   0   0   1   0   Class_647959   0   1   0   3   0   2   1   0   0   1   ... 6   0   0   0   1   1   0   0   1   Class_722649   0   0   0   0   1   0   0   0   0   1   ... 21  0   1   0   0   2   0   0   0   Class_334550   0   0   1   2   0   0   1   0   0   0   ... 0   0   1   0   0   1   1   1   1   Class_639943   3   0   0   0   0   0   0   0   0   0   ... 0   0   2   0   0   0   0   0   0   Class_638900   1   0   6   14  0   0   1   0   0   0   ... 0   0   1   0   0   0   0   0   0   Class_626333   0   0   1   0   0   0   1   1   0   0   ... 0   0   1   1   0   0   0   0   0   Class_416126   0   0   0   0   0   0   0   0   0   0   ... 0   0   1   10  0   0   0   0   0   Class_210490   0   0   0   0   0   0   0   1   0   0   ... 0   0   0   0   0   0   0   3   0   Class_258603   0   0   0   0   0   0   0   1   0   0   ... 0   0   0   0   0   0   28  0   1   Class_952668   0   0   1   2   0   0   0   4   0   0   ... 0   0   0   0   4   0   0   0   0   Class_8

回答:

为了充分利用seaborn的FacetGrid(由catplot使用),您需要将数据框从“宽”格式转换为“长”格式

# 虚拟数据框N=20N_features = 10N_classes = 5df = pd.DataFrame({f'feat_{i+1}': np.random.random(size=(N,)) for i in range(N_features)})df['target'] = np.random.choice([f'Class_{i+1}' for i in range(N_classes)], size=(N,))# 从宽格式转换为长格式,然后使用列'features'来分面绘图df2 = df.melt(id_vars=['target'], var_name='features')sns.catplot(data=df2, x='value', y='target', col='features', col_wrap=5, height=3, aspect=0.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中创建了一个多类分类项目。该项目可以对…

发表回复

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