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

This commit is contained in:
2024-11-03 04:54:19 +08:00
parent 4345e129ac
commit 42a136300f
+24 -65
View File
@@ -29,33 +29,21 @@ def load_data(filepath):
- X (numpy array): 包含非线性特征的特征数据
- y (numpy array): 标签数据
"""
# 读取数据,指定分隔符为空格,缺失值符号为 '?'
data = np.genfromtxt(filepath, delimiter=" ", dtype=str)
# 将 '?' 替换为 np.nan 并转换为浮点数
data[data == '?'] = np.nan
data = data.astype(float)
# 对每一列的缺失值使用列的均值填充
col_means = np.nanmean(data, axis=0)
inds = np.where(np.isnan(data))
data[inds] = np.take(col_means, inds[1])
# 分离特征和标签
X = data[:, :-1]
y = data[:, -1]
# 生成非线性特征 (平方和对数)
square_features = X ** 2
log_features = np.log1p(X) # 使用 log1p 避免 log(0)
# 将非线性特征添加到原特征集
log_features = np.log1p(X)
X = np.hstack([X, square_features, log_features])
return X, y
# 对率回归训练函数
def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_strength=0):
def train_logreg(X, y, initial_learning_rate=0.1, num_iterations=2000, regularization_strength=0.1):
"""
对率回归训练函数。
@@ -69,79 +57,50 @@ def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_s
Returns:
- weights (numpy array): 训练得到的权重。
"""
m, n = X.shape
weights = np.zeros(n)
for _ in range(num_iterations):
weights = np.random.randn(n) * 0.01 # 随机初始化权重
learning_rate = initial_learning_rate
for i in range(num_iterations):
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
gradient = (1 / m) * np.dot(X.T, (predictions - y)) + regularization_strength * weights
weights -= learning_rate * gradient
if i % 100 == 0: # 每100次迭代降低学习率
learning_rate *= 0.9
return 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_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
return 1 if probability >= 0.5 else 0
# 融合预测函数
def predict_fusion(inX, weights, clf):
"""
融合对率回归和决策树的预测结果。
Args:
- inX (numpy array): 输入待分类的样本。
- weights (numpy array): 对率回归的权重。
- clf (DecisionTreeClassifier): 训练好的决策树模型。
Returns:
- int: 最终预测的分类标签。
"""
# 对率回归预测
prediction_logreg = predict_logreg(inX, weights)
# 决策树预测
prediction_tree = clf.predict(inX.reshape(1, -1))[0]
# 融合投票
final_prediction = 1 if (prediction_logreg + prediction_tree) >= 1 else 0
return final_prediction
def predict_fusion(inX, weights, clf, alpha=0.6):
prob_logreg = 1 / (1 + np.exp(-np.dot(inX, weights)))
prob_tree = clf.predict_proba(inX.reshape(1, -1))[0, 1]
final_prob = alpha * prob_logreg + (1 - alpha) * prob_tree
return 1 if final_prob >= 0.5 else 0
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
weights = train_logreg(X_train, y_train, initial_learning_rate=0.1, num_iterations=2000)
# 训练决策树模型
clf = train_decision_tree(X_train, y_train)
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
y_pred_logreg = predict_logreg(X_test[11], weights)
y_pred_fusion = predict_fusion(X_test[11], weights, clf)
print(f"对率回归预测结果: {y_pred_logreg}, 实际结果: {y_test[11]}")
print(f"融合预测结果: {y_pred_fusion}, 实际结果: {y_test[11]}")