From 4a7ff9c81511912f0e770ee867441bcd3aa7cdef Mon Sep 17 00:00:00 2001 From: lzh <2233673261@qq.com> Date: Sat, 2 Nov 2024 20:33:20 +0800 Subject: [PATCH] 3 --- function.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/function.py b/function.py index b8a77c2..d2284cc 100644 --- a/function.py +++ b/function.py @@ -101,8 +101,12 @@ def predict_logreg(inX, weights): Returns: - int: 预测的分类标签。 """ + # 确保输入样本的特征数量与训练时一致 + if inX.shape[0] != weights.shape[0] - 1: + raise ValueError("输入样本的特征数量与训练时不一致") + # 添加偏置项 - inX = np.c_[np.ones((1, 1)), inX] + inX = np.insert(inX, 0, 1, axis=1) # 在特征向量的开头插入偏置项 z = np.dot(inX, weights) y_pred = 1 / (1 + np.exp(-z)) return 1 if y_pred > 0.5 else 0