From 915bdbbaf7bd6014f4d69d691995bc3526e43d20 Mon Sep 17 00:00:00 2001 From: wzy <675163717@qq.com> Date: Sat, 26 Oct 2024 18:52:09 +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 | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/function.py b/function.py index 8745732..b23487d 100644 --- a/function.py +++ b/function.py @@ -141,40 +141,16 @@ def load_data(filepath): """ if __name__ == "__main__": - # 加载数据并分割为训练集和验证集 - X, y = load_data("./data/horse-colic.data") - X_train, X_val, y_train, y_val = split_train_val(X, y) + # 以下代码可用于测试,可自行随意修改 + # 加载训练集 + X_train, y_train = load_data("./data/horse-colic.data") - # 模型调优参数 - learning_rates = [0.01, 0.001, 0.0001] - iterations = [500, 1000, 2000] - batch_sizes = [16, 32, 64] - - best_accuracy = 0 - best_params = {} - - # 网格搜索 - for lr in learning_rates: - for it in iterations: - for bs in batch_sizes: - initial_weights = np.random.randn(X_train.shape[1] + 1) - weights = train_logreg(X_train, y_train, lr, it, bs, initial_weights) - accuracy = evaluate_model(X_val, y_val, weights) - - print(f"Learning Rate: {lr}, Iterations: {it}, Batch Size: {bs}, Accuracy: {accuracy}") - - if accuracy > best_accuracy: - best_accuracy = accuracy - best_params = {'learning_rate': lr, 'iterations': it, 'batch_size': bs} - - # 输出最佳参数 - print(f"Best Parameters: {best_params}") - print(f"Best Validation Accuracy: {best_accuracy}") - - # 使用最佳参数训练最终模型 - final_weights = train_logreg(X_train, y_train, **best_params) - - # 在测试集上评估最终模型 + # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") - test_accuracy = evaluate_model(X_test, y_test, final_weights) - print(f"Test Set Accuracy: {test_accuracy}") \ No newline at end of file + + # 训练对率回归模型 + weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + + # 测试单个数据 + y_pred = predict_logreg(X_test[11], weights) + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file