130 lines
3.6 KiB
Python
Executable File
130 lines
3.6 KiB
Python
Executable File
import requests
|
|
import numpy as np
|
|
import traceback
|
|
import importlib.util
|
|
|
|
|
|
def load_user_function(file_path, function_name):
|
|
"""
|
|
动态加载函数。
|
|
"""
|
|
spec = importlib.util.spec_from_file_location("user_code", file_path)
|
|
user_code = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(user_code)
|
|
return getattr(user_code, function_name)
|
|
|
|
|
|
def evaluate_knn(user_func, train_data, train_labels, test_data, test_labels):
|
|
"""
|
|
使用学生编写的 KNN 函数对测试集进行分类,并计算准确率。
|
|
|
|
Args:
|
|
- user_func (function): 学生编写的 KNN 函数。
|
|
- train_data (numpy array): 训练数据集。
|
|
- train_labels (numpy array): 训练标签集。
|
|
- test_data (numpy array): 测试数据集。
|
|
- test_labels (numpy array): 测试标签集。
|
|
|
|
Returns:
|
|
- float: 分类准确率。
|
|
"""
|
|
correct = 0
|
|
total = len(test_data)
|
|
|
|
try:
|
|
for i in range(total):
|
|
# 获取测试数据中的每个样本
|
|
result = user_func(test_data[i], train_data, train_labels)
|
|
if result == test_labels[i]:
|
|
correct += 1
|
|
else:
|
|
print(
|
|
f"第 {i+1} 次测试错误,数据 {test_data[i]} 被分类到 {result} 实际是 {test_labels[i]}"
|
|
)
|
|
|
|
accuracy = correct / total
|
|
return accuracy
|
|
except Exception as e:
|
|
print(f"评测期间发生错误: {e}")
|
|
print(traceback.format_exc())
|
|
print("成绩为零!")
|
|
return 0.0
|
|
|
|
|
|
def load_data(file_path):
|
|
"""
|
|
加载 TSV 格式数据文件。
|
|
"""
|
|
features = []
|
|
labels = []
|
|
|
|
with open(file_path, "r") as file:
|
|
for line in file:
|
|
values = line.strip().split("\t")
|
|
features.append([float(x) for x in values[:-1]])
|
|
labels.append(int(values[-1]))
|
|
|
|
return np.array(features), np.array(labels)
|
|
|
|
|
|
def upload_score(name, id, score_data):
|
|
"""
|
|
上传成绩到服务器。
|
|
"""
|
|
url = "http://10.2.1.195:5000/upload_score"
|
|
try:
|
|
print("上传成绩中...")
|
|
print(score_data)
|
|
response = requests.post(
|
|
url,
|
|
json={
|
|
"id": id,
|
|
"name": name,
|
|
"score": score_data["score"],
|
|
"details": score_data["details"],
|
|
},
|
|
)
|
|
if response.status_code == 200:
|
|
print("上传成绩成功")
|
|
else:
|
|
print("上传成绩失败:", response.json())
|
|
except Exception as e:
|
|
print(f"上传成绩出错: {e}")
|
|
|
|
|
|
def main(name, id):
|
|
user_code_file = "function.py" # 学生代码文件
|
|
user_function_name = "classify0" # 学生需要实现的 KNN 函数
|
|
|
|
# 加载训练集和测试集
|
|
train_file = "datingTrainSet2.tsv"
|
|
test_file = "datingTestSet2.tsv"
|
|
|
|
train_data, train_labels = load_data(train_file)
|
|
test_data, test_labels = load_data(test_file)
|
|
|
|
# 加载学生的 KNN 函数
|
|
try:
|
|
user_func = load_user_function(user_code_file, user_function_name)
|
|
except Exception as e:
|
|
print(f"Error loading user function: {e}")
|
|
return
|
|
|
|
# 评估分类准确率
|
|
accuracy = evaluate_knn(user_func, train_data, train_labels, test_data, test_labels)
|
|
|
|
# 输出结果
|
|
print(f"得分: {accuracy * 100:.2f}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
from function import name, id
|
|
|
|
main(name, id)
|
|
except Exception as e:
|
|
print(f"评测出错: {e}")
|
|
print(traceback.format_exc())
|
|
print("请检查代码和返回值类型是否正确!")
|
|
print("本次评测无效!")
|