Skip to content

Commit 647d64f

Browse files
authored
Add files via upload
1 parent 81889ae commit 647d64f

File tree

82 files changed

+30416
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+30416
-0
lines changed

Book5_Ch02_Python_Codes/Bk5_Ch02_01.ipynb

+2,248
Large diffs are not rendered by default.

Book5_Ch02_Python_Codes/X_df_iris.pkl

5.44 KB
Binary file not shown.

Book5_Ch02_Python_Codes/iris_sns.pkl

5.79 KB
Binary file not shown.

Book5_Ch03_Python_Codes/Bk5_Ch03_01.ipynb

+173
Large diffs are not rendered by default.

Book5_Ch03_Python_Codes/Bk5_Ch03_02.ipynb

+245
Large diffs are not rendered by default.

Book5_Ch04_Python_Codes/Bk5_Ch04_01.ipynb

+782
Large diffs are not rendered by default.

Book5_Ch04_Python_Codes/Bk5_Ch04_02.ipynb

+903
Large diffs are not rendered by default.

Book5_Ch04_Python_Codes/X_df.pkl

5.79 KB
Binary file not shown.

Book5_Ch05_Python_Codes/Bk5_Ch05_01.ipynb

+173
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_02.ipynb

+269
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_03.ipynb

+209
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_04.ipynb

+306
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_05.ipynb

+168
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_06.ipynb

+170
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_07.ipynb

+183
Large diffs are not rendered by default.

Book5_Ch05_Python_Codes/Bk5_Ch05_08.ipynb

+184
Large diffs are not rendered by default.

Book5_Ch06_Python_Codes/Bk5_Ch06_01.ipynb

+1,367
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_01.ipynb

+253
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_02.ipynb

+193
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_03.ipynb

+200
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_04.ipynb

+191
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_05.ipynb

+185
Large diffs are not rendered by default.

Book5_Ch07_Python_Codes/Bk5_Ch07_06.ipynb

