Skip to content

ESNRegressor

Bases: ESNBase, MultiOutputMixin, RegressorMixin

Number of input and output neurons are infered from passed data.

Parameters:

Name Type Description Default
n_reservoir int

int, optional, default=100 Number of reservoir neurons. Only used if W is not passed. If W is passed, n_reservoir gets overwritten with len(W). Either n_reservoir or W must be passed.

100
W ndarray | None

np.ndarray of shape (n_reservoir, n_reservoir), optional, default=None Reservoir weights matrix. If None, random weights are used (uniformly distributed around 0, ie., in [-0.5, 0.5). Be careful with the distribution of W values. Wrong W initialization might drastically affect test performance (even with reasonable good training fit). Spectral radius will be adjusted in all cases. Either n_reservoir or W must be passed.

None
spectral_radius float

float, default=.99 Spectral radius of the reservoir weights matrix (W). Spectral radius will be adjusted in all cases (also with user specified W).

0.99
W_in ndarray | None

np.ndarray of shape (n_reservoir, 1+n_inputs) (1->bias), optional, default None. Input weights matrix by which input signal is multiplied. If None, random weights are used.

None
W_fb ndarray | None

np.ndarray of shape(n_reservoir, n_outputs), optional, default None. Feedback weights matrix by which feedback is multiplied in case of feedback.

None
sparsity float

float, optional, default=0 Proportion of the reservoir matrix weights forced to be zero. Note that with default W (centered around 0), the actual sparsity will be slightly more than the specified. If W is passed, sparsity will be ignored.

0
noise float

float, optional, default=0 Scaling factor of the (uniform) noise input added to neurons at each step. This is used for regularization purposes and should typically be very small, e.g. 0.0001 or 1e-5.

0
leak_rate float

float, optional, default=1 Leaking rate applied to the neurons at each step. Default is 1, which is no leaking. 0 would be total leakeage.

1
bias float | ndarray

int, float or np.ndarray, optional, default=1 Value of the bias neuron, injected at each time to the reservoir neurons. If int or float, all neurons receive the same. If np.ndarray is must be of length n_reservoir.

1
input_scaling float | ndarray | None

float or np.ndarray of length n_inputs, default=None Scalar to multiply each input before feeding it to the network. If float, all inputs get multiplied by same value. If array, it must match n_inputs length (X.shape[1]), specifying the scaling factor for each input.

None
input_shift float | ndarray | None

float or np.ndarray of length n_inputs, default=None Scalar to add to each input before feeding it to the network. If float, multiplied same value is added to all inputs. If array, it must match n_inputs length (X.shape[1]), specifying the value to add to each input.

None
feedback bool

bool, optional, default=False If True, the reservoir also receives the outout signal as input.

False
activation Callable

function (numba jitted), optional, default=tanh Non-linear activation function applied to the neurons at each step. For numba acceleration, it must be a jitted function. Basic activation functions as tanh, sigmoid, relu are already available in echoe.utils. Either use those or write a custom one decorated with numba njit.

tanh
activation_out Callable

function, optional, default=identity Activation function applied to the outputs. In other words, it is assumed that targets = f(outputs). So the output produced must be transformed.

identity
fit_only_states bool

bool,default=False If True, outgoing weights (W_out) are computed fitting only the reservoir states. Inputs and bias are still use to drive reservoir activity, but ignored for fitting W_out, both in the training and prediction phase.

False
regression_method str

str, optional, default "pinv" (pseudoinverse). Method to solve the linear regression to find out outgoing weights. One of ["pinv", "ridge"]. If "ridge", ridge_* parameters will be used.

'pinv'
ridge_alpha float

float, ndarray of shape (n_outputs,), default=1 Regularization coefficient used for Ridge regression. Larger values specify stronger regularization. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number. Default is None to make sure one deliberately sets this since it is a crucial parameter. See sklearn Ridge documentation for details.

1
ridge_fit_intercept bool

bool, optional, default=False If True, intercept is fit in Ridge regression. Default False. See sklearn Ridge documentation for details.

False
ridge_max_iter int | None

int, default=None Maximum number of iterations for conjugate gradient solver. See sklearn Ridge documentation for details.

None
ridge_tol float

float, default=1e-3 Precision of the solution. See sklearn Ridge documentation for details.

0.001
ridge_solver str

str, optional, default="auto" Solver to use in the Ridge regression. One of ["auto", "svd", "cholesky", "lsqr", "sparse_cg", "sag", "saga"]. See sklearn Ridge documentation for details.

'auto'
ridge_sample_weight float | ndarray | None

float or ndarray of shape (n_samples,), default=None Individual weights for each sample. If given a float, every sample will have the same weight. See sklearn Ridge documentation for details.

None
n_transient int

int, optional, default=0 Number of activity initial steps removed (not considered for training) in order to avoid initial instabilities. Default is 0, but this is something one definitely might want to tweak.

0
random_state

int, RandomState instance, default=None The seed of the pseudo random number generator used to generate weight matrices, to generate noise inyected to reservoir neurons (regularization) and it is passed to the ridge solver in case regression_method=ridge. From sklearn: If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

None
store_states_train bool

bool, optional, default=False If True, time series series of reservoir neurons during training are stored in the object attribute states_train_.

False
store_states_pred bool

bool, optional, default=False If True, time series series of reservoir neurons during prediction are stored in the object attribute states_pred_.

False

Attributes:

- W_out_ : array of shape (n_outputs, n_inputs + n_reservoir + 1)
     Outgoing weights after fitting linear regression model to predict outputs.
- training_prediction_: array of shape (n_samples, n_outputs)
     Predicted output on training data.
- states_train_: array of shape (n_samples, n_reservoir), default False.
     If store_states_train is True, states matrix is stored for visualizing
     reservoir neurons activity during training.
- states_pred_: array of shape (n_samples, n_reservoir), default False.
     If store_states_pred is True, states matrix is stored for visualizing
     reservoir neurons activity during prediction (test).
Source code in echoes/esn/_regressor.py
 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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
class ESNRegressor(ESNBase, MultiOutputMixin, RegressorMixin):
    """
    Number of input and output neurons are infered from passed data.

    Arguments:
        n_reservoir: int, optional, default=100
            Number of reservoir neurons. Only used if W is not passed.
            If W is passed, n_reservoir gets overwritten with len(W).
            Either n_reservoir or W must be passed.
        W: np.ndarray of shape (n_reservoir, n_reservoir), optional, default=None
            Reservoir weights matrix. If None, random weights are used (uniformly
            distributed around 0, ie., in [-0.5, 0.5).
            Be careful with the distribution of W values. Wrong W initialization
            might drastically affect test performance (even with reasonable good
            training fit).
            Spectral radius will be adjusted in all cases.
            Either n_reservoir or W must be passed.
        spectral_radius: float, default=.99
            Spectral radius of the reservoir weights matrix (W).
            Spectral radius will be adjusted in all cases (also with user specified W).
        W_in: np.ndarray of shape (n_reservoir, 1+n_inputs) (1->bias),
            optional, default None.
            Input weights matrix by which input signal is multiplied.
            If None, random weights are used.
        W_fb: np.ndarray of shape(n_reservoir, n_outputs), optional, default None.
            Feedback weights matrix by which feedback is multiplied in case of feedback.
        sparsity: float, optional, default=0
            Proportion of the reservoir matrix weights forced to be zero.
            Note that with default W (centered around 0), the actual sparsity will
            be slightly more than the specified.
            If W is passed, sparsity will be ignored.
        noise: float, optional, default=0
            Scaling factor of the (uniform) noise input added to neurons at each step.
            This is used for regularization purposes and should typically be
            very small, e.g. 0.0001 or 1e-5.
        leak_rate: float, optional, default=1
            Leaking rate applied to the neurons at each step.
            Default is 1, which is no leaking. 0 would be total leakeage.
        bias: int, float or np.ndarray, optional, default=1
            Value of the bias neuron, injected at each time to the reservoir neurons.
            If int or float, all neurons receive the same.
            If np.ndarray is must be of length n_reservoir.
        input_scaling: float or np.ndarray of length n_inputs, default=None
            Scalar to multiply each input before feeding it to the network.
            If float, all inputs get multiplied by same value.
            If array, it must match n_inputs length (X.shape[1]), specifying the scaling
            factor for each input.
        input_shift: float or np.ndarray of length n_inputs, default=None
            Scalar to add to each input before feeding it to the network.
            If float, multiplied same value is added to all inputs.
            If array, it must match n_inputs length (X.shape[1]), specifying the value
            to add to each input.
        feedback: bool, optional, default=False
            If True, the reservoir also receives the outout signal as input.
        activation: function (numba jitted), optional, default=tanh
            Non-linear activation function applied to the neurons at each step.
            For numba acceleration, it must be a jitted function.
            Basic activation functions as tanh, sigmoid, relu are already available
            in echoe.utils. Either use those or write a custom one decorated with
            numba njit.
        activation_out: function, optional, default=identity
            Activation function applied to the outputs. In other words, it is assumed
            that targets = f(outputs). So the output produced must be transformed.
        fit_only_states: bool,default=False
            If True, outgoing weights (W_out) are computed fitting only the reservoir
            states. Inputs and bias are still use to drive reservoir activity, but
            ignored for fitting W_out, both in the training and prediction phase.
        regression_method: str, optional, default "pinv" (pseudoinverse).
            Method to solve the linear regression to find out outgoing weights.
            One of ["pinv", "ridge"].
            If "ridge", ridge_* parameters will be used.
        ridge_alpha: float, ndarray of shape (n_outputs,), default=1
            Regularization coefficient used for Ridge regression.
            Larger values specify stronger regularization.
            If an array is passed, penalties are assumed to be specific to the targets.
            Hence they must correspond in number.
            Default is None to make sure one deliberately sets this since it is
            a crucial parameter. See sklearn Ridge documentation for details.
        ridge_fit_intercept: bool, optional, default=False
            If True, intercept is fit in Ridge regression. Default False.
            See sklearn Ridge documentation for details.
        ridge_max_iter: int, default=None
            Maximum number of iterations for conjugate gradient solver.
            See sklearn Ridge documentation for details.
        ridge_tol: float, default=1e-3
            Precision of the solution.
            See sklearn Ridge documentation for details.
        ridge_solver: str, optional, default="auto"
            Solver to use in the Ridge regression.
            One of ["auto", "svd", "cholesky", "lsqr", "sparse_cg", "sag", "saga"].
            See sklearn Ridge documentation for details.
        ridge_sample_weight: float or ndarray of shape (n_samples,), default=None
            Individual weights for each sample.
            If given a float, every sample will have the same weight.
            See sklearn Ridge documentation for details.
        n_transient: int, optional, default=0
            Number of activity initial steps removed (not considered for training)
            in order to avoid initial instabilities.
            Default is 0, but this is something one definitely might want to tweak.
        random_state : int, RandomState instance, default=None
            The seed of the pseudo random number generator used to generate weight
            matrices, to generate noise inyected to reservoir neurons (regularization)
            and it is passed to the ridge solver in case regression_method=ridge.
            From sklearn:
              If int, random_state is the seed used by the random number generator;
              If RandomState instance, random_state is the random number generator;
              If None, the random number generator is the RandomState instance used
              by `np.random`.
        store_states_train: bool, optional, default=False
            If True, time series series of reservoir neurons during training are stored
            in the object attribute states_train_.
        store_states_pred: bool, optional, default=False
            If True, time series series of reservoir neurons during prediction are
            stored in the object attribute states_pred_.

    ### Attributes:
        - W_out_ : array of shape (n_outputs, n_inputs + n_reservoir + 1)
             Outgoing weights after fitting linear regression model to predict outputs.
        - training_prediction_: array of shape (n_samples, n_outputs)
             Predicted output on training data.
        - states_train_: array of shape (n_samples, n_reservoir), default False.
             If store_states_train is True, states matrix is stored for visualizing
             reservoir neurons activity during training.
        - states_pred_: array of shape (n_samples, n_reservoir), default False.
             If store_states_pred is True, states matrix is stored for visualizing
             reservoir neurons activity during prediction (test).
    """

    def fit(self, X: np.ndarray, y: np.ndarray) -> "ESNRegressor":
        """
        Fit Echo State model, i.e., find outgoing weights matrix (W_out) for later
        prediction.
        Bias is appended automatically to the inputs.

        Arguments:
            X: None or 2D np.ndarray of shape (n_samples, n_inputs)
                Training input, i.e., X, the features.
                If None, it is assumed that only the target sequence matters (outputs)
                and simply a sequence of zeros will be fed in - matching the
                len(outputs).
                This is to be used in the case of generative mode.
            y: 2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs)
                Training output, i.e., y, the target.

        Returns
            self: returns an instance of self.
        """
        self._dtype_ = X.dtype
        X, y = check_X_y(X, y, multi_output=True, dtype=self._dtype_)
        if y.ndim == 1:
            y = y.reshape(-1, 1)
        # Check y again (enforcing 2D for multiple outputs)
        y = check_array(y, dtype=self._dtype_)

        # Initialize matrices and random state
        self.random_state_ = check_random_state(self.random_state)
        self.n_inputs_ = X.shape[1]
        self.n_reservoir_ = len(self.W) if self.W is not None else self.n_reservoir
        self.n_outputs_ = y.shape[1]
        self.W_in_ = self._init_incoming_weights()
        self.W_ = self._init_reservoir_weights()
        self.W_fb_ = self._init_feedback_weights()

        check_model_params(self.__dict__)
        X = self._scale_shift_inputs(X)

        # Initialize reservoir model
        self.reservoir_ = self._init_reservoir_neurons()

        # Run "neuronal activity"
        states = self.reservoir_.harvest_states(X, y, initial_state=None)

        # Extend states matrix with inputs, except we only train based on states
        full_states = states if self.fit_only_states else np.hstack((states, X))

        # Solve for W_out using full states and outputs, excluding transient
        self.W_out_ = self._solve_W_out(
            full_states[self.n_transient :, :], y[self.n_transient :, :]
        )
        # Predict on training set (including the pass through the output nonlinearity)
        self.training_prediction_ = self.activation_out(full_states @ self.W_out_.T)

        # Store reservoir activity
        if self.store_states_train:
            self.states_train_ = states
        return self

    def predict(self, X: np.ndarray) -> np.ndarray:
        """
        Predict outputs according to inputs.
        State/output is reinitialized to predict test outputs from
        inputs as a typical predictive model. Since the reservoir states are
        reinitialized, an initial transient, unstable phase will occur, so you
        might want to cut off those steps to test performance (as done by the
        parameter n_transient during training).

        Arguments:
            X: 2D np.ndarray of shape (n_samples, n_inputs)
                Input, i.e., X, the features.

        Returns:
            y_pred: 2D np.ndarray of shape (n_samples, n_outputs)
                Predicted outputs.
        """
        check_is_fitted(self)
        X = check_array(X, dtype=self._dtype_)

        n_time_steps = X.shape[0]

        # Scale and shift inputs
        X = self._scale_shift_inputs(X)

        # Initialize predictions
        states = np.zeros((n_time_steps, self.n_reservoir_), dtype=self._dtype_)
        y_pred = np.zeros((n_time_steps, self.n_outputs_), dtype=self._dtype_)

        check_consistent_length(X, y_pred)  # sanity check

        # Go through samples (steps) and predict for each of them
        for t in range(1, n_time_steps):
            states[t, :] = self.reservoir_.update_state(
                state_t=states[t - 1, :],
                X_t=X[t, :],
                y_t=y_pred[t - 1, :],
            )

            if self.fit_only_states:
                full_states = states[t, :]
            else:
                full_states = np.concatenate([states[t, :], X[t, :]])
            # Predict
            y_pred[t, :] = self.activation_out(self.W_out_ @ full_states)

        # Store reservoir activity
        if self.store_states_pred:
            self.states_pred_ = states

        return y_pred

    def score(self, X: np.ndarray, y=np.ndarray, sample_weight=None) -> float:
        """
        R^2 (coefficient of determination) regression score function.

        By default, the initial transient period (n_transient steps) is not considered
        to compute the score - modify sample_weight to change that behaviour
        (see below).

        From sklearn:
          Best possible score is 1.0 and it can be negative (because the model can be
          arbitrarily bad).
          A constant model that always predicts the expected value of y,
          disregarding the input features, would get a R^2 score of 0.0.

        Arguments:
            X: 2D np.ndarray of shape (n_samples, n_inputs)
                Test samples.
            y: 2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs)
                Target sequence, true values of the outputs.
            sample_weight: array-like of shape (n_samples,), default=None
                Sample weights.
                If None, the transient is left out.
                To consider all steps or leave out a different transient, pass a
                different sample_weight array with same length as outputs 1 dimension.
                **Usage**
                  >> n_steps_to_remove = 10
                  >> weights = np.ones(y_true.shape[0])
                  >> weights[: n_steps_to_remove] = 0
                  >> score(X, y_true, sample_weight=weights)

        Returns:
            score: float
                R2 score
        """
        y_pred = self.predict(X)
        # If no sample_weight passed, compute the score without considering transient
        if sample_weight is None:
            weights = np.ones(y.shape[0])
            weights[: self.n_transient] = 0
            return r2_score(y, y_pred, sample_weight=weights)

        return r2_score(y, y_pred, sample_weight=sample_weight)

fit(X, y)

Fit Echo State model, i.e., find outgoing weights matrix (W_out) for later prediction. Bias is appended automatically to the inputs.

Parameters:

Name Type Description Default
X ndarray

None or 2D np.ndarray of shape (n_samples, n_inputs) Training input, i.e., X, the features. If None, it is assumed that only the target sequence matters (outputs) and simply a sequence of zeros will be fed in - matching the len(outputs). This is to be used in the case of generative mode.

required
y ndarray

2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs) Training output, i.e., y, the target.

