diff --git a/river/linear_model/__init__.py b/river/linear_model/__init__.py index dcf20ffb12..581113dbcb 100644 --- a/river/linear_model/__init__.py +++ b/river/linear_model/__init__.py @@ -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", diff --git a/river/linear_model/incrementalAUC.py b/river/linear_model/incrementalAUC.py index f2fc9eadc7..5b89911d58 100644 --- a/river/linear_model/incrementalAUC.py +++ b/river/linear_model/incrementalAUC.py @@ -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.""" @@ -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. @@ -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] @@ -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) @@ -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] @@ -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) diff --git a/river/linear_model/rls.py b/river/linear_model/rls.py index 6150baa6cc..fbdc40e54f 100644 --- a/river/linear_model/rls.py +++ b/river/linear_model/rls.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import numpy as np @@ -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 @@ -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 @@ -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