This commit is contained in:
lda committed 2025-05-30 14:52:23 +07:00
1 parent 34d7bdfb46
commit acef4d4acc
1 file changed
+101
+101
View File
@@ -0,0 +1,101 @@
import numpy as np
# Logistic (sigmoid) function
def sigmoid(z):
"""
Compute the sigmoid of z.
"""
return 1 / (1 + np.exp(-z))
# Logistic regression hypothesis
def hypothesis(X, theta):
"""
Compute the hypothesis for logistic regression.
"""
return sigmoid(np.dot(X, theta))
# Cost function for logistic regression (binary cross-entropy)
def compute_cost(X, y, theta):
"""
Compute the cost for logistic regression.
"""
m = len(y)
h = hypothesis(X, theta)
# Avoid log(0) by clipping values
h = np.clip(h, 1e-10, 1 - 1e-10)
cost = -(1 / m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
return cost
# Gradient computation for logistic regression
def compute_gradients(X, y, theta):
"""
Compute the gradients for logistic regression.
"""
m = len(y)
h = hypothesis(X, theta)
gradients = (1 / m) * np.dot(X.T, (h - y))
return gradients
# Gradient descent for logistic regression
def gradient_descent(X, y, theta, learning_rate=0.01, iterations=1000, verbose=True):
"""
Perform gradient descent to optimize theta for logistic regression.
"""
cost_history = []
for i in range(iterations):
gradients = compute_gradients(X, y, theta)
theta -= learning_rate * gradients
cost = compute_cost(X, y, theta)
cost_history.append(cost)
if verbose and (i % 10 == 0 or i == iterations - 1):
print(
f"Iteration {i+1:4d}: Cost = {cost:.6f}, Gradients = {gradients.ravel()}, Theta = {theta.ravel()}"
)
return theta, cost_history
# Main function for running logistic regression on custom data
def main(data, learning_rate=0.01, iterations=100, verbose=True):
"""
Run logistic regression on the provided data.
data: list of tuples/lists, each [feature1, feature2, label]
learning_rate: float, step size for gradient descent
iterations: int, number of iterations
verbose: bool, print progress
Returns: optimized theta
"""
# Prepare data
data = np.array(data)
X = data[:, :2].astype(float) # Features
y = data[:, 2].astype(float).reshape(-1, 1) # Labels
m = X.shape[0]
# Add bias term (column of 1s)
X_bias = np.c_[np.ones((m, 1)), X]
# Initialize theta
theta = np.zeros((X_bias.shape[1], 1))
print("Initial theta:", theta.ravel())
print("Input X (with bias):\n", X_bias)
print("Input y:\n", y.ravel())
# Run gradient descent
theta, cost_history = gradient_descent(
X_bias, y, theta, learning_rate, iterations, verbose
)
print("\nFinal theta:", theta.ravel())
return theta
# Example usage with your data
if __name__ == "__main__":
# Each row: [t1, t2, y]
data = [
[172, 68, 1],
[163, 52, 0],
[158, 50, 0],
[180, 75, 1],
]
main(data, learning_rate=1, iterations=1, verbose=True)