[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
KL散度
KL 散度是一种衡量两个概率分布的匹配程度的指标,两个分布差异越大,KL散度越大。
定义:
其中 p(x) 是真实分布,q(x)是目标分布(建模目标分布),如果两个分布完全匹配,那么
第一组:数据集为采集100个人的年龄,为以下表格所示,我们使用KL散度去研究最符合的分布类型。
age012345678910总数
count3671113181511754100
尝试1:使用均匀分布建立模型
可视化为:黄色的为建立的目标均匀分布模型,与蓝色真实分布的对比。
尝试2:使用高斯分布建立模型
可视化为:(红色虚线是相同与拟合的一条正态分布曲线,蓝色条形图为概率密度):
计算分析 :
如何判断真实分布更接近均匀分布还是高斯分布,用肉眼看是很不准确的,用KL散度来衡量真实分布去匹配目标分布所损失的信息量。所以能够将模型量化去比较从而判断出接近哪种分布。
1、计算与均匀分布的KL散度:
import numpy as np import math count=np.array([3,6,7,11,13,18,15,11,7,5,4])
count_rate=count/100 balance_rate=1/11 sum=0 for i in range(11):
sum+=count_rate[i]*math.log(count_rate[i]/balance_rate) print(sum)
计算结果为:0.12899493763053263
2、计算与高斯分布的KL散度:
def gaosi(x): mu=5.03 sigma=2.4349743325135895
t1=1/(sigma*math.sqrt(2*math.pi)) t2=((x-mu)**2)/(2*sigma*sigma) return
math.exp(-1*t2)*t1 count=np.array([3,6,7,11,13,18,15,11,7,5,4])
count_rate=count/100 sum=0 for i in range(11):
sum+=count_rate[i]*math.log(count_rate[i]/gaosi(i)) print(sum)
计算结果为:0.03997441345364968
结论:
在只考虑均匀分布模型与高斯分布模型的情况下,用本身去拟合目标模型时,匹配高斯分布所损失的信息量最少,计算发现该数据集的分布更符合高斯分布。