required

Returns self: returns an instance of self.

Source code in echoes/esn/_regressor.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def fit(self, X: np.ndarray, y: np.ndarray) -> "ESNRegressor":
    """
    Fit Echo State model, i.e., find outgoing weights matrix (W_out) for later
    prediction.
    Bias is appended automatically to the inputs.

    Arguments:
        X: None or 2D np.ndarray of shape (n_samples, n_inputs)
            Training input, i.e., X, the features.
            If None, it is assumed that only the target sequence matters (outputs)
            and simply a sequence of zeros will be fed in - matching the
            len(outputs).
            This is to be used in the case of generative mode.
        y: 2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs)
            Training output, i.e., y, the target.

    Returns
        self: returns an instance of self.
    """
    self._dtype_ = X.dtype
    X, y = check_X_y(X, y, multi_output=True, dtype=self._dtype_)
    if y.ndim == 1:
        y = y.reshape(-1, 1)
    # Check y again (enforcing 2D for multiple outputs)
    y = check_array(y, dtype=self._dtype_)

    # Initialize matrices and random state
    self.random_state_ = check_random_state(self.random_state)
    self.n_inputs_ = X.shape[1]
    self.n_reservoir_ = len(self.W) if self.W is not None else self.n_reservoir
    self.n_outputs_ = y.shape[1]
    self.W_in_ = self._init_incoming_weights()
    self.W_ = self._init_reservoir_weights()
    self.W_fb_ = self._init_feedback_weights()

    check_model_params(self.__dict__)
    X = self._scale_shift_inputs(X)

    # Initialize reservoir model
    self.reservoir_ = self._init_reservoir_neurons()

    # Run "neuronal activity"
    states = self.reservoir_.harvest_states(X, y, initial_state=None)

    # Extend states matrix with inputs, except we only train based on states
    full_states = states if self.fit_only_states else np.hstack((states, X))

    # Solve for W_out using full states and outputs, excluding transient
    self.W_out_ = self._solve_W_out(
        full_states[self.n_transient :, :], y[self.n_transient :, :]
    )
    # Predict on training set (including the pass through the output nonlinearity)
    self.training_prediction_ = self.activation_out(full_states @ self.W_out_.T)

    # Store reservoir activity
    if self.store_states_train:
        self.states_train_ = states
    return self

