This commit is contained in:
lda committed 2025-05-30 14:52:30 +07:00
1 parent acef4d4acc
commit 8239b42a21
1 file changed
+24 -15
+24 -15
View File
@@ -17,40 +17,48 @@ def hypothesis(X, theta):
return sigmoid(np.dot(X, theta)) return sigmoid(np.dot(X, theta))
# Cost function for logistic regression (binary cross-entropy) # Cost function for logistic regression (binary cross-entropy) with L2 regularization
def compute_cost(X, y, theta): def compute_cost(X, y, theta, lambda_=0.0):
""" """
Compute the cost for logistic regression. Compute the cost for logistic regression with L2 regularization.
""" """
m = len(y) m = len(y)
h = hypothesis(X, theta) h = hypothesis(X, theta)
# Avoid log(0) by clipping values # Avoid log(0) by clipping values
h = np.clip(h, 1e-10, 1 - 1e-10) h = np.clip(h, 1e-10, 1 - 1e-10)
cost = -(1 / m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h)) cost = -(1 / m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
return cost # L2 regularization (exclude bias term theta[0])
reg_term = (lambda_ / (2 * m)) * np.sum(theta[1:] ** 2)
return cost + reg_term
# Gradient computation for logistic regression # Gradient computation for logistic regression with L2 regularization
def compute_gradients(X, y, theta): def compute_gradients(X, y, theta, lambda_=0.0):
""" """
Compute the gradients for logistic regression. Compute the gradients for logistic regression with L2 regularization.
""" """
m = len(y) m = len(y)
h = hypothesis(X, theta) h = hypothesis(X, theta)
gradients = (1 / m) * np.dot(X.T, (h - y)) gradients = (1 / m) * np.dot(X.T, (h - y))
# L2 regularization (exclude bias term)
reg = (lambda_ / m) * theta
reg[0] = 0 # Do not regularize bias
gradients += reg
return gradients return gradients
# Gradient descent for logistic regression # Gradient descent for logistic regression with L2 regularization
def gradient_descent(X, y, theta, learning_rate=0.01, iterations=1000, verbose=True): def gradient_descent(
X, y, theta, learning_rate=0.01, iterations=1000, lambda_=0.0, verbose=True
):
""" """
Perform gradient descent to optimize theta for logistic regression. Perform gradient descent to optimize theta for logistic regression with L2 regularization.
""" """
cost_history = [] cost_history = []
for i in range(iterations): for i in range(iterations):
gradients = compute_gradients(X, y, theta) gradients = compute_gradients(X, y, theta, lambda_)
theta -= learning_rate * gradients theta -= learning_rate * gradients
cost = compute_cost(X, y, theta) cost = compute_cost(X, y, theta, lambda_)
cost_history.append(cost) cost_history.append(cost)
if verbose and (i % 10 == 0 or i == iterations - 1): if verbose and (i % 10 == 0 or i == iterations - 1):
print( print(
@@ -60,12 +68,13 @@ def gradient_descent(X, y, theta, learning_rate=0.01, iterations=1000, verbose=T
# Main function for running logistic regression on custom data # Main function for running logistic regression on custom data
def main(data, learning_rate=0.01, iterations=100, verbose=True): def main(data, learning_rate=0.01, iterations=100, lambda_=0.0, verbose=True):
""" """
Run logistic regression on the provided data. Run logistic regression on the provided data.
data: list of tuples/lists, each [feature1, feature2, label] data: list of tuples/lists, each [feature1, feature2, label]
learning_rate: float, step size for gradient descent learning_rate: float, step size for gradient descent
iterations: int, number of iterations iterations: int, number of iterations
lambda_: float, regularization strength
verbose: bool, print progress verbose: bool, print progress
Returns: optimized theta Returns: optimized theta
""" """
@@ -83,7 +92,7 @@ def main(data, learning_rate=0.01, iterations=100, verbose=True):
print("Input y:\n", y.ravel()) print("Input y:\n", y.ravel())
# Run gradient descent # Run gradient descent
theta, cost_history = gradient_descent( theta, cost_history = gradient_descent(
X_bias, y, theta, learning_rate, iterations, verbose X_bias, y, theta, learning_rate, iterations, lambda_, verbose
) )
print("\nFinal theta:", theta.ravel()) print("\nFinal theta:", theta.ravel())
return theta return theta
@@ -98,4 +107,4 @@ if __name__ == "__main__":
[158, 50, 0], [158, 50, 0],
[180, 75, 1], [180, 75, 1],
] ]
main(data, learning_rate=1, iterations=1, verbose=True) main(data, learning_rate=1, iterations=1, lambda_=0.1, verbose=True)