Python Bokeh可视化:分析房贷与信用卡违约率关系
Python Bokeh可视化:分析房贷与信用卡违约率关系
本文将使用Python的Bokeh库,通过条形图直观地比较有房贷和无房贷人群的信用卡违约率。
数据准备
我们使用一份包含 'housing' (是否有房贷) 和 'default' (信用卡是否违约) 字段的数据集。
import pandas as pd
from bokeh.plotting import figure, show, output_file
# 读取数据
df = pd.read_csv('train.csv')
# 计算有房贷和无房贷的违约比例
housing_default = df.groupby(['housing', 'default']).size().unstack().reset_index()
housing_default['default_rate'] = housing_default[1] / (housing_default[0] + housing_default[1])
housing_default = housing_default[['housing', 'default_rate']]
创建条形图
# 创建条形图
p = figure(x_range=housing_default['housing'], plot_height=350, title='房贷与信用卡违约率',
           toolbar_location=None, tools='')
# 添加条形
p.vbar(x=housing_default['housing'], top=housing_default['default_rate'], width=0.9)
# 设置图形属性
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 1
p.legend.orientation = 'horizontal'
p.legend.location = 'top_center'
# 输出图形
output_file('housing_default_rate.html')
show(p)
结果分析
运行代码后,你将获得一个HTML文件,其中包含一个显示有房贷和无房贷人群信用卡违约率差异的条形图。通过该图,你可以直观地了解房贷与信用卡违约率之间的关系。
总结
本文展示了如何使用Python Bokeh库创建条形图,并以房贷与信用卡违约率分析为例,演示了数据可视化的基本流程。 Bokeh提供了丰富的可视化功能,你可以根据自己的需求进行定制。
 
原文地址: https://www.cveoy.top/t/topic/jkqT 著作权归作者所有。请勿转载和采集!