From 3ceac3241db1a99b9b84f6becc13471d17b7bcb8 Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Tue, 29 Oct 2024 08:39:05 +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 | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/function.py b/function.py index a6761cd..963c095 100644 --- a/function.py +++ b/function.py @@ -50,10 +50,21 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0): return weights, bias -def predict_logreg(inX, weights): +def predict_logreg(inX, weights, bias): + # 确保 inX 是一维数组 + if inX.ndim == 2 and inX.shape[1] == 1: + inX = inX.flatten() + elif inX.ndim > 1: + raise ValueError("Input data should be one-dimensional or a column vector.") + + # 计算线性输出 linear_output = np.dot(inX, weights) + bias - y_predicted = 1 if linear_output >= 0 else 0 # 简化判断条件,因为逻辑回归输出已经通过sigmoid函数处理 - return y_predicted + + # 应用sigmoid函数(如果需要的话) + # sigmoid_output = 1 / (1 + np.exp(-linear_output)) + + # 这里可以返回线性输出或sigmoid输出,取决于您的需求 + return linear_output # 这里也写自己的代码实现 def load_data(filepath):