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

This commit is contained in:
2024-11-13 16:17:51 +08:00
parent fbd99e7e87
commit 4c9f84d6d9
+20 -40
View File
@@ -1,41 +1,15 @@
# function.py
import numpy as np
import os
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
"""
实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
注意事项:
- 在 function.py 中实现对率回归算法。
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
测评环境:
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
- OS: Ubuntu 22.04.4 LTS
- Python: 3.10.12
- numpy: 2.1.1
"""
# 学号和姓名
id = "202210423204"
name = "陈垣豪"
import numpy as np
import os
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import joblib # 用于保存模型
def sigmoid(z):
"""计算sigmoid函数"""
return 1.0 / (1 + np.exp(-z))
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
"""计算梯度,包含L2正则化"""
z = np.dot(X_batch, weights)
@@ -44,12 +18,13 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg):
gradient += (lambda_reg / len(y_batch)) * weights # L2正则化
return gradient
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
"""逻辑回归训练函数。"""
m, n = X.shape
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
print("开始训练逻辑回归模型")
for i in range(iterations):
indices = np.random.permutation(m)
X_shuffled = X[indices]
@@ -60,23 +35,25 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam
y_batch = y_shuffled[j:j + batch_size]
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
weights -= learning_rate * gradient
if i % 1000 == 0: # 每1000次迭代打印一次信息
print(f"迭代 {i}: 当前权重形状: {weights.shape}")
return weights
def predict_logreg(inX, weights):
"""逻辑回归预测函数。"""
z = np.dot(inX, weights)
h = sigmoid(z)
return 1 if h >= 0.5 else 0
def load_data(filepath):
"""从文件中加载数据。"""
if not os.path.exists(filepath):
raise FileNotFoundError(f"文件 {filepath} 不存在!")
data, labels = [], []
print(f"加载数据中: {filepath}")
with open(filepath, 'r') as f:
@@ -89,30 +66,31 @@ def load_data(filepath):
data = np.array(data)
labels = np.array(labels)
num_features = data.shape[1]
indices_to_remove = [2]
indices_to_remove = [2]
if num_features > 23:
indices_to_remove.extend(range(23, min(28, num_features)))
data = np.delete(data, indices_to_remove, axis=1)
target_variable = np.where((labels == 2) | (labels == 3), 0, 1)
print(f"数据加载成功,数据形状: {data.shape}, 标签形状: {target_variable.shape}")
return data, target_variable
def bagging_predict(X, models):
"""集成学习预测函数。"""
predictions = np.zeros((X.shape[0], len(models)))
for i, model in enumerate(models):
predictions[:, i] = [predict_logreg(x, model) for x in X]
# 投票
final_predictions = np.round(np.mean(predictions, axis=1))
return final_predictions
def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
"""K折交叉验证。"""
m = X.shape[0]
@@ -122,7 +100,7 @@ def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, ba
for i in range(k):
val_start = i * fold_size
val_end = (i + 1) * fold_size if i != k - 1 else m
X_val = X[val_start:val_end]
y_val = y[val_start:val_end]
X_train = np.concatenate((X[:val_start], X[val_end:]))
@@ -143,11 +121,11 @@ def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, ba
return np.mean(accuracies)
if __name__ == "__main__":
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")
# 数据归一化处理
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train) # 使用训练集进行标准化
@@ -159,7 +137,6 @@ if __name__ == "__main__":
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 测试集也需要进行相同的归一化
X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化
@@ -176,3 +153,6 @@ if __name__ == "__main__":
accuracy = np.mean(y_pred == y_test)
print(f"测试集准确率: {accuracy:.4f}")
# 保存训练的模型
joblib.dump(models, "bagging_models.pkl")
print("模型已保存为 bagging_models.pkl")