generated from LufsX/Experiment-LogReg
This commit is contained in:
+34
-24
@@ -1,5 +1,6 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -67,11 +68,10 @@ def train_logreg(X, y, learning_rate=0.001, iterations=1000, batch_size=32, init
|
||||
"""
|
||||
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
inX = np.insert(inX, 0, 1) # 添加偏置项
|
||||
z = np.dot(inX, weights)
|
||||
h = sigmoid(z)
|
||||
return 1 if h >= 0.5 else 0
|
||||
def evaluate_model(X_val, y_val, weights):
|
||||
predictions = np.array([predict_logreg(x, weights) for x in X_val])
|
||||
accuracy = np.mean(predictions == y_val)
|
||||
return accuracy
|
||||
|
||||
"""
|
||||
对率回归预测函数。
|
||||
@@ -118,30 +118,40 @@ def load_data(filepath):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
# 加载数据并分割为训练集和验证集
|
||||
X, y = load_data("./data/horse-colic.data")
|
||||
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
# 模型调优参数
|
||||
learning_rates = [0.01, 0.001, 0.0001]
|
||||
iterations = [500, 1000, 2000]
|
||||
batch_sizes = [16, 32, 64]
|
||||
initial_weights = np.random.randn(X_train.shape[1] + 1) # 随机初始化权重
|
||||
|
||||
best_accuracy = 0
|
||||
best_params = {}
|
||||
|
||||
# 选择一组参数进行训练
|
||||
chosen_learning_rate = learning_rates[1]
|
||||
chosen_iterations = iterations[1]
|
||||
chosen_batch_size = batch_sizes[1]
|
||||
# 网格搜索
|
||||
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}
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train,
|
||||
learning_rate=chosen_learning_rate,
|
||||
iterations=chosen_iterations,
|
||||
batch_size=chosen_batch_size,
|
||||
initial_weights=initial_weights)
|
||||
# 输出最佳参数
|
||||
print(f"Best Parameters: {best_params}")
|
||||
print(f"Best Validation Accuracy: {best_accuracy}")
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
# 使用最佳参数训练最终模型
|
||||
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}")
|
||||
Reference in New Issue
Block a user