Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2940b26

Browse files
Merge pull request wilsonrljr#126 from wilsonrljr/code/typehints
Code/typehints
2 parents dfa0c9a + 02af7da commit 2940b26

File tree

11 files changed

+343
-145
lines changed

11 files changed

+343
-145
lines changed

‎.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pip-wheel-metadata
3232

3333
# VS Code
3434
.vscode
35-
35+
.pylintrc
3636
# default pytest cache directory
3737
*/.pytest_cache
3838
.pytest_cache/

‎sysidentpy/general_estimators/narx.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
196196
The input data to be used in the prediction process.
197197
y : ndarray of floats
198198
The output data to be used in the prediction process.
199+
steps_ahead : int (default = None)
200+
The user can use free run simulation, one-step ahead prediction
201+
and n-step ahead prediction.
202+
forecast_horizon : int, default=None
203+
The number of predictions over the time.
199204
200205
Returns
201206
-------
@@ -349,6 +354,9 @@ def _n_step_ahead_prediction(self, X, y, steps_ahead):
349354
to start recursive process.
350355
X : ndarray of floats of shape = n_samples
351356
Vector with input values to be used in model simulation.
357+
steps_ahead : int (default = None)
358+
The user can use free run simulation, one-step ahead prediction
359+
and n-step ahead prediction.
352360
353361
Returns
354362
-------
@@ -372,6 +380,8 @@ def _model_prediction(self, X, y_initial, forecast_horizon=None):
372380
to start recursive process.
373381
X : ndarray of floats of shape = n_samples
374382
Vector with input values to be used in model simulation.
383+
forecast_horizon : int, default=None
384+
The number of predictions over the time.
375385
376386
Returns
377387
-------
@@ -495,6 +505,11 @@ def _basis_function_n_step_prediction(self, X, y, steps_ahead, forecast_horizon)
495505
to start recursive process.
496506
X : ndarray of floats of shape = n_samples
497507
Vector with input values to be used in model simulation.
508+
steps_ahead : int (default = None)
509+
The user can use free run simulation, one-step ahead prediction
510+
and n-step ahead prediction.
511+
forecast_horizon : int, default=None
512+
The number of predictions over the time.
498513
499514
Returns
500515
-------

‎sysidentpy/metaheuristics/bpsogsa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Binary Hybrid Particle Swarm Optimization and Gravitational Search Algorithm"""
2+
23
# Authors:
34
# Wilson Rocha Lacerda Junior <wilsonrljr@outlook.com>
45
# License: BSD 3 clause
@@ -77,7 +78,6 @@ def __init__(
7778
p_zeros=0.5,
7879
p_ones=0.5,
7980
):
80-
8181
self.dimension = dimension
8282
self.n_agents = n_agents
8383
self.maxiter = maxiter
@@ -251,7 +251,7 @@ def calculate_acceleration(
251251
for i in range(self.n_agents):
252252
for j in range(k_best_agents):
253253
if maximum_value_index[j] != i:
254-
euclidian_distance = np.linalg.norm(
254+
euclidean_distance = np.linalg.norm(
255255
population[:, i] - population[:, maximum_value_index[j]],
256256
self._norm,
257257
)
@@ -262,7 +262,7 @@ def calculate_acceleration(
262262
] * (
263263
population[:, maximum_value_index[j]] - population[:, i]
264264
) / (
265-
euclidian_distance**self._power + np.finfo(np.float64).eps
265+
euclidean_distance**self._power + np.finfo(np.float64).eps
266266
)
267267

268268
acceleration = gravitational_force * gravitational_constant

‎sysidentpy/metrics/_regression.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
]
2828

2929

30-
def forecast_error(y, yhat):
30+
def forecast_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
3131
"""Calculate the forecast error in a regression model.
3232
3333
Parameters
@@ -59,7 +59,7 @@ def forecast_error(y, yhat):
5959
return np.array(y - yhat)
6060

6161

