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

This commit is contained in:
2024-10-08 00:24:06 +08:00
parent 3258fe3ba6
commit 39e07036da
+2 -3
View File
@@ -59,7 +59,7 @@ 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=1):
def classify0(inX, dataSet, labels, k):
"""
调用 knn_classify 函数进行分类。
"""
@@ -84,7 +84,7 @@ def main():
# 特征缩放
scaler = StandardScaler() # 创建标准化对象
train_data = scaler.fit_transform(train_data) # 对训练数据进行标准化
k=1
# 手动实现 KNN 分类计算准确率
correct_predictions = 0
for i in range(len(train_data)):
@@ -100,7 +100,6 @@ 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]) # 对测试样本进行相同的缩放