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

This commit is contained in:
cy
2024-10-26 17:45:17 +08:00
parent a4a31b88d9
commit 2409dbf8d2
+8 -1
View File
@@ -28,7 +28,7 @@ name = "程悦"
def sigmoid(z):
return 1.0 / (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, regularization='l2', lambda_=0.01):
"""
对率回归训练函数。
@@ -45,6 +45,13 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
z = np.dot(X, weights)
h = sigmoid(z)
gradient = np.dot(X.T, (h - y)) / m
# 正则化
if regularization == 'l2':
gradient += lambda_ * weights / m
elif regularization == 'l1':
gradient += lambda_ * np.sign(weights) / m
weights -= learning_rate * gradient
return weights