修改测试

This commit is contained in:
lee
2025-07-01 13:39:34 +08:00
parent 96a9445761
commit ed6a4144fa
8 changed files with 55 additions and 14 deletions

View File

@ -26,6 +26,17 @@ class ShowPR:
values.append(value)
return values
def calculate_mena(self, ratio=0.5):
values = []
for data in self.prec_value:
thres_num = int(len(data)*ratio)
sorted_data = sorted(data, reverse=True)
value = sorted_data[:thres_num]
if len(value) == 0:
value = sorted_data[:1]
values.append(sum(value)/len(value))
return values
def _calculate_pr(self, prec_value):
FN, FP, TN, TP = 0, 0, 0, 0
for output, target in zip(prec_value, self.tags):
@ -53,7 +64,7 @@ class ShowPR:
# print("TP>>{}, FP>>{}, TN>>{}, FN>>{}".format(TP, FP, TN, FN))
return prec, recall, tn_prec, tn_recall
def calculate_multiple(self, ratio=0.2):
def calculate_multiple_1(self, ratio=0.2): # 方案1 计算满足阈值判断的占比(ratio)
recall, recall_TN, PrecisePos, PreciseNeg = [], [], [], []
for thre in self.thres:
prec_value = []
@ -73,6 +84,21 @@ class ShowPR:
recall_TN.append(tn_recall)
return recall, recall_TN, PrecisePos, PreciseNeg
def calculate_multiple_2(self, ratio=0.2): # 方案2 计算前ratio的预测试值的平均值大于thre为赠品小于为非赠品
recall, recall_TN, PrecisePos, PreciseNeg = [], [], [], []
event_value = self.calculate_mena(ratio)
for thre in self.thres:
prec_value = [1 if num >= thre else 0 for num in event_value]
prec, recall_pos, tn_prec, tn_recall = self._calculate_pr(prec_value)
print(
f"thre>>{ratio:.2f}, recall>>{recall_pos:.4f}, precise_pos>>{prec:.4f}, recall_tn>>{tn_recall:.4f}, precise_neg>>{tn_prec:4f}")
PrecisePos.append(prec)
recall.append(recall_pos)
PreciseNeg.append(tn_prec)
recall_TN.append(tn_recall)
return recall, recall_TN, PrecisePos, PreciseNeg
def write_results_to_file(self, recall, recall_TN, PrecisePos, PreciseNeg, ratio):
file_path = os.sep.join(['./ckpts/tracePR', self.title_name + f"_{ratio:.2f}" + '.txt'])
with open(file_path, 'w') as file:
@ -112,5 +138,6 @@ class ShowPR:
# ratio = 0.5
if ratio < 0.1 or ratio > 0.95:
continue
recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple(ratio)
recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple_1(ratio)
# recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple_2(ratio)
self.show_pr(recall, recall_TN, PrecisePos, PreciseNeg, ratio)