fork download
  1. class MyLogisticRegression02:
  2.  
  3. def __init__(self, learning_rate=0.01, max_iter=10000):
  4. self.learning_rate = learning_rate
  5. self.max_iter = max_iter
  6. self.weights = None
  7. self.bias = 0
  8.  
  9. def initialize_weights(self, n_features):
  10. self.weights = np.zeros(n_features)
  11. self.bias = 0
  12.  
  13. def sigmoid(self, z):
  14. z = np.clip(z, -500, 500)
  15. return 1 / (1 + np.exp(-z))
  16.  
  17. def get_gradient(self, x, y):
  18.  
  19. m = x.shape[0]
  20. predictions = np.matmul(x, self.weights) + self.bias
  21.  
  22. errors = self.sigmoid(predictions) - y
  23.  
  24. weight_gradient = np.matmul(x.T, errors)
  25. bias_gradient = np.sum(errors)
  26.  
  27. return weight_gradient / m, bias_gradient / m
  28.  
  29.  
  30. def fit(self, x_train, y_train):
  31. rows = x_train.shape[0]
  32. cols = x_train.shape[1]
  33.  
  34. if self.weights is None:
  35. self.initialize_weights(cols)
  36.  
  37. for i in range(self.max_iter):
  38. gradient_weights, gradient_bias = self.get_gradient(x_train, y_train)
  39. self.weights = self.weights - self.learning_rate * gradient_weights
  40. self.bias = self.bias - self.learning_rate * gradient_bias
  41.  
  42. def predict(self, x_test):
  43. prediction = np.matmul(x_test, self.weights) + self.bias
  44. binary_predictions = self.sigmoid(prediction)
  45. return (binary_predictions >= 0.5).reshape(-1).astype(int)
  46.  
  47. def get_weights(self):
  48. print("Bias (intercept):", self.bias)
  49. print("Weights (coefficients):", self.weights)
Success #stdin #stdout 0.13s 14108KB
stdin
Standard input is empty
stdout
Standard output is empty