本文主要是介绍概率论PRML,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
In [1]:
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt%matplotlib inlinefrom scipy.stats import normxx = np.linspace(-3, 3, 200)norm_xx = norm.pdf(xx)fig, ax = plt.subplots()ax.plot(xx, norm_xx, "r")
ax.set_ylim(0, 0.5)
ax.set_ylabel(r"$\mathcal{N}\left(x|\mu,\sigma^2\right)$", fontsize="xx-large")
ax.set_yticks([])
ax.set_yticklabels([])ax.set_xticks([0])
ax.set_xticklabels([r"$\mu$"], fontsize="xx-large")ax.text(-.1, 0.25, "$2\sigma$", fontsize="xx-large")ax.annotate("",xy=(-1, 0.24), xycoords='data',xytext=(1, 0.24), textcoords='data',arrowprops=dict(arrowstyle="<->",connectionstyle="arc3"), )plt.show()
In [2]:
xx = np.linspace(-0.9, 0.9, 100)
yy = 4 * xx - np.sin(xx * np.pi)fig, ax = plt.subplots()
ax.plot(xx, yy, color="red")ax.set_xlim(-1, 1)
ax.set_ylim(-4, 4)ax.set_xticks([0])
ax.set_xticklabels([r'$x_0$'], fontsize="xx-large")
ax.set_yticks([0])
ax.set_yticklabels([r'$y(x_0, \mathbf{w})$'], fontsize="xx-large")xx = np.linspace(-4, 4, 100)
yy = norm.pdf(xx, scale=0.5) / 5ax.plot([-1, 0], [0, 0], "g--")
ax.plot([0, 0], [-4, 4], "k")
ax.plot(yy, xx)ax.annotate("",xy=(0.75, -0.5), xycoords='data',xytext=(0.75, 0.5), textcoords='data',arrowprops=dict(arrowstyle="<->",connectionstyle="arc3"), )ax.text(0.77, -0.2, r'$2\sigma$', fontsize="xx-large")
ax.text(0.15, -1, r'$p(t|x_0,\mathbf{w}, \beta)$', fontsize="xx-large")
ax.text(0.5, 3, r'$y(x, \mathbf{w})$', fontsize="xx-large")plt.show()
In [3]:
def phi(x, M):return x[:,None] ** np.arange(M + 1)N = 10# 生成 0,1 之间等距的 N 个 数
x_tr = np.linspace(0, 1, N)# 计算 t
t_tr = np.sin(2 * np.pi * x_tr) + 0.25 * np.random.randn(N)# 加正则项的解
M = 9
alpha = 5e-3
beta = 11.1
lam = alpha / betaphi_x_tr = phi(x_tr, M)
A_0 = phi_x_tr.T.dot(phi_x_tr) + lam * np.eye(M+1)
y_0 = t_tr.dot(phi_x_tr)# 求解 Aw=y
coeff = np.linalg.solve(A_0, y_0)[::-1]f = np.poly1d(coeff)# 绘图xx = np.linspace(0, 1, 500)# Bayes估计的均值和标准差
S = np.linalg.inv(A_0 * beta)m_xx = beta * phi(xx, M).dot(S).dot(y_0)
s_xx = np.sqrt(1 / beta + phi(xx, M).dot(S).dot(phi(xx, M).T).diagonal())fig, ax = plt.subplots()
ax.plot(x_tr, t_tr, 'co')
ax.plot(xx, np.sin(2 * np.pi * xx), 'g')
ax.plot(xx, f(xx), 'r')
ax.fill_between(xx, m_xx-s_xx, m_xx+s_xx, color="pink")
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-1.5, 1.5)
ax.set_xticks([0, 1])
ax.set_yticks([-1, 0, 1])
ax.set_xlabel("$x$", fontsize="x-large")
ax.set_ylabel("$t$", fontsize="x-large")plt.show()
这篇关于概率论PRML的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!