@@ -33,6 +33,8 @@ class DevelopmentML(DevelopmentBase):
3333 Time Series aspects of the model. Predictions from one development period
3434 get used as featues in the next development period. Lags should be negative
3535 integers.
36+ weight_step: str
37+ Step name within estimator_ml that is weighted
3638 drop: tuple or list of tuples
3739 Drops specific origin/development combination(s)
3840 drop_valuation: str or list of str (default = None)
@@ -56,8 +58,7 @@ def test_func(df)
5658 return df['origin'] + 1
5759 )
5860 fit_incrementals:
59- Whether the response variable should be converted to an incremental basis
60- for fitting.
61+ Whether the response variable should be converted to an incremental basis for fitting.
6162
6263 Attributes
6364 ----------
@@ -70,10 +71,9 @@ def test_func(df)
7071 """
7172
7273 def __init__ (self , estimator_ml = None , y_ml = None , autoregressive = False ,
73- weight_ml = None , weighted_step = None ,drop = None ,drop_valuation = None ,fit_incrementals = True , feat_eng = None ):
74+ weighted_step = None ,drop = None ,drop_valuation = None ,fit_incrementals = True , feat_eng = None ):
7475 self .estimator_ml = estimator_ml
7576 self .y_ml = y_ml
76- self .weight_ml = weight_ml
7777 self .weighted_step = weighted_step
7878 self .autoregressive = autoregressive
7979 self .drop = drop
@@ -168,7 +168,7 @@ def _prep_X_ml(self, X):
168168 df_base = X .incr_to_cum ().to_frame (
169169 keepdims = True , implicit_axis = True , origin_as_datetime = True
170170 ).reset_index ().iloc [:, :- 1 ]
171- df = df_base .merge (X . cum_to_incr () .to_frame (
171+ df = df_base .merge (X_ .to_frame (
172172 keepdims = True , implicit_axis = True , origin_as_datetime = True
173173 ).reset_index (), how = 'left' ,
174174 on = list (df_base .columns )).fillna (0 )
@@ -177,13 +177,18 @@ def _prep_X_ml(self, X):
177177 if self .feat_eng is not None :
178178 for key , item in self .feat_eng .items ():
179179 df [key ] = item ['func' ](df = df ,** item ['kwargs' ])
180+ return df
181+
182+ def _prep_w_ml (self ,X ,sample_weight = None ):
180183 weight_base = (~ np .isnan (X .values )).astype (float )
181- weight = weight_base .copy ()
184+ weight = weight_base .copy ()
182185 if self .drop is not None :
183186 weight = weight * self ._drop_func (X )
184187 if self .drop_valuation is not None :
185- weight = weight * self ._drop_valuation_func (X )
186- return df , weight .flatten ()[weight_base .flatten ()> 0 ]
188+ weight = weight * self ._drop_valuation_func (X )
189+ if sample_weight is not None :
190+ weight = weight * sample_weight .values
191+ return weight .flatten ()[weight_base .flatten ()> 0 ]
187192
188193 def fit (self , X , y = None , sample_weight = None ):
189194 """Fit the model with X.
@@ -194,8 +199,8 @@ def fit(self, X, y=None, sample_weight=None):
194199 Set of LDFs to which the estimator will be applied.
195200 y : None
196201 Ignored, use y_ml to set a reponse variable for the ML algorithm
197- sample_weight : None
198- Ignored
202+ sample_weight : Triangle-like
203+ Weights to use in the regression
199204
200205 Returns
201206 -------
@@ -214,8 +219,9 @@ def fit(self, X, y=None, sample_weight=None):
214219 self .valuation_encoder_ = dict (zip (
215220 val ,
216221 (pd .Series (val ).rank ()- 1 )/ {'Y' :1 , 'S' : 2 , 'Q' :4 , 'M' : 12 }[X .development_grain ]))
217- df , weight = self ._prep_X_ml (X )
222+ df = self ._prep_X_ml (X )
218223 self .df_ = df
224+ weight = self ._prep_w_ml (X ,sample_weight )
219225 self .weight_ = weight
220226 if self .weighted_step == None :
221227 sample_weights = {}
@@ -249,7 +255,7 @@ def transform(self, X):
249255 X_new : New triangle with transformed attributes.
250256 """
251257 X_new = X .copy ()
252- X_ml , weight_ml = self ._prep_X_ml (X )
258+ X_ml = self ._prep_X_ml (X )
253259 y_ml = self .estimator_ml .predict (X_ml )
254260 triangle_ml , predicted_data = self ._get_triangle_ml (X_ml , y_ml )
255261 backend = "cupy" if X .array_backend == "cupy" else "numpy"
0 commit comments