【科研自用】使用 核密度估计KDE 绘制概率分布PDF图

代码

引入seaborn 库的KDE

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# ieee样式
import scienceplots # 如果不存在,需要pip install scienceplots
plt.style.available # 查看可用的样式    
plt.style.use(['science','ieee']) # 选择一个样式

# 示例数据
hist_data_white = [0.5625, 0.92, 0.8571428571428571, 0.4642857142857143, 0.3888888888888889, 0.7857142857142857, 0.7142857142857143, 0.6111111111111112, 0.6111111111111112, 0.6111111111111112, 0.6428571428571429, 0.5555555555555556, 0.6666666666666666, 0.6428571428571429, 0.5555555555555556, 0.6666666666666666, 0.6666666666666666, 0.4444444444444444, 0.6111111111111112, 0.5555555555555556, 0.5714285714285714, 0.5714285714285714, 0.6666666666666666, 0.4444444444444444, 0.5, 0.4642857142857143, 0.5357142857142857, 0.625, 0.625, 0.5, 0.625, 0.375, 0.625, 0.5, 0.625, 0.5, 0.6666666666666666, 0.625, 0.3888888888888889, 0.625, 0.8333333333333334, 0.8214285714285714, 0.7777777777777778, 0.5, 0.625, 0.5, 0.5714285714285714, 0.9090909090909091, 0.8214285714285714, 0.5357142857142857, 0.625, 0.625, 0.5, 0.92, 0.84, 0.7857142857142857, 0.8, 0.8, 0.84, 0.75, 0.6111111111111112, 0.9444444444444444, 1.0, 1.0, 0.625, 0.625, 0.5714285714285714, 0.8888888888888888, 0.8214285714285714, 0.5652173913043478, 0.7857142857142857, 0.9444444444444444, 0.88, 0.625, 0.8214285714285714, 0.375, 0.625, 0.625, 0.5, 0.5, 0.8181818181818182, 0.625, 0.625, 0.5, 0.3333333333333333, 0.8333333333333334, 0.625, 0.625, 0.6071428571428571, 0.625, 0.84, 0.8636363636363636, 0.7777777777777778, 0.625, 0.84, 0.8, 0.88, 0.8636363636363636, 0.8333333333333334, 0.8214285714285714]
hist_data_black = [0.5625, 0.92, 0.8571428571428571, 0.4642857142857143, 0.3888888888888889, 0.7857142857142857, 0.7142857142857143, 0.6111111111111112, 0.6111111111111112, 0.6111111111111112, 0.6428571428571429, 0.5555555555555556, 0.6666666666666666, 0.6428571428571429, 0.5555555555555556, 0.6666666666666666, 0.6666666666666666, 0.4444444444444444, 0.6111111111111112, 0.5555555555555556, 0.5714285714285714, 0.5714285714285714, 0.6666666666666666, 0.4444444444444444, 0.5, 0.4642857142857143, 0.5357142857142857, 0.625, 0.625, 0.5, 0.625, 0.375, 0.625, 0.5, 0.625, 0.5, 0.6666666666666666, 0.625, 0.3888888888888889, 0.625, 0.8333333333333334, 0.8214285714285714, 0.7777777777777778, 0.5, 0.625, 0.5, 0.5714285714285714, 0.9090909090909091, 0.8214285714285714, 0.5357142857142857]
hist_data_evasion = [0.0625, 0.02, 0.0571428571428571, 0.1642857142857143, 0.1888888888888889, 0.3857142857142857,0.0625, 0.02, 0.0571428571428571, 0.1642857142857143, 0.1888888888888889, 0.3857142857142857]
plt.figure(figsize=(4, 2),dpi=300) # 设置画布大小
# 使用 seaborn的核密度估计KDE 绘制 PDF 曲线
sns.kdeplot(hist_data_white, shade=True, color='#7BB7D8')
sns.kdeplot(hist_data_black, shade=True, color='orange')
sns.kdeplot(hist_data_evasion, shade=True, color='red')
plt.xlabel('Values') # x轴标签
plt.ylabel('PDF') # y轴标签
plt.title('PDF of PIR') # 标题 

# 10% 分位数(找出阈值使得90%的值大于该阈值 )
threshold = np.percentile(data, 10)

# plt.legend()
plt.axvline(x=threshold, color='black', linestyle='--', label=f'10th Percentile Threshold:{threshold:.4f}')
plt.legend(['train: benign', 'train: malicious','test: evading malicious',f'threshold:{threshold:.4f}'])

plt.show()

print(f'Threshold for the 90th percentile: {threshold:.4f}')

结果图

在这里插入图片描述

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值