更新 function.py
Gitea Actions Challenge Test / Challenge-Actions (push) Successful in 24s

This commit is contained in:
2024-10-08 00:31:24 +08:00
parent 39e07036da
commit 39d9aff1f5
+6 -8
View File
@@ -44,7 +44,7 @@ name = "陈宇杰"
# 这里写自己的代码实现
def knn_classify(inX, dataSet, labels, k):
def classify0(inX, dataSet, labels, k):
"""
KNN 分类函数
:param inX: 待分类的样本
@@ -59,12 +59,6 @@ def knn_classify(inX, dataSet, labels, k):
predicted_label = Counter(nearest_labels).most_common(1)[0][0] # 多数投票
return predicted_label
def classify0(inX, dataSet, labels, k):
"""
调用 knn_classify 函数进行分类。
"""
return knn_classify(inX, dataSet, labels, k)
def load_data(file_path):
"""
加载 TSV 格式数据文件
@@ -84,7 +78,10 @@ def main():
# 特征缩放
scaler = StandardScaler() # 创建标准化对象
train_data = scaler.fit_transform(train_data) # 对训练数据进行标准化
k=1
# 设置 K 值
k = 1 # 直接定义 K 值为 1
# 手动实现 KNN 分类计算准确率
correct_predictions = 0
for i in range(len(train_data)):
@@ -100,6 +97,7 @@ def main():
accuracy = correct_predictions / len(train_data) # 计算准确性
print(f"使用 K 值 {k} 的准确率: {accuracy:.4f}")
# 测试分类
test_sample = [65915, 3.868359, 0.128078] # 测试样本
test_sample = scaler.transform([test_sample]) # 对测试样本进行相同的缩放