rvfl
7class RVFLRegressor(_RVFLBase, RegressorMixin): 8 """ 9 Random Vector Functional Link Regressor. 10 11 Fits a single-hidden-layer network with random, fixed weights by solving 12 a ridge regression problem in closed form. Supports single- and 13 multi-output regression. 14 15 Inherits all parameters from :class:`_RVFLBase`. 16 17 Examples 18 -------- 19 >>> from rvfl import RVFLRegressor 20 >>> model = RVFLRegressor(n_nodes=200, alpha=1e-2) 21 >>> model.fit(X_train, y_train) 22 RVFLRegressor(alpha=0.01, n_nodes=200) 23 >>> y_pred = model.predict(X_test) 24 """ 25 26 def fit(self, X, Y): 27 """ 28 Fit the RVFL regressor. 29 30 Parameters 31 ---------- 32 X : array-like of shape (n_samples, n_features) 33 Training inputs. 34 Y : array-like of shape (n_samples,) or (n_samples, n_targets) 35 Training targets. 1-D arrays are treated as single-output. 36 37 Returns 38 ------- 39 self : RVFLRegressor 40 Fitted estimator. 41 """ 42 Y = np.array(Y) if np.ndim(Y) == 2 else np.array(Y)[:, None] 43 self._fit_hidden(X, Y) 44 return self 45 46 def predict(self, X): 47 """ 48 Predict target values for ``X``. 49 50 Parameters 51 ---------- 52 X : array-like of shape (n_samples, n_features) 53 Input samples. 54 55 Returns 56 ------- 57 y : ndarray of shape (n_samples,) or (n_samples, n_targets) 58 Predicted values. The trailing dimension is squeezed away for 59 single-output problems. 60 """ 61 return self._predict_raw(X).squeeze()
Random Vector Functional Link Regressor.
Fits a single-hidden-layer network with random, fixed weights by solving a ridge regression problem in closed form. Supports single- and multi-output regression.
Inherits all parameters from _RVFLBase.
Examples
>>> from rvfl import RVFLRegressor
>>> model = RVFLRegressor(n_nodes=200, alpha=1e-2)
>>> model.fit(X_train, y_train)
RVFLRegressor(alpha=0.01, n_nodes=200)
>>> y_pred = model.predict(X_test)
26 def fit(self, X, Y): 27 """ 28 Fit the RVFL regressor. 29 30 Parameters 31 ---------- 32 X : array-like of shape (n_samples, n_features) 33 Training inputs. 34 Y : array-like of shape (n_samples,) or (n_samples, n_targets) 35 Training targets. 1-D arrays are treated as single-output. 36 37 Returns 38 ------- 39 self : RVFLRegressor 40 Fitted estimator. 41 """ 42 Y = np.array(Y) if np.ndim(Y) == 2 else np.array(Y)[:, None] 43 self._fit_hidden(X, Y) 44 return self
Fit the RVFL regressor.
Parameters
X : array-like of shape (n_samples, n_features) Training inputs. Y : array-like of shape (n_samples,) or (n_samples, n_targets) Training targets. 1-D arrays are treated as single-output.
Returns
self : RVFLRegressor Fitted estimator.
46 def predict(self, X): 47 """ 48 Predict target values for ``X``. 49 50 Parameters 51 ---------- 52 X : array-like of shape (n_samples, n_features) 53 Input samples. 54 55 Returns 56 ------- 57 y : ndarray of shape (n_samples,) or (n_samples, n_targets) 58 Predicted values. The trailing dimension is squeezed away for 59 single-output problems. 60 """ 61 return self._predict_raw(X).squeeze()
Predict target values for X.
Parameters
X : array-like of shape (n_samples, n_features) Input samples.
Returns
y : ndarray of shape (n_samples,) or (n_samples, n_targets) Predicted values. The trailing dimension is squeezed away for single-output problems.
8class RVFLClassifier(_RVFLBase, ClassifierMixin): 9 """ 10 Random Vector Functional Link Classifier. 11 12 Targets are one-hot encoded via :class:`sklearn.preprocessing.LabelBinarizer` 13 before solving the ridge system. Predictions are obtained by applying a 14 softmax to the raw outputs and returning the argmax class. 15 16 Supports binary and multiclass problems. The class order follows 17 ``LabelBinarizer``, so it is consistent with the order seen during 18 ``fit``. 19 20 Inherits all parameters from :class:`_RVFLBase`. 21 22 Attributes 23 ---------- 24 lb_ : LabelBinarizer 25 Fitted binarizer used to encode targets and decode predictions. 26 27 Examples 28 -------- 29 >>> from rvfl import RVFLClassifier 30 >>> clf = RVFLClassifier(n_nodes=200, activation='relu') 31 >>> clf.fit(X_train, y_train) 32 RVFLClassifier(activation='relu', n_nodes=200) 33 >>> clf.predict(X_test) 34 array([...]) 35 >>> clf.predict_proba(X_test) 36 array([[...]]) 37 """ 38 39 def __init__( 40 self, 41 n_nodes=200, 42 activation="relu", 43 scale=1.0, 44 alpha=1e-4, 45 direct_link=True, 46 random_state=42, 47 ): 48 super().__init__( 49 n_nodes=n_nodes, 50 activation=activation, 51 scale=scale, 52 alpha=alpha, 53 direct_link=direct_link, 54 random_state=random_state, 55 ) 56 self.classes_ = None 57 58 def fit(self, X, y): 59 """ 60 Fit the RVFL classifier. 61 62 Internally one-hot encodes ``y``, expands binary targets to two 63 columns so that softmax is well-defined, then delegates to 64 :meth:`_fit_hidden`. 65 66 Parameters 67 ---------- 68 X : array-like of shape (n_samples, n_features) 69 Training inputs. 70 y : array-like of shape (n_samples,) 71 Class labels. Can be integers, strings, or any type accepted 72 by :class:`sklearn.preprocessing.LabelBinarizer`. 73 74 Returns 75 ------- 76 self : RVFLClassifier 77 Fitted estimator. 78 """ 79 self.lb_ = LabelBinarizer() 80 Y = self.lb_.fit_transform(y).astype(float) 81 self.classes_ = self.lb_.classes_ 82 if Y.shape[1] == 1: # binary: expand to two columns 83 Y = np.hstack([1 - Y, Y]) 84 self._fit_hidden(X, Y) 85 return self 86 87 def predict_proba(self, X): 88 """ 89 Predict class probabilities using a softmax over the raw outputs. 90 91 The softmax is computed with the standard max-subtraction trick for 92 numerical stability. 93 94 Parameters 95 ---------- 96 X : array-like of shape (n_samples, n_features) 97 Input samples. 98 99 Returns 100 ------- 101 proba : ndarray of shape (n_samples, n_classes) 102 Estimated class probabilities. Rows sum to 1. 103 """ 104 logits = self._predict_raw(X) 105 e = np.exp(logits - logits.max(axis=1, keepdims=True)) 106 return e / e.sum(axis=1, keepdims=True) 107 108 def predict(self, X): 109 """ 110 Predict class labels for ``X``. 111 112 Parameters 113 ---------- 114 X : array-like of shape (n_samples, n_features) 115 Input samples. 116 117 Returns 118 ------- 119 labels : ndarray of shape (n_samples,) 120 Predicted class labels, in the same dtype / encoding as the 121 labels seen during ``fit``. 122 """ 123 return self.lb_.classes_[self.predict_proba(X).argmax(axis=1)]
Random Vector Functional Link Classifier.
Targets are one-hot encoded via sklearn.preprocessing.LabelBinarizer
before solving the ridge system. Predictions are obtained by applying a
softmax to the raw outputs and returning the argmax class.
Supports binary and multiclass problems. The class order follows
LabelBinarizer, so it is consistent with the order seen during
fit.
Inherits all parameters from _RVFLBase.
Attributes
lb_ : LabelBinarizer Fitted binarizer used to encode targets and decode predictions.
Examples
>>> from rvfl import RVFLClassifier
>>> clf = RVFLClassifier(n_nodes=200, activation='relu')
>>> clf.fit(X_train, y_train)
RVFLClassifier(activation='relu', n_nodes=200)
>>> clf.predict(X_test)
array([...])
>>> clf.predict_proba(X_test)
array([[...]])
58 def fit(self, X, y): 59 """ 60 Fit the RVFL classifier. 61 62 Internally one-hot encodes ``y``, expands binary targets to two 63 columns so that softmax is well-defined, then delegates to 64 :meth:`_fit_hidden`. 65 66 Parameters 67 ---------- 68 X : array-like of shape (n_samples, n_features) 69 Training inputs. 70 y : array-like of shape (n_samples,) 71 Class labels. Can be integers, strings, or any type accepted 72 by :class:`sklearn.preprocessing.LabelBinarizer`. 73 74 Returns 75 ------- 76 self : RVFLClassifier 77 Fitted estimator. 78 """ 79 self.lb_ = LabelBinarizer() 80 Y = self.lb_.fit_transform(y).astype(float) 81 self.classes_ = self.lb_.classes_ 82 if Y.shape[1] == 1: # binary: expand to two columns 83 Y = np.hstack([1 - Y, Y]) 84 self._fit_hidden(X, Y) 85 return self
Fit the RVFL classifier.
Internally one-hot encodes y, expands binary targets to two
columns so that softmax is well-defined, then delegates to
_fit_hidden().
Parameters
X : array-like of shape (n_samples, n_features)
Training inputs.
y : array-like of shape (n_samples,)
Class labels. Can be integers, strings, or any type accepted
by sklearn.preprocessing.LabelBinarizer.
Returns
self : RVFLClassifier Fitted estimator.
87 def predict_proba(self, X): 88 """ 89 Predict class probabilities using a softmax over the raw outputs. 90 91 The softmax is computed with the standard max-subtraction trick for 92 numerical stability. 93 94 Parameters 95 ---------- 96 X : array-like of shape (n_samples, n_features) 97 Input samples. 98 99 Returns 100 ------- 101 proba : ndarray of shape (n_samples, n_classes) 102 Estimated class probabilities. Rows sum to 1. 103 """ 104 logits = self._predict_raw(X) 105 e = np.exp(logits - logits.max(axis=1, keepdims=True)) 106 return e / e.sum(axis=1, keepdims=True)
Predict class probabilities using a softmax over the raw outputs.
The softmax is computed with the standard max-subtraction trick for numerical stability.
Parameters
X : array-like of shape (n_samples, n_features) Input samples.
Returns
proba : ndarray of shape (n_samples, n_classes) Estimated class probabilities. Rows sum to 1.
108 def predict(self, X): 109 """ 110 Predict class labels for ``X``. 111 112 Parameters 113 ---------- 114 X : array-like of shape (n_samples, n_features) 115 Input samples. 116 117 Returns 118 ------- 119 labels : ndarray of shape (n_samples,) 120 Predicted class labels, in the same dtype / encoding as the 121 labels seen during ``fit``. 122 """ 123 return self.lb_.classes_[self.predict_proba(X).argmax(axis=1)]
Predict class labels for X.
Parameters
X : array-like of shape (n_samples, n_features) Input samples.
Returns
labels : ndarray of shape (n_samples,)
Predicted class labels, in the same dtype / encoding as the
labels seen during fit.