generated from LufsX/Experiment-LogReg
This commit is contained in:
+34
-24
@@ -1,5 +1,6 @@
|
|||||||
# function.py
|
# function.py
|
||||||
import numpy as np
|
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):
|
def evaluate_model(X_val, y_val, weights):
|
||||||
inX = np.insert(inX, 0, 1) # 添加偏置项
|
predictions = np.array([predict_logreg(x, weights) for x in X_val])
|
||||||
z = np.dot(inX, weights)
|
accuracy = np.mean(predictions == y_val)
|
||||||
h = sigmoid(z)
|
return accuracy
|
||||||
return 1 if h >= 0.5 else 0
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
对率回归预测函数。
|
对率回归预测函数。
|
||||||
@@ -118,30 +118,40 @@ def load_data(filepath):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载训练集
|
# 加载数据并分割为训练集和验证集
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
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)
|
||||||
# 加载测试集
|
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
|
||||||
|
|
||||||
# 模型调优参数
|
# 模型调优参数
|
||||||
learning_rates = [0.01, 0.001, 0.0001]
|
learning_rates = [0.01, 0.001, 0.0001]
|
||||||
iterations = [500, 1000, 2000]
|
iterations = [500, 1000, 2000]
|
||||||
batch_sizes = [16, 32, 64]
|
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]
|
for lr in learning_rates:
|
||||||
chosen_iterations = iterations[1]
|
for it in iterations:
|
||||||
chosen_batch_size = batch_sizes[1]
|
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,
|
print(f"Best Parameters: {best_params}")
|
||||||
learning_rate=chosen_learning_rate,
|
print(f"Best Validation Accuracy: {best_accuracy}")
|
||||||
iterations=chosen_iterations,
|
|
||||||
batch_size=chosen_batch_size,
|
|
||||||
initial_weights=initial_weights)
|
|
||||||
|
|
||||||
# 测试单个数据
|
# 使用最佳参数训练最终模型
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
final_weights = train_logreg(X_train, y_train, **best_params)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
|
||||||
|
# 在测试集上评估最终模型
|
||||||
|
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