fork download
  1. class MyLogisticRegression:
  2.  
  3. def __init__(self, learning_rate=0.01, max_iter=1000):
  4. self.learning_rate = learning_rate
  5. self.max_iter = max_iter
  6. self.weights = None
  7. self.bias = 0
  8. self.all_weights = []
  9. self.all_biases = []
  10. self.unique_classes = 0
  11.  
  12. def initialize_weights(self, n_features):
  13. self.weights = np.zeros(n_features)
  14. self.bias = 0
  15.  
  16. def sigmoid(self, z):
  17. z = np.clip(z, -500, 500)
  18. return 1 / (1 + np.exp(-z))
  19.  
  20. def get_gradient(self, x, y):
  21. m = x.shape[0]
  22. predictions = x @ self.weights + self.bias
  23. errors = self.sigmoid(predictions) - y
  24. weight_gradient = x.T @ errors
  25. bias_gradient = np.sum(errors)
  26. return weight_gradient / m, bias_gradient / m
  27.  
  28.  
  29. def binary_fit(self, x_train, y_train):
  30. rows = x_train.shape[0]
  31. cols = x_train.shape[1]
  32.  
  33. if self.weights is None:
  34. self.initialize_weights(cols)
  35.  
  36. for i in range(self.max_iter):
  37. gradient_weights, gradient_bias = self.get_gradient(x_train, y_train)
  38. self.weights = self.weights - self.learning_rate * gradient_weights
  39. self.bias = self.bias - self.learning_rate * gradient_bias
  40. return self.weights, self.bias
  41.  
  42. def fit(self, x_train, y_train):
  43.  
  44. self.unique_classes = len(np.unique(y_train))
  45.  
  46. for i in range(self.unique_classes):
  47. new_y = (y_train == i).astype(int)
  48. self.weights = None
  49. weights, bias = self.binary_fit(x_train, new_y)
  50. self.all_weights.append(weights)
  51. self.all_biases.append(bias)
  52.  
  53. def predict_single_class(self, x_test, weights, bias):
  54. prediction = np.dot(x_test, weights) + bias
  55. binary_predictions = self.sigmoid(prediction)
  56. return binary_predictions
  57.  
  58. def predict(self, x_test):
  59. predict_prob = np.zeros((x_test.shape[0], self.unique_classes))
  60. for i in range(self.unique_classes):
  61. predict_prob[:, i] = self.predict_single_class(x_test, self.all_weights[i], self.all_biases[i])
  62. return np.argmax(predict_prob, axis=1)
Success #stdin #stdout 0.09s 14168KB
stdin
Standard input is empty
stdout
Standard output is empty