Python Bokeh 可视化分析:房产与违约关系

本示例使用 Python 的 Bokeh 库对房产 (是否有房子) 和违约 (是否违约) 之间的关系进行可视化分析。我们将使用饼图来直观展示不同情况下的数据分布。

代码实现

from bokeh.plotting import figure, show, output_file
import numpy as np
import pandas as pd
import pandas

df = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/train.csv')
test = pd.read_csv(r'/Users/fuchuanruo/Desktop/可视化作业/实践/test.csv')

# 提取housing和default两列数据
data = df[['housing', 'default']]

# 统计有房子且违约的人数
data['housing_default'] = np.where((data['housing'] == 'yes') & (data['default'] == 'yes'), 1, 0)
housing_default_count = data['housing_default'].sum()

# 统计有房子但未违约的人数
data['housing_no_default'] = np.where((data['housing'] == 'yes') & (data['default'] == 'no'), 1, 0)
housing_no_default_count = data['housing_no_default'].sum()

# 统计无房子但违约的人数
data['no_housing_default'] = np.where((data['housing'] == 'no') & (data['default'] == 'yes'), 1, 0)
no_housing_default_count = data['no_housing_default'].sum()

# 统计无房子且未违约的人数
data['no_housing_no_default'] = np.where((data['housing'] == 'no') & (data['default'] == 'no'), 1, 0)
no_housing_no_default_count = data['no_housing_no_default'].sum()

# 绘制饼图
labels = ['有房子且违约', '有房子但未违约', '无房子但违约', '无房子且未违约']
values = [housing_default_count, housing_no_default_count, no_housing_default_count, no_housing_no_default_count]

p = figure(title='房产与违约关系', toolbar_location=None, tools="")
p.wedge(x=0, y=1, radius=0.4, start_angle=0, end_angle=2*np.pi, color=['red', 'blue', 'green', 'gray'],
        legend_label=labels, direction='clock', labels=labels, values=values)

p.legend.location = "top_left"

output_file('housing_default.html')
show(p)

运行结果

运行代码后,会生成一个名为 housing_default.html 的文件,其中包含一个饼图,展示了房产与违约关系的统计结果。

总结

本示例展示了如何使用 Python Bokeh 库对房产与违约关系进行可视化分析,并使用饼图直观展示数据分布。 Bokeh 提供了丰富的可视化工具,可以帮助我们更好地理解数据,发现数据之间的关系。

Python Bokeh 可视化分析:房产与违约关系

原文地址: https://www.cveoy.top/t/topic/jjX6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录