sy2
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s

This commit is contained in:
2024-10-26 18:36:13 +08:00
parent 2cc63b3cf9
commit 316a3944b0
+10 -4
View File
@@ -25,14 +25,17 @@ name = "孔祥芷"
# 这里写自己的代码实现
def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128):
def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01):
"""
对率回归训练函数。
对率回归训练函数,加入L2正则化。
Args:
- X (numpy array): 特征数据。
- y (numpy array): 标签数据。
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
- numIter (int): 迭代次数。默认为1000。
- learning_rate (float): 学习率。默认为0.06。
- batch_size (int): 批量大小。默认为128。
- lambda_reg (float): L2正则化参数。默认为0.01。
Returns:
- numpy array: 训练得到的权重。
@@ -54,6 +57,9 @@ def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128):
h = 1 / (1 + np.exp(-z)) # 计算h
error = batch_y - h # 计算误差
gradient = np.dot(batch_X.T, error) / len(batch_y) # 计算梯度
# 添加L2正则化
reg_term = 2 * lambda_reg * weights
gradient += reg_term / len(batch_y)
weights += learning_rate * gradient # 更新权重
return weights
@@ -135,7 +141,7 @@ if __name__ == "__main__":
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")'''
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, numIter=1000, learning_rate=0.01)
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01,lambda_reg=0.2)
error=0
for i in range(len(y_test)):
y_pred = predict_logreg(X_test[i], weights)