predict(X)

Predict outputs according to inputs. State/output is reinitialized to predict test outputs from inputs as a typical predictive model. Since the reservoir states are reinitialized, an initial transient, unstable phase will occur, so you might want to cut off those steps to test performance (as done by the parameter n_transient during training).

Parameters:

Name Type Description Default
X ndarray

2D np.ndarray of shape (n_samples, n_inputs) Input, i.e., X, the features.

required

Returns:

Name Type Description
y_pred ndarray

2D np.ndarray of shape (n_samples, n_outputs) Predicted outputs.

Source code in echoes/esn/_regressor.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def predict(self, X: np.ndarray) -> np.ndarray:
    """
    Predict outputs according to inputs.
    State/output is reinitialized to predict test outputs from
    inputs as a typical predictive model. Since the reservoir states are
    reinitialized, an initial transient, unstable phase will occur, so you
    might want to cut off those steps to test performance (as done by the
    parameter n_transient during training).

    Arguments:
        X: 2D np.ndarray of shape (n_samples, n_inputs)
            Input, i.e., X, the features.

    Returns:
        y_pred: 2D np.ndarray of shape (n_samples, n_outputs)
            Predicted outputs.
    """
    check_is_fitted(self)
    X = check_array(X, dtype=self._dtype_)

    n_time_steps = X.shape[0]

    # Scale and shift inputs
    X = self._scale_shift_inputs(X)

    # Initialize predictions
    states = np.zeros((n_time_steps, self.n_reservoir_), dtype=self._dtype_)
    y_pred = np.zeros((n_time_steps, self.n_outputs_), dtype=self._dtype_)

    check_consistent_length(X, y_pred)  # sanity check

    # Go through samples (steps) and predict for each of them
    for t in range(1, n_time_steps):
        states[t, :] = self.reservoir_.update_state(
            state_t=states[t - 1, :],
            X_t=X[t, :],
            y_t=y_pred[t - 1, :],
        )

        if self.fit_only_states:
            full_states = states[t, :]
        else:
            full_states = np.concatenate([states[t, :], X[t, :]])
        # Predict
        y_pred[t, :] = self.activation_out(self.W_out_ @ full_states)

    # Store reservoir activity
    if self.store_states_pred:
        self.states_pred_ = states

    return y_pred

