# 统计学关我什么事？

1. 一个例子

2. 矩阵树图

# 导入库
from pyecharts import options as opts
from pyecharts.charts import TreeMap
from pyecharts.globals import ThemeType

# 真命天子的概率
a = 0.1
# 无关路人的概率
b = 1 - a
# 送给真命天子的概率
c = 0.425
# 不送真命天子的概率
d = 1 - c
# 送给无关路人的概率
e = 0.22
# 不送无关路人的概率
f = 1 - e

# 定义数据
data = [
{
"value": 100,
"name": "后验概率",
"children": [
{
"value": 100,
"name": "送巧克力",
"children": [
{"value": round(a*c/(a*c+b*e)*100,2), "name": "送给真命天子"},
{"value": round(b*e/(a*c+b*e)*100,2), "name": "送给无关路人"},
],
}
],
},
{
"value": 100,
"name": "先验概率",
"children": [
{
"value": 100,
"name": "条件概率",
"children": [
{"value": round(a*c*100,2), "name": "送给真命天子"},
{"value": round(a*d*100,2), "name": "不送真命天子"},
{"value": round(b*e*100,2), "name": "送给无关路人"},
{"value": round(b*f*100,2), "name": "不送无关路人"},
],
}
],
},
]

# 绘制矩形树图
c = (
TreeMap(init_opts=opts.InitOpts(width='800px', height='800px', theme=ThemeType.LIGHT))
data,
levels=[
opts.TreeMapLevelsOpts(
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color="#00589F", border_width=4, gap_width=4
)
),
opts.TreeMapLevelsOpts(
color_saturation=[0.3, 0.5],
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color_saturation=0.7, gap_width=2, border_width=2
),
),
opts.TreeMapLevelsOpts(
color_saturation=[0.3, 0.5],
treemap_itemstyle_opts=opts.TreeMapItemStyleOpts(
border_color_saturation=0.6, gap_width=1
),
),
opts.TreeMapLevelsOpts(color_saturation=[0.3, 0.5]),
],
)
.set_global_opts(title_opts=opts.TitleOpts(
title="推测送巧克力的女同事的心意，她把你当成真命天子的概率是"+'%.2f%%' % (a*c/(a*c+b*e)*100),
pos_left="center", pos_top="30",
))
.render("推测送巧克力的女同事的心意.html")
)


3. 小结

☟分享  ☟收藏     :blue_heart:     点赞 ☟ 在看