From 163b557baff38158e41c08c6c128561535e4b3b0 Mon Sep 17 00:00:00 2001 From: cy <939601402@qq.com> Date: Tue, 15 Oct 2024 18:49:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/function.py b/function.py index e64cd0e..5036d73 100644 --- a/function.py +++ b/function.py @@ -56,6 +56,27 @@ def classify(input_vector, dataSet, labels, k=2): sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True) return sorted_class_count[0][0] +def cross_validation(dataSet, labels, k_list=(2, 3, 5, 7, 9)): + from sklearn.model_selection import KFold + kf = KFold(n_splits=5) + best_k = None + best_accuracy = 0 + for k in k_list: + accuracies = [] + for train_index, test_index in kf.split(dataSet): + train_data, test_data = dataSet[train_index], dataSet[test_index] + train_labels, test_labels = np.array(labels)[train_index], np.array(labels)[test_index] + predictions = [] + for vector in test_data: + predicted_label = classify(vector.reshape(1, -1), train_data, train_labels, k) + predictions.append(predicted_label) + accuracy = sum([1 for p, t in zip(predictions, test_labels) if p == t]) / len(test_labels) + accuracies.append(accuracy) + avg_accuracy = sum(accuracies) / len(accuracies) + if avg_accuracy > best_accuracy: + best_accuracy = avg_accuracy + best_k = k + return best_k def img_to_vector(filename): """ @@ -100,8 +121,9 @@ def load_dataset(directory): if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 train_data, train_labels = load_dataset("trainingDigits") + best_k = cross_validation(train_data, train_labels) + print("Best k value:", best_k) test_data = ( "00000000000000110000000000000000" @@ -141,6 +163,7 @@ if __name__ == "__main__": np.array([int(char) for char in test_data]).reshape(1, 1024), train_data, train_labels, + best_k ) print(f"预测的标签: {predicted_label}") \ No newline at end of file