我有一个包含93个特征和9个类别标签的数据框。我希望为每个特征绘制其值,并显示相应的类别标签,但我希望生成一个包含93个子图的子图集,每个子图代表数据集中一个特征。我可以生成一个图表,示例如下:
sns.catplot(x="feat_1", y="target", data=train)
现在我基本上想重复同样的操作,但以分面网格的形式进行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)