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