我很幸运在数据分析中发现了这个错误:
我正在画三个条形图。皮尔逊相关、斯皮尔曼相关,然后是显示每个变量s-P差异的曲线图
为了创建可视化,我使用了以下代码行。特别值得注意的是我使用了agment data=cor.sort_values(by='SomeMeasureHere')
def plot_response_corr(df, features, response, corr_type):
cor = pd.DataFrame()
# Measure difference between Spearman's and Pearson's to analyze for non-linearity
if corr_type == 's-p':
cor['feature'] = features
cor['spearman'] = [df[f].corr(df[response], 'spearman') for f in features]
cor['pearson'] = [df[f].corr(df[response], 'pearson') for f in features]
cor['comparison'] = cor['spearman'] - cor['pearson']
fig, axes = plt.subplots(1,3, sharex=True, figsize=(12, 4), )
[ax.tick_params('x', labelrotation=45) for ax in axes]
sns.barplot(
data=cor,
x='feature', y='comparison', ax=axes[2]
).set_title('S-P Comparison')
sns.barplot(
data=cor,
x='feature', y='spearman', ax=axes[1]
).set_title('Spearman Association')
sns.barplot(
data=cor,
x='feature', y='pearson', ax=axes[0]
).set_title('Pearson Correlation')
plt.tight_layout()
plt.show()
return cor
其结果如下:
然而,凭直觉,我决定签出用于绘制这些图的cor
数据帧,而不是仅仅放弃它。我发现S-P差异最大的变量实际上是LotArea,而不是GrLivArea。由于某种原因,情节发生了逆转
feature spearman pearson comparison
0 LotArea 0.456461 0.263843 0.192617
1 GrLivArea 0.731310 0.708624 0.022685
2 GarageArea 0.649379 0.623431 0.025947
为了修复它,我只是删除了.sort_values(by='comparison')
参数,它就被修复了。值/条形图现在与正确的变量相关联,但图形是无序的
我想知道:
我很幸运抓到了这个,但我需要帮助修理它。多谢各位
问题不在于对数据帧进行排序,而在于
x-ticks
特别是参数sharex=True
,因为您正在打印到子地块中,它们将共享相同的顺序pearson
和spearman
顺序相同,因此唯一受影响的是comparison
这应该解决以下问题:
相关问题 更多 >
编程相关推荐