代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
def initialize_params(dims):
w = np.zeros((dims, 1))
b = 0
return w, b
def sigmoid(x):
z = 1 / (1 + np.exp(-x))
return z
def logistic(X, y, w, b):
num_train = X.shape[0]
y_hat = sigmoid(np.dot(X, w) + b)
loss = -1 / num_train * np.sum(y * np.log(y_hat) + (1-y) * np.log(1-y_hat))
cost = -1 / num_train * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
dw = np.dot(X.T, (y_hat - y)) / num_train
db = np.sum(y_hat - y) / num_train
return y_hat, cost, dw, db