+199
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
###############
3+
# Authored by Weisheng Jiang
4+
# Book 5 | From Basic Arithmetic to Machine Learning
5+
# Published and copyrighted by Tsinghua University Press
6+
# Beijing, China, 2025
7+
###############
8+
9+
import streamlit as st # 导入Streamlit库,用于创建交互式Web应用程序
10+
from scipy.stats import beta # 从SciPy库中导入Beta分布相关函数
11+
import matplotlib.pyplot as plt # 导入Matplotlib库,用于绘图
12+
import numpy as np # 导入NumPy库,用于数值计算
13+
14+
x_array = np.linspace(0, 1, 200) # 生成一个包含200个点的数组,范围在[0,1],用于绘制Beta分布的PDF
15+
16+
with st.sidebar: # 在Streamlit的侧边栏中添加内容
17+
st.write('Beta distribution PDF') # 在侧边栏显示标题文字
18+
st.latex(r'''
19+
{\displaystyle {\begin{aligned}f(x;\alpha ,\beta )&=
20+
\mathrm {constant} \cdot x^{\alpha -1}(1-x)^{\beta -1}\\
21+
&=
22+
{\frac {x^{\alpha -1}(1-x)^{\beta -1}}
23+
{\displaystyle \int _{0}^{1}u^{\alpha -1}
24+
(1-u)^{\beta -1}\,du}}\\
25+
&=
26+
{\frac {\Gamma (\alpha +\beta )}
27+
{\Gamma (\alpha )\Gamma (\beta )}}\,x^{\alpha -1}(1-x)^{\beta -1}\\
28+
&=
29+
{\frac {1}{\mathrm {B} (\alpha ,\beta )}}
30+
x^{\alpha -1}(1-x)^{\beta -1}\end{aligned}}}
31+
''') # 显示Beta分布概率密度函数的LaTeX公式
32+
33+
alpha_input = st.slider('alpha', min_value=0.0, max_value=10.0, value=2.0, step=0.1) # 创建一个滑块,用于调整参数α
34+
beta_input = st.slider('beta', min_value=0.0, max_value=10.0, value=2.0, step=0.1) # 创建另一个滑块,用于调整参数β
35+
36+
mean_loc = alpha_input / (alpha_input + beta_input) # 计算Beta分布的均值
37+
pdf_array = beta.pdf(x_array, alpha_input, beta_input) # 计算x_array对应的Beta分布PDF值
38+
39+
fig, ax = plt.subplots(figsize=(8, 8)) # 创建一个大小为8x8的图表
40+
41+
title_idx = '\u03B1 = ' + str(alpha_input) + '; \u03B2 = ' + str(beta_input) # 设置标题,显示当前α和β的值
42+
ax.plot(x_array, pdf_array, 'b', lw=1) # 绘制Beta分布PDF曲线,蓝色线条,线宽为1
43+
44+
ax.axvline(x=mean_loc, c='r', ls='--') # 在均值位置绘制一条红色虚线
45+
ax.set_title(title_idx) # 设置图表标题
46+
ax.set_xlim(0, 1) # 设置x轴范围为[0,1]
47+
ax.set_ylim(0, 4) # 设置y轴范围为[0,4]
48+
ax.set_xticks([0, 0.5, 1]) # 设置x轴刻度为0, 0.5, 1
49+
ax.set_yticks([0, 2, 4]) # 设置y轴刻度为0, 2, 4
50+
ax.spines.right.set_visible(False) # 隐藏右边框线
51+
ax.spines.top.set_visible(False) # 隐藏上边框线
52+
ax.yaxis.set_ticks_position('left') # 将y轴刻度设置在左侧
53+
ax.xaxis.set_ticks_position('bottom') # 将x轴刻度设置在底部
54+
ax.tick_params(axis="x", direction='in') # 设置x轴刻度向内
55+
ax.tick_params(axis="y", direction='in') # 设置y轴刻度向内
56+
57+
st.pyplot(fig) # 在Streamlit应用中显示绘制的图表
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
###############
3+
# Authored by Weisheng Jiang
4+
# Book 5 | From Basic Arithmetic to Machine Learning
5+
# Published and copyrighted by Tsinghua University Press
6+
# Beijing, China, 2025
7+
###############
8+
9+
10+
import streamlit as st # 导入Streamlit库,用于构建交互式Web应用程序
11+
from scipy.stats import dirichlet # 从SciPy库中导入Dirichlet分布相关函数
12+
import matplotlib.pyplot as plt # 导入Matplotlib库,用于绘图
13+
import numpy as np # 导入NumPy库,用于数值计算
14+
15+
x_array = np.linspace(0, 1, 200) # 创建一个数组,包含[0, 1]范围内的200个点,用于绘图或计算
16+
17+
with st.sidebar: # 在Streamlit应用程序的侧边栏中添加内容
18+
st.write('Dirichlet distribution PDF') # 显示标题文字
19+
st.latex(r'''
20+
{\displaystyle f\left(x_{1},\ldots ,x_{K};\alpha _{1},\ldots ,\alpha _{K}\right)=
21+
{\frac {1}{\mathrm {B} ({\boldsymbol {\alpha }})}}\prod _{i=1}^{K}x_{i}^{\alpha _{i}-1}}''') # 显示Dirichlet分布的PDF公式
22+
23+
st.latex(r'''{\displaystyle \mathrm {B} ({\boldsymbol {\alpha }})=
24+
{\frac {\prod \limits _{i=1}^{K}\Gamma (\alpha _{i})}{\Gamma \left(\sum \limits _{i=1}^{K}\alpha _{i}\right)}},\qquad {\boldsymbol {\alpha }}=(\alpha _{1},\ldots ,\alpha _{K}).}''') # 显示Dirichlet分布的归一化常数公式
25+
26+
alpha_1_input = st.slider('alpha_1', min_value=1.0, max_value=10.0, value=2.0, step=0.1) # 创建一个滑块,用于设置α_1参数
27+
alpha_2_input = st.slider('alpha_2', min_value=1.0, max_value=10.0, value=2.0, step=0.1) # 创建一个滑块,用于设置α_2参数
28+
alpha_3_input = st.slider('alpha_3', min_value=1.0, max_value=10.0, value=2.0, step=0.1) # 创建一个滑块,用于设置α_3参数
29+
30+
rv = dirichlet([alpha_1_input, alpha_2_input, alpha_3_input]) # 根据输入的α参数创建一个Dirichlet分布对象
31+
32+
x1 = np.linspace(0, 1, 201) # 生成x_1的值范围,包含201个点
33+
x2 = np.linspace(0, 1, 201) # 生成x_2的值范围,包含201个点
34+
35+
xx1, xx2 = np.meshgrid(x1, x2) # 创建网格点,用于在二维空间表示x_1和x_2的取值
36+
37+
xx3 = 1.0 - xx1 - xx2 # 计算x_3的值,满足Dirichlet分布的条件x_1 + x_2 + x_3 = 1
38+
xx3 = np.where(xx3 > 0.0, xx3, np.nan) # 如果x_3的值小于0,将其替换为NaN,避免无效计算
39+
40+
PDF_ff = rv.pdf(np.array(([xx1.ravel(), xx2.ravel(), xx3.ravel()]))) # 计算每个网格点对应的Dirichlet分布PDF值
41+
PDF_ff = np.reshape(PDF_ff, xx1.shape) # 将PDF值重新形状化为与网格一致的形状
42+
43+
import plotly as py # 导入Plotly库,用于高级绘图
44+
import plotly.graph_objects as go # 从Plotly库中导入图形对象
45+
from plotly.subplots import make_subplots # 导入子图创建函数
46+
47+
fig = make_subplots(rows=1, cols=1,
48+
specs=[[{'is_3d': True}]]) # 创建一个3D子图,包含1行1列
49+
50+
fig.add_trace(go.Surface(x=xx1, y=xx2, z=xx3, surfacecolor=PDF_ff, colorscale='RdYlBu_r'), 1, 1)
51+
# 添加3D表面图,x和y为网格点,z为x_3,颜色根据PDF值设置,使用‘RdYlBu_r’颜色映射
52+
53+
fig.update_layout(scene=dict(
54+
xaxis_title=r'x_1', # 设置x轴标题为x_1
55+
yaxis_title=r'x_2', # 设置y轴标题为x_2
56+
zaxis_title=r'x_3'), # 设置z轴标题为x_3
57+
width=700, # 设置图形宽度
58+
margin=dict(r=20, b=10, l=10, t=10)) # 设置图形边距
59+
60+
st.plotly_chart(fig, use_container_width=True) # 在Streamlit应用程序中显示Plotly图表,并使图表适应容器宽度

