Download model.py from CshardZ/app-test: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/CshardZ/app-test/resolve/main/model.py
- Command line
-
hf download hf://spaces/CshardZ/app-test/model.py
-
curl -L -o model.py https://huggingface.co/spaces/CshardZ/app-test/resolve/main/model.py
1.37 kB
| import numpy as np | |
| class LogisticRegression: | |
| def __init__(self, x, y, m, n): | |
| self.x = x | |
| self.y = y | |
| self.m = m | |
| self.n = n | |
| self.w = None | |
| self.b = None | |
| def initialize_parameters(self): | |
| self.w = np.zeros(self.n) | |
| self.b = 0 | |
| def sigmoid(self, z): | |
| return 1 / (1 + np.exp(-z)) | |
| def compute_cost(self): | |
| z = np.dot(self.x, self.w) + self.b | |
| f = self.sigmoid(z) | |
| f = np.clip(f, 1e-15, 1 - 1e-15) # prevents log(0) crash | |
| loss = (-self.y * np.log(f)) - ((1 - self.y) * np.log(1 - f)) | |
| return np.sum(loss) / self.m | |
| def compute_grads(self): | |
| z = np.dot(self.x, self.w) + self.b | |
| f = self.sigmoid(z) | |
| error = f - self.y | |
| dj_dw = np.dot(self.x.T, error) / self.m | |
| dj_db = np.sum(error) / self.m | |
| return dj_dw, dj_db | |
| def gradient_descent(self, alpha=0.1, epochs=500, cost_display_step=100): | |
| self.initialize_parameters() | |
| for i in range(1, epochs + 1): | |
| dj_dw, dj_db = self.compute_grads() | |
| self.w = self.w - (alpha * dj_dw) | |
| self.b = self.b - (alpha * dj_db) | |
| def predict(self, x_test): | |
| # FIXED: Changed self.x to x_test | |
| z = np.dot(x_test, self.w) + self.b | |
| return self.sigmoid(z) |