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

This commit is contained in:
cy
2024-11-10 23:46:59 +08:00
parent a50cfb03af
commit f3636cd5ae
+74 -87
View File
@@ -1,6 +1,7 @@
# function.py
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import KFold
from itertools import product
"""
实验说明:
@@ -24,78 +25,86 @@ from sklearn.model_selection import KFold
id = "202210423117"
name = "程悦"
def sigmoid(z):
"""计算Sigmoid激活函数值"""
return 1.0 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64,
regularization='L2', lambda_reg=0.05, cv=5):
"""训练逻辑回归模型并使用交叉验证计算权重"""
def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regularization='L2', lambda_reg=0.05):
"""
对率回归训练函数。
Args:
- X (numpy array): 特征数据。
- y (numpy array): 标签数据。
Returns:
- numpy array: 训练得到的权重。
"""
m, n = X.shape
kf = KFold(n_splits=cv, shuffle=True)
weights_list = []
fold_scores = []
weights = np.random.randn(n) * 0.01 # 随机初始化权重
for i in range(iterations):
indices = np.random.permutation(m) # 随机打乱数据
X_shuffled = X[indices]
y_shuffled = y[indices]
for train_index, val_index in kf.split(X):
X_train, X_val = X[train_index], X[val_index]
y_train, y_val = y[train_index], y[val_index]
weights = np.random.randn(n) * 0.01
for j in range(0, m, batch_size):
X_batch = X_shuffled[j:j + batch_size]
y_batch = y_shuffled[j:j + batch_size]
z = np.dot(X_batch, weights)
h = sigmoid(z)
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
for _ in range(iterations):
indices = np.random.permutation(len(X_train))
X_shuffled, y_shuffled = X_train[indices], y_train[indices]
# 添加正则化项
if regularization == 'L1':
gradient += lambda_reg * np.sign(weights) / len(y_batch)
elif regularization == 'L2':
gradient += lambda_reg * weights / len(y_batch)
for j in range(0, len(X_train), batch_size):
X_batch = X_shuffled[j:j + batch_size]
y_batch = y_shuffled[j:j + batch_size]
z = np.dot(X_batch, weights)
h = sigmoid(z)
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
weights -= learning_rate * gradient
# 正则化处理
if regularization == 'L1':
gradient += lambda_reg * np.sign(weights) / len(y_batch)
elif regularization == 'L2':
gradient += (lambda_reg / len(y_train)) * weights
# 可选:学习率衰减
if i % 100 == 0:
learning_rate *= 0.95 # 每100次迭代衰减5%
weights -= learning_rate * gradient
if _ % 100 == 0:
learning_rate *= 0.95 # 动态调整学习率
# 验证集准确率
predictions = predict_logreg(X_val, weights)
score = np.mean(predictions == y_val)
fold_scores.append(score)
weights_list.append(weights)
# 权重加权平均
total_score = sum(fold_scores)
if total_score == 0:
return weights_list[-1]
averaged_weights = np.zeros(n)
for weight, score in zip(weights_list, fold_scores):
averaged_weights += weight * score
return averaged_weights / total_score
return weights
def predict_logreg(X, weights):
"""对输入数据进行逻辑回归预测"""
z = np.dot(X, weights)
def predict_logreg(inX, weights):
"""
对率回归预测函数。
Args:
- inX (numpy array): 输入待分类的样本。
- weights (numpy array): 训练得到的权重。
Returns:
- int: 预测的分类标签。
"""
z = np.dot(inX, weights)
h = sigmoid(z)
return (h > 0.5).astype(int)
return 1 if h > 0.5 else 0
def load_data(filepath):
"""加载数据并处理缺失值"""
data, labels = [], []
"""
从文件中加载数据,填充缺失值,并进行特征处理。
Args:
- filepath (str): 数据文件路径。
Returns:
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = []
labels = []
with open(filepath, 'r') as f:
for line in f:
lines = f.readlines()
for line in lines:
line = line.strip().split()
if line and line[-1].isdigit():
if len(line) >= 1 and line[-1].isdigit():
# 将缺失值 '?' 填充为 0,并转换为浮点数
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
data.append(line_data)
labels.append(int(line[-1]))
@@ -103,47 +112,25 @@ def load_data(filepath):
data = np.array(data)
labels = np.array(labels)
# 处理目标变量
labels[(labels == 2) | (labels == 3)] = 0
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
# 选择特征,合并目标变量
target_variable = labels
target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0
# 去掉特征3和特征24~28
data = np.delete(data, [2] + list(range(23, 28)), axis=1) # 特征3的索引是2,特征24-28的索引是23-27
# 数据标准化
scaler = StandardScaler()
data = scaler.fit_transform(data)
return data, labels
def accuracy(X, y, weights):
"""计算模型的准确率"""
predictions = predict_logreg(X, weights)
return np.mean(predictions == y)
def ensemble_predict(X, models, weights):
"""集成模型预测"""
predictions = np.array([predict_logreg(X, model) for model in models]) # (num_models, num_samples)
weighted_predictions = np.tensordot(predictions, weights, axes=(0, 0)) # 权重加权
return (weighted_predictions > 0.5).astype(int) # 返回最终预测
return data, target_variable
if __name__ == "__main__":
X_train, y_train = load_data("./data/horse-colic.data")
X_test, y_test = load_data("./data/horse-colic.test")
# 训练多个模型
num_models = 5
models = [train_logreg(X_train, y_train, learning_rate=0.02, iterations=6000,
batch_size=48, regularization='None', lambda_reg=0.02)
for _ in range(num_models)]
final_weights = train_logreg(X_train, y_train, learning_rate=0.05, iterations=1500, batch_size=64, regularization='L2', lambda_reg=0.05)
# 模型准确率作为权重
model_accuracies = [accuracy(X_train, y_train, model) for model in models]
weights = np.array(model_accuracies) / sum(model_accuracies)
# 集成预测
ensemble_predictions = ensemble_predict(X_test, models, weights)
final_accuracy = np.mean(ensemble_predictions == y_test)
print(f"最终测试集的准确率: {final_accuracy:.4f}")
# 计算并输出最终测试集的准确率
final_accuracy = accuracy(X_test, y_test, final_weights)
print(f"最终测试集的准确率: {final_accuracy:.4f}")