Book5_Ch08_Python_Codes/Bk5_Ch08_01.ipynb

+826
Large diffs are not rendered by default.

Book5_Ch08_Python_Codes/X_df.pkl

5.79 KB
Binary file not shown.

Book5_Ch09_Python_Codes/Bk5_Ch09_01.ipynb

+282
Large diffs are not rendered by default.

Book5_Ch09_Python_Codes/Bk5_Ch09_02.ipynb

+421
Large diffs are not rendered by default.

Book5_Ch09_Python_Codes/Bk5_Ch09_03.ipynb

+401
Large diffs are not rendered by default.

Book5_Ch09_Python_Codes/Bk5_Ch09_04.ipynb

+207
Large diffs are not rendered by default.

Book5_Ch09_Python_Codes/Bk5_Ch09_05.ipynb

+718
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
###############
3+
# Authored by Weisheng Jiang
4+
# Book 5 | From Basic Arithmetic to Machine Learning
5+
# Published and copyrighted by Tsinghua University Press
6+
# Beijing, China, 2025
7+
###############
8+
9+
import streamlit as st # 导入Streamlit库,用于创建交互式Web应用程序
10+
from scipy.stats import beta # 从SciPy库导入Beta分布相关函数(此代码未使用)
11+
import matplotlib.pyplot as plt # 导入Matplotlib库,用于绘图
12+
import numpy as np # 导入NumPy库,用于数值计算
13+
14+
def uni_normal_pdf(x, mu, sigma): # 定义一个函数,用于计算一元正态分布的概率密度函数
15+
coeff = 1 / np.sqrt(2 * np.pi) / sigma # 计算正态分布的系数部分 1/(σ√(2π))
16+
z = (x - mu) / sigma # 计算标准化后的值 (x-μ)/σ
17+
f_x = coeff * np.exp(-1 / 2 * z**2) # 计算概率密度值 f(x) = coeff * exp(-1/2 * z^2)
18+
return f_x # 返回概率密度值
19+
20+
x_array = np.linspace(-5, 5, 200) # 创建一个包含[-5, 5]范围内200个点的数组,用于绘制PDF
21+
22+
with st.sidebar: # 在Streamlit的侧边栏中添加控件
23+
mu_input = st.slider('mu', min_value=-5.0, max_value=5.0, value=0.0, step=0.2)
24+
# 添加滑块控件,用于调整正态分布的均值μ,初始值为0.0,范围[-5, 5],步长为0.2
25+
sigma_input = st.slider('sigma', min_value=0.0, max_value=4.0, value=1.0, step=0.1)
26+
# 添加滑块控件,用于调整正态分布的标准差σ,初始值为1.0,范围[0, 4],步长为0.1
27+
28+
st.write('Univariate Gaussian distribution PDF') # 在主页面显示正态分布的标题
29+
st.latex(r'''{\displaystyle f(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}e^{-{\frac {1}{2}}\left({\frac {x-\mu }{\sigma }}\right)^{2}}}''')
30+
# 在主页面显示正态分布的LaTeX公式
31+
32+
pdf_array = uni_normal_pdf(x_array, mu_input, sigma_input) # 计算用户选择参数下的正态分布PDF值
33+
34+
fig, ax = plt.subplots(figsize=(8, 5)) # 创建一个大小为8x5的绘图窗口
35+
36+
ax.plot(x_array, pdf_array, 'b', lw=1)
37+
# 绘制用户选择参数下的正态分布PDF曲线,蓝色线条,线宽为1
38+
39+
ax.axvline(x=mu_input, c='r', ls='--')
40+
# 在均值位置绘制一条红色虚线
41+
ax.axvline(x=mu_input + sigma_input, c='r', ls='--')
42+
# 在均值加标准差位置绘制一条红色虚线
43+
ax.axvline(x=mu_input - sigma_input, c='r', ls='--')
44+
# 在均值减标准差位置绘制一条红色虚线
45+
46+
# 绘制标准正态分布
47+
ax.plot(x_array, uni_normal_pdf(x_array, 0, 1), c=[0.8, 0.8, 0.8], lw=1)
48+
# 绘制标准正态分布PDF曲线,灰色线条,线宽为1
49+
50+
ax.axvline(x=0, c=[0.8, 0.8, 0.8], ls='--')
51+
# 在标准正态分布均值位置绘制灰色虚线
52+
ax.axvline(x=0 + 1, c=[0.8, 0.8, 0.8], ls='--')
53+
# 在标准正态分布均值加标准差位置绘制灰色虚线
54+
ax.axvline(x=0 - 1, c=[0.8, 0.8, 0.8], ls='--')
55+
# 在标准正态分布均值减标准差位置绘制灰色虚线
56+
57+
ax.set_xlim(-5, 5) # 设置x轴范围为[-5, 5]
58+
ax.set_ylim(0, 1) # 设置y轴范围为[0, 1]
59+
60+
ax.spines.right.set_visible(False) # 隐藏右边框线
61+
ax.spines.top.set_visible(False) # 隐藏上边框线
62+
ax.yaxis.set_ticks_position('left') # 将y轴刻度设置在左侧
63+
ax.xaxis.set_ticks_position('bottom') # 将x轴刻度设置在底部
64+
ax.tick_params(axis="x", direction='in') # 设置x轴刻度线向内
65+
ax.tick_params(axis="y", direction='in') # 设置y轴刻度线向内
66+
67+
st.pyplot(fig) # 在Streamlit应用程序中显示绘制的图表

