代码
引入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}')