diff --git a/function.py b/function.py index fb0fb6d..ddd8604 100644 --- a/function.py +++ b/function.py @@ -47,6 +47,10 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000): z = np.dot(X, weights) predictions = 1 / (1 + np.exp(-z)) gradient = np.dot(X.T, (predictions - y)) / m + + # L2正则化 + gradient += (lambda_reg / m) * weights + weights -= learning_rate * gradient return weights