From 6922a8e6cb3370a13b08a3ade0acd54daf64193a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Berk=20Ba=C5=9Fo=C4=9Flu?= Date: Mon, 22 Sep 2025 18:01:29 -0400 Subject: [PATCH] Added weight report and more comments. --- linear-regression-parkinsons.py | 4 ++++ logistic-regression-wdbc.py | 8 ++++++-- mini-batch-sgd-linear-regression-parkinsons.py | 8 ++++++-- mini-batch-sgd-logistic-regression-wdbc.py | 8 ++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/linear-regression-parkinsons.py b/linear-regression-parkinsons.py index 8759023..95aaf46 100644 --- a/linear-regression-parkinsons.py +++ b/linear-regression-parkinsons.py @@ -164,3 +164,7 @@ if __name__ == "__main__": preds = model.predict(x_test) print("\nFirst 10 predictions:") print(preds.head(10)) + + # weight report + print("\nWeights from the model:") + print(model.w) \ No newline at end of file diff --git a/logistic-regression-wdbc.py b/logistic-regression-wdbc.py index 3e6b84f..75e9f6f 100644 --- a/logistic-regression-wdbc.py +++ b/logistic-regression-wdbc.py @@ -74,7 +74,7 @@ class LogisticRegression: z = self.x.dot(self.w) # linear prediction p = self.sigmoid(z) # probabilities of the model predictions - gradient = self.x.T.dot(p - self.y) / self.y.size # gradient calculation formula + gradient = self.x.T.dot(p - self.y) / self.y.size # for logistic regression X^T*(p - y) self.w -= self.lr * gradient # gradient multiplied by learning rate is removed from weight @@ -190,4 +190,8 @@ if __name__ == "__main__": # predict Y values using the trained data first_10 = X_test[:10] y_hat = model.predict(first_10) - print("\nFirst 10 predictions:", y_hat.ravel()) \ No newline at end of file + print("\nFirst 10 predictions:", y_hat.ravel()) + + # weight report + print("\nWeights from the model:") + print(model.w) \ No newline at end of file diff --git a/mini-batch-sgd-linear-regression-parkinsons.py b/mini-batch-sgd-linear-regression-parkinsons.py index a7d3164..31370bc 100644 --- a/mini-batch-sgd-linear-regression-parkinsons.py +++ b/mini-batch-sgd-linear-regression-parkinsons.py @@ -74,7 +74,7 @@ class LinearRegression: # makes Y prediction value for X batch value by multiplying X and weight vectors. error = y_batch - y_pred # error is difference between Y batch value and Y prediction value - grad = -2 * x_batch.T.dot(error) / batch_size + grad = -2 * x_batch.T.dot(error) / batch_size # for linear regression -2*X^T*(error) # gradient is calculated by multiplication of error, transposed X batch value and -2 divided by batch size w_np -= self.lr * grad # weight is decreased by multiplication of learning rate and gradient @@ -169,4 +169,8 @@ if __name__ == "__main__": # predict Y values using the trained data preds = model.predict(x_test) print("\nFirst 10 predictions:") - print(preds.head(10)) \ No newline at end of file + print(preds.head(10)) + + # weight report + print("\nWeights from the model:") + print(model.w) \ No newline at end of file diff --git a/mini-batch-sgd-logistic-regression-wdbc.py b/mini-batch-sgd-logistic-regression-wdbc.py index ef71567..e132b8e 100644 --- a/mini-batch-sgd-logistic-regression-wdbc.py +++ b/mini-batch-sgd-logistic-regression-wdbc.py @@ -30,7 +30,7 @@ class LogisticRegression: """Cross‑entropy loss is used for the cost calculation""" eps = 1e-15 p = np.clip(p, eps, 1 - eps) - return -np.mean(y * np.log(p) + (1 - y) * np.log(1 - p)) + return -np.mean(y * np.log(p) + (1 - y) * np.log(1 - p)) # mean of -[y*log(p) + (1 - y)*log(1-p)] def prepare(self, df: pd.DataFrame, target_col: str) -> None: """ @@ -90,7 +90,7 @@ class LogisticRegression: z = x_batch.dot(self.w) # linear prediction p = self.sigmoid(z) # probabilities of the model predictions - grad = x_batch.T.dot(p - y_batch) / y_batch.size # gradient calculation formula + grad = x_batch.T.dot(p - y_batch) / y_batch.size # for logistic regression X^T*(p - y) self.w -= self.lr * grad # gradient multiplied by learning rate is removed from weight # cost is calculated through cross‑entropy and added for the current range @@ -207,3 +207,7 @@ if __name__ == "__main__": first_10 = X_test[:10] y_hat = model.predict(first_10) print("\nFirst 10 predictions:", y_hat.ravel()) + + # weight report + print("\nWeights from the model:") + print(model.w)