机器学习数学知识点

机器学习数学知识点

嗯。。。随便写写画画图形。。。

一、 高斯分布

目录

1.1 公式与python函数封装

高斯分布公式
机器学习数学知识点

x 一个维度为D的矢量,μ数学期望, Σ协方差矩阵

import matplotlib.pyplot as plt
import numpy as np
def gaussian(x, mean, cov_matrix, cov_I=None, cov_det=None, H=None):
    """
    多维度高斯斯分布函数
    x向量, mean向量 均值, cov_matrix 协方差矩阵
    return float x对应的概率
    """
    assert x.shape == mean.shape and len(x.shape) == 1 or len(x.shape) == 2
    if len(x.shape) == 1:
        # 转成行向量
        x = x.reshape(-1, 1)
        mean = mean.reshape(-1, 1)
    
    assert x.shape == mean.shape and len(x.shape) == 2 and x.shape[1] == 1
    
    dn = cov_matrix.shape[0]
    cov_det = cov_det if cov_det is not None else np.linalg.det(cov_matrix)
    cov_I = cov_I if cov_I is not None else np.matrix(cov_matrix).I
    # 计算常量值
    if not H:
        a = 1 / np.power((2 * np.pi), dn / 2)
        b = 1 / np.power(cov_det, 0.5)
        H = a * b
        
    y = H * np.exp(-0.5 * ((x - mean).T.dot(cov_I).dot(x - mean)) )
    y = np.asarray(y).ravel()[0]
    return y

1.2 根据数据集估计概率分布函数

# 生成二维高斯分布的随机数
X = np.random.randn(500, 2)
X.shape, np.abs(X).max(0)
((500, 2), array([3.30703823, 2.89320206]))
X.max(0)
array([2.92378595, 2.89320206])
X0, X1 = X[:,0], X[:, 1]
# 可视化数据
plt.plot(X0, X1, "b.")
plt.grid(True)
plt.show()

机器学习数学知识点

X_cov = np.cov(X.T) # 协方差估计
X_mean = np.mean(X, axis=0) # 期望估计
# 计算每个点的概率
X_p = np.array([gaussian(i, X_mean, X_cov, cov_det=np.linalg.det(X_cov), cov_I=np.linalg.inv(X_cov)) for i in X])
print(X_cov)
print(X_mean)
[[ 0.96914675 -0.05832111]
 [-0.05832111  0.93365249]]
[ 0.02417388 -0.00053277]
# 绘制概率图
plt.figure(figsize=(10,10))
plt.subplot(projection="3d").scatter(X[:, 0], X[:,1], X_p)
plt.show()

机器学习数学知识点

1.3 高斯分布图形

X = np.linspace(-3, 3, 20).reshape(-1, 1)
Y = np.linspace(-3, 3, 20).reshape(-1, 1)
X.shape
(20, 1)
gx, gy = np.meshgrid(X, Y)
gx.shape, gy.shape
((20, 20), (20, 20))
xy = np.c_[gx.ravel(), gy.ravel()]
p = gaussian(xy[0].reshape(-1, 1), np.array([0, 0]).reshape(-1, 1), np.array([[1,0],[0,1]]))
p
1.964128034639744e-05
avgs = np.array([0, 0])
# covs = np.array([[2, 1],[1,2]])
covs = np.array([[1, 0],[0,1]])
cov_det = np.linalg.det(covs)
cov_I = np.matrix(covs).I
z = np.array([gaussian(i, avgs, covs, cov_det=cov_det, cov_I=cov_I) for i in xy])
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(projection='3d')
ax.scatter(gx.ravel(), gy.ravel(), z)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7f4ef83f3e50>

机器学习数学知识点

plt.contour(gx, gy, z.reshape(*gx.shape))
plt.grid(True)
# plt.gca().set_xlim(-3,3)
# plt.gca().set_ylim(-3,3)
plt.title("Contour map")
plt.show()

机器学习数学知识点


上一篇:JS浏览器兼容问题


下一篇:设计模式之桥接模式(四)