generated from LufsX/Experiment-LogReg
This commit is contained in:
+15
-9
@@ -114,16 +114,22 @@ if __name__ == "__main__":
|
|||||||
# 数据预处理
|
# 数据预处理
|
||||||
X, y = preprocess_data(X, y)
|
X, y = preprocess_data(X, y)
|
||||||
|
|
||||||
# 训练模型
|
# 尝试不同的正则化强度
|
||||||
weights = train_logreg(X, y, learning_rate=0.001, l2_penalty=0.01)
|
l2_penalties = [0.01, 0.1, 1.0]
|
||||||
|
|
||||||
# 加载测试数据并进行预处理
|
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
|
||||||
X_test, y_test = preprocess_data(X_test, y_test)
|
|
||||||
|
|
||||||
# 预测
|
for l2_penalty in l2_penalties:
|
||||||
predictions = predict_logreg(X_test, weights)
|
# 训练模型
|
||||||
|
weights = train_logreg(X, y, learning_rate=0.01, l2_penalty=l2_penalty)
|
||||||
|
|
||||||
# 打印预测结果和实际结果
|
# 加载测试数据并进行预处理
|
||||||
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
X_test, y_test = preprocess_data(X_test, y_test)
|
||||||
|
|
||||||
|
# 评估模型准确率
|
||||||
|
accuracy = evaluate_model(X_test, y_test, weights)
|
||||||
|
print(f"L2正则化强度: {l2_penalty}, 准确率: {accuracy:.4f}")
|
||||||
|
|
||||||
|
# 打印预测结果和实际结果(以最后一个正则化强度为例)
|
||||||
|
predictions = predict_logreg(X_test, weights)
|
||||||
for i in range(min(10, len(y_test))):
|
for i in range(min(10, len(y_test))):
|
||||||
print(f"预测结果: {predictions[i]}, 实际结果: {y_test[i]}")
|
print(f"预测结果: {predictions[i]}, 实际结果: {y_test[i]}")
|
||||||
|
|||||||
Reference in New Issue
Block a user