generated from LufsX/Experiment-LogReg
This commit is contained in:
+11
-35
@@ -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}")
|
||||
|
||||
# 训练对率回归模型
|
||||
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]}")
|
||||
Reference in New Issue
Block a user