-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLM.py
More file actions
56 lines (43 loc) · 2 KB
/
GLM.py
File metadata and controls
56 lines (43 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
class UnifiedGLM:
"""
A unified linear solver mapping directly to the (H, L, Omega, A) framework.
Solves Perceptron, Adaline, and Logistic Regression.
"""
ACTIVATIONS = {
'perceptron': lambda z: np.where(z >= 0.0, 1.0, 0.0), # Step Function
'adaline': lambda z: z, # Identity (MSE)
'logistic': lambda z: 1.0 / (1.0 + np.exp(-np.clip(z, -250, 250))), # Sigmoid (Log-Loss)
}
def __init__(self, model_type='logistic', eta=0.1, epochs=1000):
if model_type not in self.ACTIVATIONS:
raise ValueError(f"Model must be one of {list(self.ACTIVATIONS.keys())}")
self.model_type = model_type
self.activation_fn = self.ACTIVATIONS[model_type]
self.eta = eta # Learning rate
self.epochs = epochs
self.w = None # Weights
self.b = None # Bias
def fit(self, X, y):
"""The Solver (A): Batch Gradient Descent"""
n_samples, n_features = X.shape
self.w = np.zeros(n_features)
self.b = 0.0
for _ in range(self.epochs):
# Hypothesis (H): Global linear assumption
z = X @ self.w + self.b
# Apply the activation function
y_hat = self.activation_fn(z)
# Compute gradients
error = y_hat - y
grad_w = (X.T @ error) / n_samples
grad_b = np.sum(error, axis=0) / n_samples
self.w -= self.eta * grad_w
self.b -= self.eta * grad_b
def predict(self, X):
"""The Decision Rule d(y)."""
z = X @ self.w + self.b
if self.model_type == 'adaline':
return z # Return continuous score for regression
# Binary Classification rule for Perceptron/Logistic
return np.where(z >= 0.0, 1, 0)