Untitled

 avatar
unknown
python
a year ago
1.2 kB
3
Indexable
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score


def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def compute_cost(X, y, weights):
    m = len(y)
    h = sigmoid(np.dot(X, weights))
    cost = -(1/m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
    return cost

def gradient_descent(X, y, weights, learning_rate, iterations):
    m = len(y)
    cost_history = []

    for i in range(iterations):
        h = sigmoid(np.dot(X, weights))
        gradient = np.dot(X.T, (h - y)) / m
        weights -= learning_rate * gradient
        cost = compute_cost(X, y, weights)
        cost_history.append(cost)

    return weights, cost_history

def predict(X, weights):
    return sigmoid(np.dot(X, weights)) >= 0.5

# Model Training Example
weights = np.zeros(X_train_scaled.shape[1])
weights, cost_history = gradient_descent(X_train_scaled, y_train, weights, learning_rate=0.01, iterations=1000)

# Model Prediction Example
y_pred = predict(X_test_scaled, weights)


accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
Editor is loading...
Leave a Comment