From 3a1b0746a2e2a8ec987d17cc9d096817c6401c3e Mon Sep 17 00:00:00 2001 From: mogu-king <1547976000@qq.com> Date: Sun, 3 Nov 2024 04:39:55 +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 | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/function.py b/function.py index 8182d90..1d98e5c 100644 --- a/function.py +++ b/function.py @@ -17,6 +17,7 @@ id = "202210423208" name = "刘义天" + # 加载数据并添加非线性特征 def load_data(filepath): """ @@ -96,6 +97,22 @@ def train_decision_tree(X, y): return clf +# 对率回归预测函数 +def predict_logreg(inX, weights): + """ + 对率回归预测函数。 + + Args: + - inX (numpy array): 输入待分类的样本。 + - weights (numpy array): 训练得到的权重。 + + Returns: + - int: 预测的分类标签。 + """ + probability = 1 / (1 + np.exp(-np.dot(inX, weights))) + return 1 if probability >= 0.4 else 0 + + # 融合预测函数 def predict_fusion(inX, weights, clf): """ @@ -110,8 +127,7 @@ def predict_fusion(inX, weights, clf): - int: 最终预测的分类标签。 """ # 对率回归预测 - probability_logreg = 1 / (1 + np.exp(-np.dot(inX, weights))) - prediction_logreg = 1 if probability_logreg >= 0.4 else 0 + prediction_logreg = predict_logreg(inX, weights) # 决策树预测 prediction_tree = clf.predict(inX.reshape(1, -1))[0]