Book5_Ch09_Python_Codes/X_df.pkl

5.44 KB
Binary file not shown.

Book5_Ch10_Python_Codes/Bk5_Ch10_01.ipynb

+576
Large diffs are not rendered by default.

Book5_Ch10_Python_Codes/Bk5_Ch10_02.ipynb

+277
Large diffs are not rendered by default.

Book5_Ch10_Python_Codes/Bk5_Ch10_03.ipynb

+241
Large diffs are not rendered by default.

Book5_Ch10_Python_Codes/Bk5_Ch10_04.ipynb

+435
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
3+
###############
4+
# Authored by Weisheng Jiang
5+
# Book 5 | From Basic Arithmetic to Machine Learning
6+
# Published and copyrighted by Tsinghua University Press
7+
# Beijing, China, 2025
8+
###############
9+
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
from scipy.stats import multivariate_normal
13+
from matplotlib import cm # 导入 matplotlib 中的颜色映射功能
14+
import streamlit as st # 导入 Streamlit 库,用于创建交互式 Web 应用程序
15+
16+
# 使用 Streamlit 创建侧边栏,允许用户调整参数
17+
with st.sidebar:
18+
rho = st.slider('rho', min_value = -0.95, max_value = 0.95, value = 0.0, step = 0.05) # 相关系数 rho 的滑块
19+
sigma_X = st.slider('sigma_X', min_value = 0.5, max_value = 3.0, value = 1.0, step = 0.1) # X 的标准差滑块
20+
sigma_Y = st.slider('sigma_Y', min_value = 0.5, max_value = 3.0, value = 1.0, step = 0.1) # Y 的标准差滑块
21+
22+
# 设置均值 μ_X 和 μ_Y 为 0
23+
mu_X = 0
24+
mu_Y = 0
25+
26+
# 显示标题和公式
27+
st.write('Bivariate Gaussian distribution PDF') # 显示标题
28+
st.latex(r'''{\displaystyle f(x,y)={\frac {1}{2\pi \sigma _{X}\sigma _{Y}{\sqrt {1-\rho ^{2}}}}}\exp \left(-{\frac {1}{2(1-\rho ^{2})}}\left[\left({\frac {x-\mu _{X}}{\sigma _{X}}}\right)^{2}-2\rho \left({\frac {x-\mu _{X}}{\sigma _{X}}}\right)\left({\frac {y-\mu _{Y}}{\sigma _{Y}}}\right)+\left({\frac {y-\mu _{Y}}{\sigma _{Y}}}\right)^{2}\right]\right)}''')
29+
# 显示双变量正态分布概率密度函数的数学公式
30+
31+
# 构造均值和协方差矩阵
32+
mu = [mu_X, mu_Y] # 均值向量
33+
Sigma = [[sigma_X**2, sigma_X*sigma_Y*rho], # 协方差矩阵
34+
[sigma_X*sigma_Y*rho, sigma_Y**2]]
35+
36+
# 定义绘图范围和网格
37+
width = 4 # 定义 X 和 Y 的绘图范围宽度
38+
X = np.linspace(-width,width,321) # 在 [-width, width] 范围内生成 321 个 X 坐标点
39+
Y = np.linspace(-width,width,321) # 在 [-width, width] 范围内生成 321 个 Y 坐标点
40+
41+
XX, YY = np.meshgrid(X, Y) # 生成网格
42+
43+
XXYY = np.dstack((XX, YY)) # 将 X 和 Y 网格堆叠为三维数组
44+
bi_norm = multivariate_normal(mu, Sigma) # 定义双变量正态分布
45+
46+
#%% 计算联合 PDF 并进行可视化
47+
f_X_Y_joint = bi_norm.pdf(XXYY) # 计算联合概率密度函数的值
48+
49+
# 3D 可视化
50+
fig = plt.figure() # 创建新的图形对象
51+
ax = fig.add_subplot(projection='3d') # 添加 3D 子图
52+
53+
ax.plot_wireframe(XX,YY, f_X_Y_joint,
54+
rstride=10, cstride=10, # 设置网格线步长
55+
color = [0.3,0.3,0.3], # 设置颜色
56+
linewidth = 0.25) # 设置网格线宽度
57+
58+
ax.contour3D(XX,YY, f_X_Y_joint,15, # 添加 3D 等高线
59+
cmap = 'RdYlBu_r') # 使用红黄蓝颜色映射
60+
61+
# 设置轴标签
62+
ax.set_xlabel('$x$')
63+
ax.set_ylabel('$y$')
64+
ax.set_zlabel('$f_{X,Y}(x,y)$')
65+
66+
ax.set_proj_type('ortho') # 设置正交投影
67+
# 修改轴网格线的样式
68+
ax.xaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
69+
ax.yaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
70+
ax.zaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
71+
72+
# 设置绘图范围
73+
ax.set_xlim(-width, width)
74+
ax.set_ylim(-width, width)
75+
ax.set_zlim(f_X_Y_joint.min(),f_X_Y_joint.max())
76+
77+
# 设置视角
78+
ax.view_init(azim=-120, elev=30) # 设置方位角和仰角
79+
plt.tight_layout() # 调整布局以避免重叠
80+
81+
st.pyplot(fig) # 在 Streamlit 中显示图形
82+
83+
#%% 绘制填充等高线图
84+
fig, ax = plt.subplots(figsize=(7, 7)) # 创建新的图形和子图
85+
86+
# 绘制双变量正态分布的填充等高线
87+
plt.contourf(XX, YY, f_X_Y_joint, 20, cmap=cm.RdYlBu_r) # 使用填充等高线
88+
plt.axvline(x = mu_X, color = 'k', linestyle = '--') # 绘制垂直线表示 μ_X
89+
plt.axhline(y = mu_Y, color = 'k', linestyle = '--') # 绘制水平线表示 μ_Y
90+
91+
# 设置轴标签
92+
ax.set_xlabel('$x$')
93+
ax.set_ylabel('$y$')
94+
95+
st.pyplot(fig) # 在 Streamlit 中显示图形
96+

0 commit comments

Comments
 (0)