62-
def mean_forecast_error(y, yhat):
62+
def mean_forecast_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
6363
"""Calculate the mean of forecast error of a regression model.
6464
6565
Parameters
@@ -92,7 +92,7 @@ def mean_forecast_error(y, yhat):
9292
return np.average(y - yhat)
9393

9494

95-
def mean_squared_error(y, yhat):
95+
def mean_squared_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
9696
"""Calculate the Mean Squared Error.
9797
9898
Parameters
@@ -157,7 +157,7 @@ def root_mean_squared_error(y: ArrayLike, yhat: ArrayLike) -> np.ndarray:
157157
return np.sqrt(mean_squared_error(y, yhat))
158158

159159

160-
def normalized_root_mean_squared_error(y, yhat):
160+
def normalized_root_mean_squared_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
161161
"""Calculate the normalized Root Mean Squared Error.
162162
163163
Parameters
@@ -189,7 +189,7 @@ def normalized_root_mean_squared_error(y, yhat):
189189
return root_mean_squared_error(y, yhat) / (y.max() - y.min())
190190

191191

192-
def root_relative_squared_error(y, yhat):
192+
def root_relative_squared_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
193193
"""Calculate the Root Relative Mean Squared Error.
194194
195195
Parameters
@@ -218,7 +218,7 @@ def root_relative_squared_error(y, yhat):
218218
return np.sqrt(np.divide(numerator, denominator))
219219

220220

221-
def mean_absolute_error(y, yhat):
221+
def mean_absolute_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
222222
"""Calculate the Mean absolute error.
223223
224224
Parameters
@@ -251,7 +251,7 @@ def mean_absolute_error(y, yhat):
251251
return np.average(output_errors)
252252

253253

254-
def mean_squared_log_error(y: ArrayLike, yhat: ArrayLike) -> np.ndarray:
254+
def mean_squared_log_error(y: ArrayLike, yhat: ArrayLike) -> ArrayLike:
255255
"""Calculate the Mean Squared Logarithmic Error.
256256
257257
Parameters
@@ -278,7 +278,7 @@ def mean_squared_log_error(y: ArrayLike, yhat: ArrayLike) -> np.ndarray:
278278
return mean_squared_error(np.log1p(y), np.log1p(yhat))
279279

280280

