generated from LufsX/Experiment-LogReg
This commit is contained in:
+1
-8
@@ -28,7 +28,7 @@ name = "程悦"
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1.0 / (1 + np.exp(-z))
|
return 1.0 / (1 + np.exp(-z))
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization='l2', lambda_=0.01):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||||
"""
|
"""
|
||||||
对率回归训练函数。
|
对率回归训练函数。
|
||||||
|
|
||||||
@@ -45,13 +45,6 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization='l2',
|
|||||||
z = np.dot(X, weights)
|
z = np.dot(X, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
gradient = np.dot(X.T, (h - y)) / m
|
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
|
weights -= learning_rate * gradient
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user