generated from LufsX/Experiment-LogReg
This commit is contained in:
+8
-1
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user