其他分享
首页 > 其他分享> > 深度学习-常用概率分布及其分布图

深度学习-常用概率分布及其分布图

作者:互联网

深度学习-常用概率分布及其分布图

文章目录

深度学习 第三章第9小节

# utils
import numpy as np


# 写一个装饰器,用来将单个点的计算函数,转成一个数组的计算
def for_numpy(index=0, key=None):
    def out(func):
        def inner(*args, **kwargs):
            x = None
            if isinstance(index, int):
                x = args[index]
                
            elif key:
                x = kwargs.get(key)
            
            if x is not None:
                res = []
                for i in x:
#                     print(i, type(i))
                    new_args = list(args)
                    if isinstance(index, int):
                        new_args[index] = i
                        
                    elif key:
                        kwargs[key] = i
                        
                    r = func(*new_args, **kwargs)
                    res.append(r)
                    
                return np.array(res)
            
            else:
                return func(*args, **kwargs)
            
        return inner   
    return out

Laplace分布(拉普拉斯分布)

import math
import matplotlib.pyplot as plt
import numpy as np


@for_numpy(index=0)
def laplace(x, u=0, l=1):
    a = 1 / (2 * l)
    x = -abs(x - u) / l
    return a * math.exp(x)

x = np.arange(-10, 10, 0.5) # numpy.ndarray
y = laplace(x,u=0,l=1)
plt.grid()
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x20619060280>]

在这里插入图片描述

指数分布

@for_numpy()
def exp_dis(x, l=1):
    return l * math.exp(-l * x) if x >=0 else 0

x = np.arange(-5, 10, 0.001)

y = exp_dis(x, l=2)

plt.grid()
plt.plot(x,y, ".r", markersize=1)
plt.show()

在这里插入图片描述

高斯分布

一维

@for_numpy()
def gaus_dis(x, u=0, l=1):
    # u 均值,l 方差
    a = math.sqrt(1 / (2 * math.pi * (l **2)))
    x = -(1/2 * (l **2)) * ((x - u) ** 2)
    return a * math.exp(x)

x = np.arange(-10, 10, 0.1)
y = gaus_dis(x)
plt.grid()
plt.plot(x, y)
plt.show()

在这里插入图片描述

多维

@for_numpy()
def gaus_dis_multi(x, u, m, n=None):
    n = x.shape[-1]
    a = (2 * math.pi) ** n * np.linalg.det(m)
    a = np.sqrt( 1 / a)
    
    m_ = np.linalg.inv(m) # 求协方差矩阵m的逆
    
    x = (x - u).T.dot(m_).dot(x-u) * (-0.5)

    return a * np.exp(x)

from matplotlib import cm

m = np.array([[1.0, 0.0],[0.0, 1.0]]) * 5 # 协方差矩阵,对称且正定的矩阵
u = np.zeros((2,))  # 均值矢量
print(m)
print()
print(u)
x = np.random.rand(5000,2) * 20 - 10

y = gaus_dis_multi(x, u, m)
# print(y)
print(y.shape)

d_x = []
d_y = []
d_z = []
for (i,j),k in zip(x, y):
    d_x.append(i)
    d_y.append(j)
    d_z.append(k)

d_x = np.array(d_x)
d_y = np.array(d_y)
d_z = np.array(d_z)

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(projection='3d')
ax.scatter(d_x, d_y,d_z, s=3) # s 控制散点的大小

[[5. 0.]
 [0. 5.]]

[0. 0.]
(5000,)





<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x2061b291130>

在这里插入图片描述

标签:10,plt,return,概率分布,分布图,深度,np,numpy,math
来源: https://blog.csdn.net/qq_42250782/article/details/120934054