Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. def logistic_regression(X, y, eta=0.001, iter=100, alpha=0.5):
  2.     """Logistic regression
  3. Args:
  4.    X - feature matrix
  5.    y - target vector
  6.    eta - learning rate
  7.    iter - number of iterations
  8.    alpha - regularization parameter
  9. Returns:
  10.    theta - weight estimates
  11.    losses - historical loss data
  12.    thetas - historical thetas data
  13. """
  14.     _, n = X.shape
  15.     theta = np.random.uniform(size=n)
  16.     losses = np.zeros(iter)
  17.     thetas = np.zeros(iter*3).reshape(iter, 3)
  18.     #### your code (start)
  19.     for i in range(iter):
  20.         output = sigmoid(X @ theta)
  21.  
  22.         loss = binary_cross_entropy(output, y) + alpha / 2 * np.sum(theta[1:] ** 2)
  23.         if i % 10 == 0:
  24.             print(f'\r{loss}', end='')
  25.  
  26.         grad = (y - output) @ X
  27.         grad[1:] += alpha * theta[1:] # regularization term
  28.  
  29.         theta += eta * grad
  30.  
  31.         losses[i] = loss
  32.         thetas[i] = theta.copy()
  33.        
  34.     #### your code (end)
  35.     return theta, losses, thetas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement