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

This commit is contained in:
2024-10-31 15:03:13 +08:00
parent 167541c297
commit 81b134cc1a
+15 -3
View File
@@ -32,14 +32,14 @@ def sigmoid(z):
"""计算sigmoid函数"""
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
def train_logreg(X, y, learning_rate=0.01, iterations=1000,l2_penalty=0.01):
"""对率回归训练函数。"""
weights = np.zeros(X.shape[1])
for _ in range(iterations):
z = np.dot(X, weights)
predictions = sigmoid(z)
gradient = np.dot(X.T, (predictions - y)) / y.size
gradient = np.dot(X.T, (predictions - y)) / y.size + l2_penalty * weights # 添加L2正则化项
weights -= learning_rate * gradient
return weights
@@ -84,6 +84,18 @@ def load_data(filepath):
return data_array[:, :-1], data_array[:, -1]
def preprocess_data(X, y):
"""数据预处理,合并特征23的值,并删除特征3和特征24~28,以及特征缩放。"""
# 合并特征23的值和删除特征
X, y = preprocess_data_base(X, y)
# 特征缩放:标准化特征
X_mean = np.mean(X, axis=0)
X_std = np.std(X, axis=0)
X = (X - X_mean) / X_std
return X, y
def preprocess_data_base(X, y):
"""数据预处理,合并特征23的值,并删除特征3和特征24~28。"""
# 合并特征23的值:将2(死亡)和3(安乐死)合并为0(死亡)
y = np.where(y == 2, 0, y) # 将值2替换为0
@@ -102,7 +114,7 @@ if __name__ == "__main__":
X, y = preprocess_data(X, y)
# 训练模型
weights = train_logreg(X, y)
weights = train_logreg(X, y, learning_rate=0.01, l2_penalty=0.01)
# 加载测试数据并进行预处理
X_test, y_test = load_data("./data/horse-colic.test")