Skip to content

Commit

Permalink
update rls.py
Browse files Browse the repository at this point in the history
test precommit
  • Loading branch information
W0lfgunbl00d committed Nov 17, 2024
1 parent dd269a7 commit 0953c5e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
4 changes: 2 additions & 2 deletions river/linear_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from . import base
from .alma import ALMAClassifier
from .bayesian_lin_reg import BayesianLinearRegression
from .incrementalAUC import IncrementalAUC
from .lin_reg import LinearRegression
from .log_reg import LogisticRegression
from .pa import PAClassifier, PARegressor
from .perceptron import Perceptron
from .softmax import SoftmaxRegression
from .rls import RLS
from .incrementalAUC import IncrementalAUC
from .softmax import SoftmaxRegression

__all__ = [
"base",
Expand Down
18 changes: 13 additions & 5 deletions river/linear_model/incrementalAUC.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from river import metrics
from __future__ import annotations

import numpy as np

from river import metrics


class IncrementalAUC(metrics.base.BinaryMetric):
"""Calculates AUC incrementally."""

Expand All @@ -17,7 +21,9 @@ def update(self, y_true, y_pred):
self.negative_scores.append(y_pred)
return self

def get(self, X_train, y_train, X_test, y_test, epochs=900, lr=0.5, n_mc=500, gamma=1e-4, eps=0.01):
def get(
self, X_train, y_train, X_test, y_test, epochs=900, lr=0.5, n_mc=500, gamma=1e-4, eps=0.01
):
"""
Implements the stochastic gradient ascent method to optimize theta and computes the AUC
based on the accumulated scores.
Expand All @@ -37,6 +43,7 @@ def get(self, X_train, y_train, X_test, y_test, epochs=900, lr=0.5, n_mc=500, ga
- auc: Final AUC score based on the accumulated scores.
"""
from sklearn.metrics import roc_auc_score

# Separate the classes
X1 = X_train[y_train == 1]
X0 = X_train[y_train == 0]
Expand All @@ -56,7 +63,9 @@ def get(self, X_train, y_train, X_test, y_test, epochs=900, lr=0.5, n_mc=500, ga
current_lr = current_lr / (1 + gamma)

# Update theta using stochastic gradient ascent
theta -= current_lr * self.stochastic_gradient(theta, X1, X0, N=n_mc, eps=eps, random_state=seed)
theta -= current_lr * self.stochastic_gradient(
theta, X1, X0, N=n_mc, eps=eps, random_state=seed
)

# After training, compute the scores on the test set
y_scores = np.dot(X_test, theta)
Expand All @@ -79,7 +88,7 @@ def sigma_eps(self, x, eps=0.01):
elif z < -35:
return 0
else:
return 1.0 / (1.0 + np.exp(- z))
return 1.0 / (1.0 + np.exp(-z))

def reg_u_statistic(self, y_true, y_probs, eps=0.01):
p = y_probs[y_true == 1]
Expand All @@ -94,7 +103,6 @@ def reg_u_statistic(self, y_true, y_probs, eps=0.01):
return u

def stochastic_gradient(self, theta, X1, X0, N=1000, eps=0.01, random_state=1):

np.random.seed(random_state)

indices_1 = np.random.choice(np.arange(X1.shape[0]), size=N)
Expand Down
53 changes: 28 additions & 25 deletions river/linear_model/rls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numpy as np


Expand All @@ -13,7 +15,7 @@ class RLS:
----------
p : int
The order of the filter (number of coefficients to be estimated).
l : float, optional, default=0.99
forgetting_factor : float, optional, default=0.99
Forgetting factor (0 < l ≤ 1). Controls how quickly the algorithm forgets past data.
A smaller value makes the algorithm more responsive to recent data.
delta : float, optional, default=1000000
Expand Down Expand Up @@ -65,18 +67,19 @@ class RLS:
>>> print("Final Weights:", rls.estimates[-1].flatten())
Final Weights: [ 3.48065382 -6.15301727 3.3361416 ]
"""

def __init__(self, p: int, forgetting_factor=0.99, delta=1000000):
"""
Initializes the Recursive Least Squares (RLS) filter.
Parameters
----------
p : int
Filter order (number of coefficients).
forgetting_factor : float, optional
Forgetting factor (default is 0.99).
delta : float, optional
Initial value for the inverse correlation matrix (default is 1,000,000).
Initializes the Recursive Least Squares (RLS) filter.
Parameters
----------
p : int
Filter order (number of coefficients).
forgetting_factor : float, optional
Forgetting factor (default is 0.99).
delta : float, optional
Initial value for the inverse correlation matrix (default is 1,000,000).
"""
self.p = p # Filter order
self.forgetting_factor = forgetting_factor # Forgetting factor
Expand All @@ -95,20 +98,20 @@ def __init__(self, p: int, forgetting_factor=0.99, delta=1000000):

def estimate(self, xn: float, dn: float):
"""
Performs one iteration of the RLS algorithm to update filter coefficients.
Parameters
----------
xn : float
The current input sample.
dn : float
The desired output corresponding to the current input.
Returns
-------
numpy.ndarray
Updated weight vector (filter coefficients) after the current iteration.
"""
Performs one iteration of the RLS algorithm to update filter coefficients.
Parameters
----------
xn : float
The current input sample.
dn : float
The desired output corresponding to the current input.
Returns
-------
numpy.ndarray
Updated weight vector (filter coefficients) after the current iteration.
"""
# Update input vector
self.x = np.roll(self.x, -1)
self.x[-1, 0] = xn
Expand Down

0 comments on commit 0953c5e

Please sign in to comment.