Python Bokeh可视化分析:住房拥有情况与违约风险的关系
from bokeh.plotting import figure, show, output_file
import numpy as np
import pandas as pd
# 读取数据
df = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
# 计算是否有房子和是否违约的比例
has_house_default = df.groupby(['has_house', 'default']).size().reset_index(name='count')
has_house_default['percent'] = has_house_default['count'] / has_house_default['count'].sum()
# 创建绘图空间
p = figure(title='住房拥有情况与违约风险的关系', plot_width=600, plot_height=400)
# 绘制饼图
p.wedge(x=0.4, y=0.5, radius=0.4, start_angle=np.pi, end_angle=0, color='#c9d9d3', legend_label='无违约', source=has_house_default[has_house_default['default'] == 0])
p.wedge(x=0.6, y=0.5, radius=0.4, start_angle=np.pi, end_angle=0, color='#718dbf', legend_label='违约', source=has_house_default[has_house_default['default'] == 1])
# 设置图例位置
p.legend.location = 'top_right'
# 设置标题和轴标签
p.title.text_font_size = '16pt'
p.xaxis.axis_label = '拥有住房'
p.yaxis.axis_label = '违约'
# 输出图像
output_file('housing_default.html')
show(p)
原文地址: https://www.cveoy.top/t/topic/jjX3 著作权归作者所有。请勿转载和采集!