score(X, y=np.ndarray, sample_weight=None)

R^2 (coefficient of determination) regression score function.

By default, the initial transient period (n_transient steps) is not considered to compute the score - modify sample_weight to change that behaviour (see below).

From sklearn

Best possible score is 1.0 and it can be negative (because the model can be arbitrarily bad). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.

Parameters:

Name Type Description Default
X ndarray

2D np.ndarray of shape (n_samples, n_inputs) Test samples.

required
y

2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs) Target sequence, true values of the outputs.

ndarray
sample_weight

array-like of shape (n_samples,), default=None Sample weights. If None, the transient is left out. To consider all steps or leave out a different transient, pass a different sample_weight array with same length as outputs 1 dimension. Usage

n_steps_to_remove = 10 weights = np.ones(y_true.shape[0]) weights[: n_steps_to_remove] = 0 score(X, y_true, sample_weight=weights)

None

Returns:

Name Type Description
score float

float R2 score

Source code in echoes/esn/_regressor.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def score(self, X: np.ndarray, y=np.ndarray, sample_weight=None) -> float:
    """
    R^2 (coefficient of determination) regression score function.

    By default, the initial transient period (n_transient steps) is not considered
    to compute the score - modify sample_weight to change that behaviour
    (see below).

    From sklearn:
      Best possible score is 1.0 and it can be negative (because the model can be
      arbitrarily bad).
      A constant model that always predicts the expected value of y,
      disregarding the input features, would get a R^2 score of 0.0.

    Arguments:
        X: 2D np.ndarray of shape (n_samples, n_inputs)
            Test samples.
        y: 2D np.ndarray of shape (n_samples,) or (n_samples, n_outputs)
            Target sequence, true values of the outputs.
        sample_weight: array-like of shape (n_samples,), default=None
            Sample weights.
            If None, the transient is left out.
            To consider all steps or leave out a different transient, pass a
            different sample_weight array with same length as outputs 1 dimension.
            **Usage**
              >> n_steps_to_remove = 10
              >> weights = np.ones(y_true.shape[0])
              >> weights[: n_steps_to_remove] = 0
              >> score(X, y_true, sample_weight=weights)

    Returns:
        score: float
            R2 score
    """
    y_pred = self.predict(X)
    # If no sample_weight passed, compute the score without considering transient
    if sample_weight is None:
        weights = np.ones(y.shape[0])
        weights[: self.n_transient] = 0
        return r2_score(y, y_pred, sample_weight=weights)

    return r2_score(y, y_pred, sample_weight=sample_weight)