Coverage for mlinsights/timeseries/dummies.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-09 08:45 +0200

1""" 

2@file 

3@brief Dummy auto-regressor which takes past values as predictions. 

4""" 

5import numpy 

6from .base import BaseTimeSeries, TimeSeriesRegressorMixin 

7from .utils import check_ts_X_y 

8 

9 

10class DummyTimeSeriesRegressor(BaseTimeSeries, TimeSeriesRegressorMixin): 

11 """ 

12 Dummy regressor for time series. Use past values as prediction. 

13 """ 

14 

15 def __init__(self, estimator="dummy", past=1, delay1=1, delay2=2, 

16 use_all_past=False, preprocessing=None): 

17 """ 

18 @param estimator estimator to use for regression, 

19 :epkg:`sklearn:linear_model:LinearRegression` 

20 implements a linear auto-regressor, 

21 ``'dummy'`` use past value as predictions 

22 @param past values to use to predict 

23 @param delay1 the model computes the first prediction for 

24 *time=t + delay1* 

25 @param delay2 the model computes the last prediction for 

26 *time=t + delay2* excluded 

27 @param use_all_past use all past features, not only the timeseries 

28 @param preprocessing preprocessing to apply before predicting, 

29 only the timeseries itselves, it can be 

30 a difference, it must be of type 

31 @see cl BaseReciprocalTimeSeriesTransformer 

32 """ 

33 TimeSeriesRegressorMixin.__init__(self) 

34 BaseTimeSeries.__init__(self, past=past, delay1=delay1, delay2=delay2, 

35 use_all_past=use_all_past, preprocessing=preprocessing) 

36 

37 def fit(self, X, y, sample_weight=None): 

38 """ 

39 Trains the model. 

40 

41 :param X: output of 

42 X may be empty (None) 

43 :param y: timeseries (one single vector), array [n_obs] 

44 :param sample_weight: weights None or array [n_obs] 

45 :return: self 

46 """ 

47 X, y, _ = self._base_fit_predict(X, y, sample_weight) 

48 check_ts_X_y(self, X, y) 

49 return self 

50 

51 def predict(self, X, y): 

52 """ 

53 Returns the prediction 

54 """ 

55 X, y, _ = self._base_fit_predict(X, y, None) 

56 check_ts_X_y(self, X, y) 

57 nbrow = X.shape[0] 

58 X, y = self._applies_preprocessing(X, y, None)[:2] 

59 nb = self.delay2 - self.delay1 

60 pred = numpy.empty((nbrow, nb), dtype=X.dtype) 

61 first = nbrow - X.shape[0] 

62 pred[:first] = numpy.nan 

63 for i in range(0, nb): 

64 pred[first:, i] = X[:, -1] 

65 return pred