Skip to content

Commit

Permalink
Fixed logical error in calculating the derivatives of weights and `…
Browse files Browse the repository at this point in the history
…bias` in linear regression

Signed-off-by: Ayush Joshi <[email protected]>
  • Loading branch information
joshiayush committed Nov 22, 2023
1 parent d5d2da1 commit 050f37f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ai/linear_model/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,18 @@ def fit(self, X: np.ndarray, y: np.ndarray) -> 'LinearRegression':
`n_features` is the number of features.
y: Target vector.
"""
n_samples, _ = X.shape
self._bias = 0
self._weights = np.zeros(X.shape[1])

for _ in range(self._n_iters):
y_pred = np.dot(X, self._weights) + self._bias

bias_d = 1 / X[0] * np.sum((y_pred - y))
weights_d = 1 / X[0] * np.dot(X.T, (y_pred - y))
weights_d = (1 / n_samples) * np.dot(X.T, (y_pred - y))
bias_d = (1 / n_samples) * np.sum((y_pred - y))

self._bias = self._bias - (self._alpha * bias_d)
self._weights = self._weights - (self._alpha * weights_d)
self._bias = self._bias - (self._alpha * bias_d)

return self

Expand Down

0 comments on commit 050f37f

Please sign in to comment.