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

This commit is contained in:
2024-10-22 19:47:22 +08:00
parent 3d31271936
commit 4edfffa687
+5 -5
View File
@@ -6,7 +6,7 @@ import time
id = "202210423220"
name = "方子涵"
def classify(input_vector, dataSet, labels, k=4):
def classify(input_vector, dataSet, labels, k=5):
"""
KNN 分类算法。
@@ -14,7 +14,7 @@ def classify(input_vector, dataSet, labels, k=4):
- input_vector (numpy array): 输入待分类的样本。
- dataSet (numpy array): 训练数据集。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,默认值为 4。
- k (int): kNN 算法参数,默认值为 5。
Returns:
- int: 预测的分类标签。
@@ -127,8 +127,8 @@ if __name__ == "__main__":
)
test_data_vector = np.array([int(char) for char in test_data]).reshape(1, 1024)
# 使用k=4进行分类
# 使用k=5进行分类
start_time = time.time()
predicted_label = classify(test_data_vector, train_data, train_labels, k=4)
predicted_label = classify(test_data_vector, train_data, train_labels, k=5)
end_time = time.time()
print(f"k = 4, 预测的标签: {predicted_label}, 执行时间: {end_time - start_time:.2f} 秒")
print(f"k = 5, 预测的标签: {predicted_label}, 执行时间: {end_time - start_time:.2f} 秒")