281-
def median_absolute_error(y, yhat):
281+
def median_absolute_error(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
282282
"""Calculate the Median Absolute Error.
283283
284284
Parameters
@@ -310,7 +310,7 @@ def median_absolute_error(y, yhat):
310310
return np.median(np.abs(y - yhat))
311311

312312

313-
def explained_variance_score(y, yhat):
313+
def explained_variance_score(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
314314
"""Calculate the Explained Variance Score.
315315
316316
Parameters
@@ -353,7 +353,7 @@ def explained_variance_score(y, yhat):
353353
return np.average(output_scores)
354354

355355

356-
def r2_score(y, yhat):
356+
def r2_score(y: ArrayLike, yhat: ArrayLike) ->ArrayLike:
357357
"""Calculate the R2 score. Based on sklearn solution.
358358
359359
Parameters
@@ -400,7 +400,9 @@ def r2_score(y, yhat):
400400
return np.average(output_scores)
401401

402402

403-
def symmetric_mean_absolute_percentage_error(y, yhat):
403+
def symmetric_mean_absolute_percentage_error(
404+
y: ArrayLike, yhat: ArrayLike
405+
) -> ArrayLike:
404406
"""Calculate the SMAPE score.
405407
406408
Parameters

‎sysidentpy/model_structure_selection/accelerated_orthogonal_least_squares.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Authors:
44
# Wilson Rocha Lacerda Junior <wilsonrljr@outlook.com>
55
# License: BSD 3 clause
6-
from typing import Tuple, Union
6+
from typing import Tuple, Union, Optional
77

88
import numpy as np
99
from numpy import linalg as LA
@@ -262,7 +262,7 @@ def aols(
262262
theta = theta[theta != 0]
263263
return theta.reshape(-1, 1), pivv, residual_norm
264264

265-
def fit(self, *, X=None, y=None):
265+
def fit(self, *, X: Optional[np.ndarray] =None, y: Optional[np.ndarray] =None):
266266
"""Fit polynomial NARMAX model using AOLS algorithm.
267267
268268
The 'fit' function allows a friendly usage by the user.
@@ -343,7 +343,14 @@ def fit(self, *, X=None, y=None):
343343
] # just to use the `results` method. Will be changed in next update.
344344
return self
345345

346-
def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
346+
def predict(
347+
self,
348+
*,
349+
X: Optional[np.ndarray] = None,
350+
y: Optional[np.ndarray] = None,
351+
steps_ahead: Optional[int] = None,
352+
forecast_horizon: int = 0,
353+
) -> np.ndarray:
347354
"""Return the predicted values given an input.
348355
349356
The predict function allows a friendly usage by the user.
@@ -401,7 +408,9 @@ def predict(self, *, X=None, y=None, steps_ahead=None, forecast_horizon=None):
401408
yhat = np.concatenate([y[: self.max_lag], yhat], axis=0)
402409
return yhat
403410

404-
def _one_step_ahead_prediction(self, X, y):
411+
def _one_step_ahead_prediction(
412+
self, X: Optional[np.ndarray], y: Optional[np.ndarray]
413+
) -> np.ndarray:
405414
"""Perform the 1-step-ahead prediction of a model.
406415
407416
Parameters
@@ -435,7 +444,12 @@ def _one_step_ahead_prediction(self, X, y):
435444
yhat = super()._one_step_ahead_prediction(X_base)
436445
return yhat.reshape(-1, 1)
437446

438-
def _n_step_ahead_prediction(self, X, y, steps_ahead):
447+
def _n_step_ahead_prediction(
448+
self,
449+
X: Optional[np.ndarray],
450+
y: Optional[np.ndarray],
451+
steps_ahead: Optional[int],
452+
) -> np.ndarray:
439453
"""Perform the n-steps-ahead prediction of a model.
440454
441455
Parameters
@@ -445,6 +459,9 @@ def _n_step_ahead_prediction(self, X, y, steps_ahead):
445459
to start recursive process.
446460
X : ndarray of floats of shape = n_samples
447461
Vector with input values to be used in model simulation.
462+
steps_ahead : int (default = None)
463+
The user can use free run simulation, one-step ahead prediction
464+
and n-step ahead prediction.
448465
449466
Returns
450467
-------
@@ -455,7 +472,12 @@ def _n_step_ahead_prediction(self, X, y, steps_ahead):
455472
yhat = super()._n_step_ahead_prediction(X, y, steps_ahead)
456473
return yhat
457474

458-
def _model_prediction(self, X, y_initial, forecast_horizon=None):
475+
def _model_prediction(
476+
self,
477+
X: Optional[np.ndarray],
478+
y_initial: Optional[np.ndarray],
479+
forecast_horizon: int = 1,
480+
) -> np.ndarray:
459481
"""Perform the infinity steps-ahead simulation of a model.
460482
461483
Parameters
@@ -481,7 +503,12 @@ def _model_prediction(self, X, y_initial, forecast_horizon=None):
481503
f"model_type must be NARMAX, NAR or NFIR. Got {self.model_type}"
482504
)
483505

484-
def _narmax_predict(self, X, y_initial, forecast_horizon):
506+
def _narmax_predict(
507+
self,
508+
X: Optional[np.ndarray],
509+
y_initial: Optional[np.ndarray],
510+
forecast_horizon: int = 1,
511+
) -> np.ndarray:
485512
if len(y_initial) < self.max_lag:
486513
raise ValueError(
487514
"Insufficient initial condition elements! Expected at least"
@@ -499,11 +526,18 @@ def _narmax_predict(self, X, y_initial, forecast_horizon):
499526
y_output = super()._narmax_predict(X, y_initial, forecast_horizon)
500527
return y_output
501528

502-
def _nfir_predict(self, X, y_initial):
529+
def _nfir_predict(
530+
self, X: Optional[np.ndarray], y_initial: Optional[np.ndarray]
531+
) -> np.ndarray:
503532
y_output = super()._nfir_predict(X, y_initial)
504533
return y_output
505534

506-
def _basis_function_predict(self, X, y_initial, forecast_horizon=None):
535+
def _basis_function_predict(
536+
self,
537+
X: Optional[np.ndarray],
538+
y_initial: Optional[np.ndarray],
539+
forecast_horizon: int = 1,
540+
) -> np.ndarray:
507541
if X is not None:
508542
forecast_horizon = X.shape[0]
509543
else:
@@ -515,7 +549,13 @@ def _basis_function_predict(self, X, y_initial, forecast_horizon=None):
515549
yhat = super()._basis_function_predict(X, y_initial, forecast_horizon)
516550
return yhat.reshape(-1, 1)
517551

518-
def _basis_function_n_step_prediction(self, X, y, steps_ahead, forecast_horizon):
552+
def _basis_function_n_step_prediction(
553+
self,
554+
X: Optional[np.ndarray],
555+
y: Optional[np.ndarray],
556+
steps_ahead: Optional[int],
557+
forecast_horizon: int,
558+
) -> np.ndarray:
519559
"""Perform the n-steps-ahead prediction of a model.
520560
521561
Parameters
@@ -548,7 +588,13 @@ def _basis_function_n_step_prediction(self, X, y, steps_ahead, forecast_horizon)
548588
)
549589
return yhat.reshape(-1, 1)
550590

551-
def _basis_function_n_steps_horizon(self, X, y, steps_ahead, forecast_horizon):
591+
def _basis_function_n_steps_horizon(
592+
self,
593+
X: Optional[np.ndarray],
594+
y: Optional[np.ndarray],
595+
steps_ahead: Optional[int],
596+
forecast_horizon: int,
597+
) -> np.ndarray:
552598
yhat = super()._basis_function_n_steps_horizon(
553599
X, y, steps_ahead, forecast_horizon
554600
)

‎sysidentpy/model_structure_selection/entropic_regression.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# License: BSD 3 clause
66

77
import warnings
8-
from typing import Union
8+
from typing import Union, Optional
99

1010
import numpy as np
1111
from numpy import linalg as LA
@@ -165,7 +165,7 @@ def __init__(
165165
weight: float = 0.02,
166166
model_type: str = "NARMAX",
167167
basis_function: Union[Polynomial, Fourier] = Polynomial(),
168-
random_state: Union[int, None] = None,
168+
random_state: Optional[int] = None,
169169
):
170170
self.basis_function = basis_function
171171
self.model_type = model_type
@@ -524,12 +524,6 @@ def tolerance_estimator(self, y):
524524
525525
"""
526526
ksg_estimation = []
527-
# ksg_estimation = [
528-
# getattr(self, self.mutual_information_estimator)(y,
529-
# self.rng.permutation(y))
530-
# for i in range(self.n_perm)
531-
# ]
532-
533527
for _ in range(self.n_perm):
534528
mutual_information_output = getattr(
535529
self, self.mutual_information_estimator
@@ -601,12 +595,10 @@ def fit(self, *, X=None, y=None):
601595

602596
if self.regressor_code.shape[0] > 90:
603597
warnings.warn(
604-
(
605-
"Given the higher number of possible regressors"
606-
f" ({self.regressor_code.shape[0]}), the Entropic Regression"
607-
" algorithm may take long time to run. Consider reducing the"
608-
" number of regressors "
609-
),
598+
"Given the higher number of possible regressors"
599+
f" ({self.regressor_code.shape[0]}), the Entropic Regression"
600+
" algorithm may take long time to run. Consider reducing the"
601+
" number of regressors ",
610602
stacklevel=2,
611603
)
612604

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /