Bohrium
robot
新建

空间站广场

论文
Notebooks
比赛
课程
Apps
我的主页
我的Notebooks
我的论文库
我的足迹

我的工作空间

任务
节点
文件
数据集
镜像
项目
数据库
公开
Bk2_Ch32_Dirichlet分布_02Dirichlet分布PDF投影到斜面
《可视之美》
python
鸾尾花书
数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-29
推荐镜像 :Basic Image:bohrium-notebook:2023-04-07
推荐机型 :c2_m4_cpu
Dirichlet分布PDF投影到斜面
自定义可视化函数

©️ Copyright 2024 @ Authors
共享协议:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
作者信息:Notebook原地址为 https://github.com/Visualize-ML

代码
文本

Chapter 32

Dirichlet分布PDF投影到斜面

Book_2《可视之美》 | 鸢尾花书:从加减乘除到机器学习

代码
文本
[1]
from scipy.stats import dirichlet
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
from matplotlib import cm

import os
# 如果文件夹不存在,创建文件夹
if not os.path.isdir("Figures"):
os.makedirs("Figures")
代码
文本
[2]
theta_1_array = np.linspace(0,1,1001)
theta_2_array = np.linspace(0,1,1001)
tt1,tt2 = np.meshgrid(theta_1_array,theta_2_array)
tt3 = 1 - tt1 - tt2
mask = ((tt3 > 1) | (tt3 < 0))
tt1[mask] = None
tt2[mask] = None
tt3[mask] = None
代码
文本

自定义可视化函数

代码
文本
[3]
def visualize_Dirichlet(alpha_array, num = 50):
PDF = dirichlet.pdf([tt1.ravel(),tt2.ravel(),tt3.ravel()], alpha_array)
fig = plt.figure(figsize=(5, 5))

all_contours = plt.contour(tt1, tt2, PDF.reshape(tt1.shape),
levels = 20,
cmap='RdYlBu_r')
plt.clf()
norm = Normalize(vmin=all_contours.levels.min(),
vmax=all_contours.levels.max(),
clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.RdYlBu_r)

ax = plt.axes(projection="3d")

for level_idx, ctr_idx in zip(all_contours.levels,
all_contours.allsegs):

for i in range(0,len(ctr_idx)):

t1_i,t2_i = ctr_idx[i][:,0],ctr_idx[i][:,1]
t3_i = 1 - t1_i - t2_i

# 绘制映射结果
ax.plot(t1_i, t2_i, t3_i,
color = mapper.to_rgba(level_idx),
linewidth = 1)

ax.set_proj_type('ortho')
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.plot((1,0),(0,1),(0,0),lw = 1,c = 'k')
ax.plot((0,0),(1,0),(0,1),lw = 1,c = 'k')
ax.plot((1,0),(0,0),(0,1),lw = 1,c = 'k')

ax.set_xticks([0,1])
ax.set_yticks([0,1])
ax.set_zticks([0,1])

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
# ax.view_init(azim=20, elev=20)
ax.view_init(azim=30, elev=30)
ax.set_xlabel(r'$\theta_1$')
ax.set_ylabel(r'$\theta_2$')
ax.set_zlabel(r'$\theta_3$')
ax.set_box_aspect(aspect = (1,1,1))

ax.grid()
title = '_'.join(str(v) for v in alpha_array)
title = 'alphas_' + title

fig.savefig('Figures/斜面等高线_' + title + '.svg', format='svg')
代码
文本
[4]
alpha_array = [1, 2, 4]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[5]
alpha_array = [4,1,2]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[6]
alpha_array = [2,4,1]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[7]
alpha_array = [2,4,8]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[8]
alpha_array = [4,8,2]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[9]
alpha_array = [8,2,4]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[10]
alpha_array = [2,2,2]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[11]
alpha_array = [8,8,8]
visualize_Dirichlet(alpha_array)
/tmp/ipykernel_911/891849301.py:34: MatplotlibDeprecationWarning: The w_xaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use xaxis instead.
  ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:35: MatplotlibDeprecationWarning: The w_yaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use yaxis instead.
  ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
/tmp/ipykernel_911/891849301.py:36: MatplotlibDeprecationWarning: The w_zaxis attribute was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use zaxis instead.
  ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
代码
文本
[12]
# Repo: https://github.com/Visualize-ML
# Book 2 Beauty of Visualization | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2023
代码
文本
《可视之美》
python
鸾尾花书
数据可视化
《可视之美》python鸾尾花书数据可视化
点个赞吧
本文被以下合集收录
Book2《可视之美》 | 鸢尾花书:从加减乘除到机器学习
刀刀
更新于 2024-06-03
198 篇3 人关注
推荐阅读
公开
Bk2_Ch15_网格曲面_03可视化 Dirichlet 分布
《可视之美》python鸾尾花书数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-28
公开
Bk2_Ch32_Dirichlet分布_01平面上可视化Dirichlet分布
《可视之美》python鸾尾花书数据可视化
《可视之美》python鸾尾花书数据可视化
刀刀
发布于 2024-05-29