generated from LufsX/Experiment-LogReg
This commit is contained in:
+24
@@ -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}")
|
||||
Reference in New Issue
Block a user