首页 > 解决方案 > 如何解决 seaborn :RuntimeWarning: log10 中遇到的除以零?

问题描述

我正在尝试可视化一些特征的统计信息,我的一些特征的值为0,我正在使用seaborn histplot 的参数log_scalelog for base 10因为我的特征值中存在0,所以我无法可视化绘图,所以应该我这样做不会对数据造成损害?

我的代码:

fig, axes = plt.subplots(ncols=2, nrows=len(METAFEATURES), figsize=(20,50),dpi=100)

for i, feature in enumerate(METAFEATURES):
    sns.histplot(dfs['toxic'][feature], label='toxic', ax=axes[i][0], color=colors[0],log_scale=True)
    sns.histplot(dfs['severe_toxic'][feature], label='severe_toxic',ax=axes[i][0], color=colors[1],log_scale=True)
    sns.histplot(dfs['obscene'][feature], label='obscene', ax=axes[i][0], color=colors[2],log_scale=True)
    sns.histplot(dfs['threat'][feature], label='threat', ax=axes[i][0], color=colors[3],log_scale=True)
    sns.histplot(dfs['insult'][feature], label='insult', ax=axes[i][0], color=colors[4],log_scale=True)
    sns.histplot(dfs['identity_hate'][feature], label='identity_hate', ax=axes[i][0], color=colors[5],log_scale=True)
    
    sns.histplot(train_df[feature], label="Training", ax=axes[i][1],color=colors[0],log_scale=True)
    sns.histplot(test_df[feature], label='Test', ax=axes[i][1],color=colors[1],log_scale=True)
    
    for j in range(2):
        axes[i][j].set_xlabel('')
        axes[i][j].tick_params(axis='x', labelsize=12)
        axes[i][j].tick_params(axis='y', labelsize=12)
        axes[i][j].legend()
        
    axes[i][0].set_title(f"{feature} Target Distribution in Training Set", fontsize=13)
    axes[i][1].set_title(f"{feature} Training & Test Set Distribution", fontsize=13)
    
plt.show()

标签: pythonpandasseaborndata-visualization

解决方案


推荐阅读