-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_DrawSimCoef_ch.py
More file actions
94 lines (78 loc) · 3.34 KB
/
A_DrawSimCoef_ch.py
File metadata and controls
94 lines (78 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import matplotlib.pyplot as plt
import warnings
import numpy as np
warnings.filterwarnings("ignore")
plt.rcParams['font.weight'] = 'bold'
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus']=False
title_font = {
#'fontsize': rcParams['axes.titlesize'], # 设置成和轴刻度标签一样的大小
'fontsize': 13,
#'fontweight': rcParams['axes.titleweight'], # 设置成和轴刻度标签一样的粗细
'fontweight': 'bold',
#'color': rcParams['axes.titlecolor'], # 设置成和轴刻度标签一样的颜色
# 'color': 'black',
}
label_font = {
#'fontsize': rcParams['axes.titlesize'], # 设置成和轴刻度标签一样的大小
'fontsize': 12,
#'fontweight': rcParams['axes.titleweight'], # 设置成和轴刻度标签一样的粗细
'fontweight': 'bold',
#'color': rcParams['axes.titlecolor'], # 设置成和轴刻度标签一样的颜色
'color': 'red',
}
def SimCoef(sim, label, threshold):
if sim<=threshold:
coef=threshold
else:
if label==1:
coef=sim*2
else:
coef=threshold*(1-sim)
return coef
def get_y_values(func, x_values, label=1, threshold=0.5):
"""
func: 绘图函数
x_values: 横坐标
label: true值: 0:unchg 1:chg
"""
y_values = []
for i in x_values:
y_values.append(func(i, label, threshold))
return y_values
# 横坐标 x_values
# x_values = np.linspace(-1 * np.pi, 1 * np.pi, 1000) # 生成从 -2π 到 2π 的等间距的 1000 个点
x_values = np.linspace(start=-1 , stop=1 , num=2000)
x_values_1 = np.linspace(start=-1 , stop=0.5 , num=2000)
# 纵坐标 y_values
y_values_0 = get_y_values(func=SimCoef,x_values=x_values,label=0, threshold=0.5)
y_values_1 = get_y_values(func=SimCoef,x_values=x_values,label=1, threshold=0.5)
y_values_unchg_chg = np.linspace(start=0.5 , stop=0.5 , num=2000)
y_values_0_0_4 = get_y_values(func=SimCoef,x_values=x_values,label=0, threshold=0.4)
y_values_1_0_4 = get_y_values(func=SimCoef,x_values=x_values,label=1, threshold=0.4)
y_values_chg = np.linspace(start=0.515 , stop=0.515 , num=2000)
y_values_unchg = np.linspace(start=0.485 , stop=0.485 , num=2000)
# 绘制函数图像
# plt.plot(x_values, y_values_0, label='label:0 unchg')
# plt.plot(x_values, y_values_1, label='label:1 chg')
plt.plot(x_values, y_values_0, label='未变化-未变化', color='blue')
plt.plot(x_values, y_values_1, label='变化-变化', color='orange')
plt.plot(x_values, y_values_unchg_chg, label='变化-未变化', color='green')
plt.plot(x_values_1, y_values_unchg, color='blue')
plt.plot(x_values_1, y_values_chg, color='orange')
# plt.plot(x_values, y_values_0_0_4, label='Sim-Coef-0-0.4')
# plt.plot(x_values, y_values_1_0_4, label='Sim-Coef-1-0.4')
# 添加标题和标签
# plt.title('Cosine Similarity - Attention Coefficient(\u03B1)', fontdict=title_font)
# plt.xlabel('Cosine Similarity', fontdict=label_font)
# plt.ylabel('Attention Coefficient (\u03B1)', fontdict=label_font)
plt.title('余弦相似度 - 注意力系数(\u03B1)', fontdict=title_font)
plt.xlabel('余弦相似度', fontdict=label_font)
plt.ylabel('注意力系数 (\u03B1)', fontdict=label_font)
plt.scatter(0.5, 0.5, label='阈值')
# 显示图例
plt.legend()
# 显示图像
# plt.grid(True)
plt.show()
print("OK!")