feat: add LOW_ACCURACY_THRESHOLD to avoid long logs

This commit is contained in:
2024-10-07 00:23:24 +08:00
parent 3adf505be9
commit d7adeee86c
+9 -3
View File
@@ -6,6 +6,7 @@ import traceback
EXP_URL = "http://10.2.1.195:880" EXP_URL = "http://10.2.1.195:880"
PROJECT_NAME = "challenge_knn" PROJECT_NAME = "challenge_knn"
LOW_ACCURACY_THRESHOLD = 0.5
def load_user_function(file_path, function_name): def load_user_function(file_path, function_name):
@@ -22,10 +23,10 @@ def evaluate_knn(
user_func, train_data, train_labels, test_data, test_labels, log_details user_func, train_data, train_labels, test_data, test_labels, log_details
): ):
""" """
使用学生编写的 KNN 函数对测试集进行分类,并计算准确率。 使用编写的 KNN 函数对测试集进行分类,并计算准确率。
Args: Args:
- user_func (function): 学生编写的 KNN 函数。 - user_func (function): 编写的 KNN 函数。
- train_data (numpy array): 训练数据集。 - train_data (numpy array): 训练数据集。
- train_labels (numpy array): 训练标签集。 - train_labels (numpy array): 训练标签集。
- test_data (numpy array): 测试数据集。 - test_data (numpy array): 测试数据集。
@@ -37,6 +38,7 @@ def evaluate_knn(
""" """
correct = 0 correct = 0
total = len(test_data) total = len(test_data)
temp_log_details = []
try: try:
for i in range(total): for i in range(total):
@@ -45,7 +47,7 @@ def evaluate_knn(
if result == test_labels[i]: if result == test_labels[i]:
correct += 1 correct += 1
else: else:
log_details.append( temp_log_details.append(
f"第 {i+1} 次测试错误,数据 {test_data[i]} 被分类到 {result} 实际是 {test_labels[i]}" f"第 {i+1} 次测试错误,数据 {test_data[i]} 被分类到 {result} 实际是 {test_labels[i]}"
) )
print( print(
@@ -53,6 +55,10 @@ def evaluate_knn(
) )
accuracy = correct / total accuracy = correct / total
if accuracy >= LOW_ACCURACY_THRESHOLD:
log_details.extend(temp_log_details)
else:
log_details.append("分类准确率较低,成绩中不记录错误日志")
log_details.append(f"分类准确率: {accuracy * 100:.2f}%") log_details.append(f"分类准确率: {accuracy * 100:.2f}%")
return accuracy return accuracy
except Exception as e: except Exception as e: