From 288e0bdd99339aaef4b276a2195d9a6a08bb2292 Mon Sep 17 00:00:00 2001 From: Xionghaoran <719929980@qq.com> Date: Thu, 31 Oct 2024 17:57:04 +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 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/function.py b/function.py index feccd54..68a5fff 100644 --- a/function.py +++ b/function.py @@ -25,3 +25,27 @@ def load_data(filepath): scaler = StandardScaler() data = scaler.fit_transform(data) return data, labels + +# 测试代码 +if __name__ == "__main__": + # 指定数据文件路径 + filepath = "./data/horse-colic.data" + + # 加载数据 + X, y = load_data(filepath) + print("数据加载成功。") + + # 训练模型 + model = train_logreg(X, y) + print("模型训练完成。") + + # 随机选择一个样本进行预测测试 + sample = X[0] + actual_label = y[0] + predicted_label = predict_logreg(sample, model) + print(f"预测结果: {predicted_label}, 实际结果: {actual_label}") + + # 计算测试集上的准确率 + correct_predictions = sum(predict_logreg(X[i], model) == y[i] for i in range(len(y))) + accuracy = correct_predictions / len(y) + print(f"测试集准确率: {accuracy:.2f}") \ No newline at end of file