@@ -739,17 +739,17 @@ def gen_sample(self,sample_size=None,x_continuous=None,x_categorical=None):
739
739
by default None.
740
740
x_categorical : numpy ndarray, optional
741
741
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``,
742
- by default None. Each element x [i,j] must satisfy
743
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
742
+ by default None. Each element x_categorical [i,j] must satisfy
743
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
744
744
745
745
Returns
746
746
-------
747
747
x_continuous : numpy ndarray
748
748
2 dimensional float array whose size is ``(sample_size,c_dim_continuous)``.
749
749
x_categorical : numpy ndarray, optional
750
750
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``.
751
- Each element x [i,j] must satisfies
752
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
751
+ Each element x_categorical [i,j] must satisfies
752
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
753
753
y : numpy ndarray
754
754
1 dimensional array whose size is ``sample_size``.
755
755
"""
@@ -789,7 +789,7 @@ def gen_sample(self,sample_size=None,x_continuous=None,x_categorical=None):
789
789
for i in range (self .c_dim_categorical ):
790
790
if x_categorical [:,i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
791
791
raise (DataFormatError (
792
- f"x_categorical[{ i } ].max() must smaller than "
792
+ f"x_categorical[:, { i } ].max() must smaller than "
793
793
+ f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
794
794
+ f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
795
795
else :
@@ -825,7 +825,7 @@ def gen_sample(self,sample_size=None,x_continuous=None,x_categorical=None):
825
825
for i in range (self .c_dim_categorical ):
826
826
if x_categorical [:,i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
827
827
raise (DataFormatError (
828
- f"x_categorical[{ i } ].max() must smaller than "
828
+ f"x_categorical[:, { i } ].max() must smaller than "
829
829
+ f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
830
830
+ f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
831
831
else :
@@ -844,9 +844,9 @@ def gen_sample(self,sample_size=None,x_continuous=None,x_categorical=None):
844
844
)
845
845
x_categorical = x_categorical .reshape (- 1 ,self .c_dim_categorical )
846
846
for i in range (self .c_dim_categorical ):
847
- if x_categorical [i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
847
+ if x_categorical [:, i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
848
848
raise (DataFormatError (
849
- f"x_categorical[{ i } ].max() must smaller than "
849
+ f"x_categorical[:, { i } ].max() must smaller than "
850
850
+ f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
851
851
+ f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
852
852
@@ -887,8 +887,8 @@ def save_sample(self,filename,sample_size,x=None):
887
887
by default None.
888
888
x_categorical : numpy ndarray, optional
889
889
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``,
890
- by default None. Each element x [i,j] must satisfy
891
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
890
+ by default None. Each element x_categorical [i,j] must satisfy
891
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
892
892
893
893
See Also
894
894
--------
@@ -1782,8 +1782,8 @@ def _MTRF(self,x_continuous,x_categorical,y,n_estimators=100,**kwargs):
1782
1782
by default None.
1783
1783
x_categorical : numpy ndarray, optional
1784
1784
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``,
1785
- by default None. Each element x [i,j] must satisfy
1786
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1785
+ by default None. Each element x_categorical [i,j] must satisfy
1786
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1787
1787
y : numpy ndarray
1788
1788
values of objective variable whose dtype may be int or float
1789
1789
n_estimators : int, optional
@@ -1843,8 +1843,8 @@ def _given_MT(self,x_continuous,x_categorical,y):
1843
1843
by default None.
1844
1844
x_categorical : numpy ndarray, optional
1845
1845
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``,
1846
- by default None. Each element x [i,j] must satisfy
1847
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1846
+ by default None. Each element x_categorical [i,j] must satisfy
1847
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1848
1848
y : numpy ndarray
1849
1849
values of objective variable whose dtype may be int or float
1850
1850
@@ -1874,8 +1874,8 @@ def update_posterior(self,x_continuous=None,x_categorical=None,y=None,alg_type='
1874
1874
by default None.
1875
1875
x_categorical : numpy ndarray, optional
1876
1876
2 dimensional int array whose size is ``(sample_size,c_dim_categorical)``,
1877
- by default None. Each element x [i,j] must satisfy
1878
- 0 <= x [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1877
+ by default None. Each element x_categorical [i,j] must satisfy
1878
+ 0 <= x_categorical [i,j] < self.c_num_children_vec[self.c_dim_continuous+i].
1879
1879
y : numpy ndarray
1880
1880
values of objective variable whose dtype may be int or float
1881
1881
alg_type : {'MTRF', 'given_MT'}, optional
@@ -1907,7 +1907,7 @@ def update_posterior(self,x_continuous=None,x_categorical=None,y=None,alg_type='
1907
1907
for i in range (self .c_dim_categorical ):
1908
1908
if x_categorical [:,i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
1909
1909
raise (DataFormatError (
1910
- f"x_categorical[{ i } ].max() must smaller than "
1910
+ f"x_categorical[:, { i } ].max() must smaller than "
1911
1911
+ f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
1912
1912
+ f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
1913
1913
x_categorical .reshape ([- 1 ,self .c_dim_categorical ])
@@ -1950,7 +1950,7 @@ def update_posterior(self,x_continuous=None,x_categorical=None,y=None,alg_type='
1950
1950
for i in range (self .c_dim_categorical ):
1951
1951
if x_categorical [:,i ].max () >= self .c_num_children_vec [self .c_dim_continuous + i ]:
1952
1952
raise (DataFormatError (
1953
- f"x_categorical[{ i } ].max() must smaller than "
1953
+ f"x_categorical[:, { i } ].max() must smaller than "
1954
1954
+ f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
1955
1955
+ f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
1956
1956
x_categorical .reshape ([- 1 ,self .c_dim_categorical ])
@@ -2292,33 +2292,84 @@ def get_p_params(self):
2292
2292
return None
2293
2293
2294
2294
def _calc_pred_dist_leaf (self ,node :_Node ,x ):
2295
- try :
2296
- node .sub_model .calc_pred_dist (x )
2297
- except :
2298
- node .sub_model .calc_pred_dist ()
2295
+ try :
2296
+ node .sub_model .calc_pred_dist (x )
2297
+ except :
2298
+ node .sub_model .calc_pred_dist ()
2299
2299
2300
- def _calc_pred_dist_recursion (self ,node :_Node ,x ):
2301
- self ._calc_pred_dist_leaf (node ,x )
2300
+ def _calc_pred_dist_recursion (self ,node :_Node ,x_continuous , x_categorical ):
2301
+ self ._calc_pred_dist_leaf (node ,x_continuous )
2302
2302
if not node .leaf : # inner node
2303
- self ._calc_pred_dist_recursion (node .children [x [node .k ]],x )
2303
+ if node .k < self .c_dim_continuous :
2304
+ for i in range (self .c_num_children_vec [node .k ]):
2305
+ if node .thresholds [i ] < x_continuous [node .k ] and x_continuous [node .k ] < node .thresholds [i + 1 ]:
2306
+ index = i
2307
+ break
2308
+ else :
2309
+ index = x_categorical [node .k - self .c_dim_continuous ]
2310
+ self ._calc_pred_dist_recursion (node .children [index ],x_continuous ,x_categorical )
2304
2311
2305
- def calc_pred_dist (self ,x_continuous ,x_categorical ):
2312
+ def calc_pred_dist (self ,x_continuous = None ,x_categorical = None ):
2306
2313
"""Calculate the parameters of the predictive distribution.
2307
2314
2308
2315
Parameters
2309
2316
----------
2310
- x : numpy ndarray
2311
- values of explanatory variables whose dtype is int
2317
+ x_continuous : numpy ndarray, optional
2318
+ A float vector whose length is ``self.c_dim_continuous``,
2319
+ by default None.
2320
+ x_categorical : numpy ndarray, optional
2321
+ A int vector whose length is ``self.c_dim_categorical``,
2322
+ by default None. Each element x_categorical[i] must satisfy
2323
+ 0 <= x_categorical[i] < self.c_num_children_vec[self.c_dim_continuous+i].
2312
2324
"""
2313
- pass
2314
- # _check.nonneg_int_vec(x,'x',DataFormatError)
2315
- # if x.shape[0] != self.c_k:
2316
- # raise(DataFormatError(f"x.shape[0] must equal to c_k:{self.c_k}"))
2317
- # if x.max() >= self.c_num_children:
2318
- # raise(DataFormatError(f"x.max() must smaller than c_num_children:{self.c_num_children}"))
2319
- # self._tmp_x[:] = x
2320
- # for root in self.hn_metatree_list:
2321
- # self._calc_pred_dist_recursion(root,self._tmp_x)
2325
+ if self .c_dim_continuous > 0 and self .c_dim_categorical > 0 :
2326
+ _check .float_vec (x_continuous ,'x_continuous' ,DataFormatError )
2327
+ _check .shape_consistency (
2328
+ x_continuous .shape [0 ],'x_continuous.shape[0]' ,
2329
+ self .c_dim_continuous ,'self.c_dim_continuous' ,
2330
+ ParameterFormatError
2331
+ )
2332
+ _check .nonneg_int_vec (x_categorical ,'x_categorical' ,DataFormatError )
2333
+ _check .shape_consistency (
2334
+ x_categorical .shape [0 ],'x_categorical.shape[0]' ,
2335
+ self .c_dim_categorical ,'self.c_dim_categorical' ,
2336
+ ParameterFormatError
2337
+ )
2338
+ for i in range (self .c_dim_categorical ):
2339
+ if x_categorical [i ] >= self .c_num_children_vec [self .c_dim_continuous + i ]:
2340
+ raise (DataFormatError (
2341
+ f"x_categorical[{ i } ] must smaller than "
2342
+ + f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
2343
+ + f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
2344
+
2345
+ elif self .c_dim_continuous > 0 :
2346
+ _check .float_vec (x_continuous ,'x_continuous' ,DataFormatError )
2347
+ _check .shape_consistency (
2348
+ x_continuous .shape [0 ],'x_continuous.shape[0]' ,
2349
+ self .c_dim_continuous ,'self.c_dim_continuous' ,
2350
+ ParameterFormatError
2351
+ )
2352
+ x_categorical = np .empty (0 ) # dummy
2353
+
2354
+ elif self .c_dim_categorical > 0 :
2355
+ _check .nonneg_int_vec (x_categorical ,'x_categorical' ,DataFormatError )
2356
+ _check .shape_consistency (
2357
+ x_categorical .shape [0 ],'x_categorical.shape[0]' ,
2358
+ self .c_dim_categorical ,'self.c_dim_categorical' ,
2359
+ ParameterFormatError
2360
+ )
2361
+ for i in range (self .c_dim_categorical ):
2362
+ if x_categorical [i ] >= self .c_num_children_vec [self .c_dim_continuous + i ]:
2363
+ raise (DataFormatError (
2364
+ f"x_categorical[{ i } ] must smaller than "
2365
+ + f"self.c_num_children_vec[{ self .c_dim_continuous + i } ]: "
2366
+ + f"{ self .c_num_children_vec [self .c_dim_continuous + i ]} " ))
2367
+ x_continuous = np .empty (0 ) # dummy
2368
+
2369
+ self ._tmp_x_continuous [:] = x_continuous
2370
+ self ._tmp_x_categorical [:] = x_categorical
2371
+ for root in self .hn_metatree_list :
2372
+ self ._calc_pred_dist_recursion (root ,self ._tmp_x_continuous ,self ._tmp_x_categorical )
2322
2373
2323
2374
def _make_prediction_recursion_squared (self ,node :_Node ):
2324
2375
if node .leaf == False : # inner node
0 commit comments