| 89 | return X.dot(self.theta) |
| 90 | |
| 91 | def _gradient_descent(self): |
| 92 | theta = self.theta |
| 93 | errors = [self._cost(self.X, self.y, theta)] |
| 94 | # Get derivative of the loss function |
| 95 | cost_d = grad(self._loss) |
| 96 | for i in range(1, self.max_iters + 1): |
| 97 | # Calculate gradient and update theta |
| 98 | delta = cost_d(theta) |
| 99 | theta -= self.lr * delta |
| 100 | |
| 101 | errors.append(self._cost(self.X, self.y, theta)) |
| 102 | logging.info("Iteration %s, error %s" % (i, errors[i])) |
| 103 | |
| 104 | error_diff = np.linalg.norm(errors[i - 1] - errors[i]) |
| 105 | if error_diff < self.tolerance: |
| 106 | logging.info("Convergence has reached.") |
| 107 | break |
| 108 | return theta, errors |
| 109 | |
| 110 | |
| 111 | class LinearRegression(BasicRegression): |