更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s

This commit is contained in:
2024-11-03 04:33:11 +08:00
parent d217ae1b24
commit a3730c4840
+35 -8
View File
@@ -1,5 +1,5 @@
import numpy as np
from sklearn.tree import DecisionTreeClassifier
"""
实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
@@ -16,6 +16,7 @@ import numpy as np
id = "202210423208"
name = "刘义天"
# 加载数据并添加非线性特征
def load_data(filepath):
"""
@@ -78,20 +79,46 @@ def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_s
return weights
# 使用 0.4 阈值的对率回归预测函数
def predict_logreg(inX, weights):
# 决策树训练函数
def train_decision_tree(X, y):
"""
对率回归预测函数。
训练简单的决策树模型。
Args:
- X (numpy array): 特征数据
- y (numpy array): 标签数据
Returns:
- clf (DecisionTreeClassifier): 训练好的决策树模型
"""
clf = DecisionTreeClassifier(max_depth=5, random_state=42)
clf.fit(X, y)
return clf
# 融合预测函数
def predict_fusion(inX, weights, clf):
"""
融合对率回归和决策树的预测结果。
Args:
- inX (numpy array): 输入待分类的样本。
- weights (numpy array): 训练得到的权重。
- weights (numpy array): 对率回归的权重。
- clf (DecisionTreeClassifier): 训练好的决策树模型。
Returns:
- int: 预测的分类标签。
- int: 最终预测的分类标签。
"""
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
return 1 if probability >= 0.4 else 0
# 对率回归预测
probability_logreg = 1 / (1 + np.exp(-np.dot(inX, weights)))
prediction_logreg = 1 if probability_logreg >= 0.4 else 0
# 决策树预测
prediction_tree = clf.predict(inX.reshape(1, -1))[0]
# 融合投票
final_prediction = 1 if (prediction_logreg + prediction_tree) >= 1 else 0
return final_prediction
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改