Skip to content

Commit 7445e62

Browse files
committed
Faster copy of list and array
1 parent dd81760 commit 7445e62

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

bayesml/metatree/_metatree.py

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def _gen_params_recursion(self,node:_Node,h_node:_Node,feature_fix,threshold_fix
276276
node.thresholds[:] = np.linspace(node.ranges[node.k,0],node.ranges[node.k,1],self.c_num_children_vec[node.k]+1)
277277
else:
278278
node.thresholds = None
279-
child_k_candidates = node.k_candidates + []
279+
child_k_candidates = node.k_candidates.copy()
280280
child_k_candidates.remove(node.k)
281281
node.leaf = False
282282
for i in range(self.c_num_children_vec[node.k]):
@@ -286,8 +286,7 @@ def _gen_params_recursion(self,node:_Node,h_node:_Node,feature_fix,threshold_fix
286286
sub_model=self.SubModel.GenModel(seed=self.rng,**self.sub_h_params),
287287
)
288288
node.children[i].k_candidates = child_k_candidates
289-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
290-
node.children[i].ranges[:] = node.ranges
289+
node.children[i].ranges = np.array(node.ranges)
291290
if node.thresholds is not None:
292291
node.children[i].ranges[node.k,0] = node.thresholds[i]
293292
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -309,11 +308,10 @@ def _gen_params_recursion(self,node:_Node,h_node:_Node,feature_fix,threshold_fix
309308
node.k = h_node.k
310309
node.children = [None for i in range(self.c_num_children_vec[node.k])]
311310
if node.k < self.c_dim_continuous:
312-
node.thresholds = np.empty(self.c_num_children_vec[node.k]+1)
313-
node.thresholds[:] = h_node.thresholds
311+
node.thresholds = np.array(h_node.thresholds)
314312
else:
315313
node.thresholds = None
316-
child_k_candidates = node.k_candidates + []
314+
child_k_candidates = node.k_candidates.copy()
317315
child_k_candidates.remove(node.k)
318316
node.leaf = False
319317
for i in range(self.c_num_children_vec[node.k]):
@@ -323,8 +321,7 @@ def _gen_params_recursion(self,node:_Node,h_node:_Node,feature_fix,threshold_fix
323321
sub_model=self.SubModel.GenModel(seed=self.rng,**self.sub_h_params),
324322
)
325323
node.children[i].k_candidates = child_k_candidates
326-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
327-
node.children[i].ranges[:] = node.ranges
324+
node.children[i].ranges = np.array(node.ranges)
328325
if node.thresholds is not None:
329326
node.children[i].ranges[node.k,0] = node.thresholds[i]
330327
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -354,14 +351,13 @@ def _gen_params_recursion_feature_and_tree_fix(self,node:_Node,threshold_fix,thr
354351
node.thresholds[:] = np.linspace(node.ranges[node.k,0],node.ranges[node.k,1],self.c_num_children_vec[node.k]+1)
355352
else:
356353
node.thresholds = None
357-
child_k_candidates = node.k_candidates + []
354+
child_k_candidates = node.k_candidates.copy()
358355
child_k_candidates.remove(node.k)
359356
node.leaf = False
360357
for i in range(self.c_num_children_vec[node.k]):
361358
if node.children[i] is not None:
362359
node.children[i].k_candidates = child_k_candidates
363-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
364-
node.children[i].ranges[:] = node.ranges
360+
node.children[i].ranges = np.array(node.ranges)
365361
if node.thresholds is not None:
366362
node.children[i].ranges[node.k,0] = node.thresholds[i]
367363
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -387,11 +383,10 @@ def _set_params_recursion(self,node:_Node,original_tree_node:_Node):
387383
node.k = original_tree_node.k
388384
node.children = [None for i in range(self.c_num_children_vec[node.k])]
389385
if node.k < self.c_dim_continuous:
390-
node.thresholds = np.empty(self.c_num_children_vec[node.k]+1)
391-
node.thresholds[:] = original_tree_node.thresholds
386+
node.thresholds = np.array(original_tree_node.thresholds)
392387
else:
393388
node.thresholds = None
394-
child_k_candidates = node.k_candidates + []
389+
child_k_candidates = node.k_candidates.copy()
395390
child_k_candidates.remove(node.k)
396391
node.leaf = False
397392
for i in range(self.c_num_children_vec[node.k]):
@@ -402,8 +397,7 @@ def _set_params_recursion(self,node:_Node,original_tree_node:_Node):
402397
sub_model=self.SubModel.GenModel(seed=self.rng,**self.sub_h_params)
403398
)
404399
node.children[i].k_candidates = child_k_candidates
405-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
406-
node.children[i].ranges[:] = node.ranges
400+
node.children[i].ranges = np.array(node.ranges)
407401
if node.thresholds is not None:
408402
node.children[i].ranges[node.k,0] = node.thresholds[i]
409403
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -520,11 +514,10 @@ def _set_h_params_recursion(self,node:_Node,original_tree_node:_Node):
520514
node.k = original_tree_node.k
521515
node.children = [None for i in range(self.c_num_children_vec[node.k])]
522516
if node.k < self.c_dim_continuous:
523-
node.thresholds = np.empty(self.c_num_children_vec[node.k]+1)
524-
node.thresholds[:] = original_tree_node.thresholds
517+
node.thresholds = np.array(original_tree_node.thresholds)
525518
else:
526519
node.thresholds = None
527-
child_k_candidates = node.k_candidates + []
520+
child_k_candidates = node.k_candidates.copy()
528521
child_k_candidates.remove(node.k)
529522
node.leaf = False
530523
for i in range(self.c_num_children_vec[node.k]):
@@ -534,8 +527,7 @@ def _set_h_params_recursion(self,node:_Node,original_tree_node:_Node):
534527
sub_model=self.SubModel.GenModel(seed=self.rng,**self.sub_h_params),
535528
)
536529
node.children[i].k_candidates = child_k_candidates
537-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
538-
node.children[i].ranges[:] = node.ranges
530+
node.children[i].ranges = np.array(node.ranges)
539531
if node.thresholds is not None:
540532
node.children[i].ranges[node.k,0] = node.thresholds[i]
541533
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -618,7 +610,7 @@ def set_h_params(self,
618610
for i in range(len(self.h_metatree_list)):
619611
self._set_h_params_recursion(self.h_metatree_list[i],h_metatree_list[i])
620612
if h_metatree_prob_vec is not None:
621-
self.h_metatree_prob_vec = np.copy(
613+
self.h_metatree_prob_vec = np.array(
622614
_check.float_vec_sum_1(
623615
h_metatree_prob_vec,
624616
'h_metatree_prob_vec',
@@ -632,7 +624,7 @@ def set_h_params(self,
632624
else:
633625
self.h_metatree_prob_vec = None
634626
elif h_metatree_prob_vec is not None:
635-
self.h_metatree_prob_vec = np.copy(
627+
self.h_metatree_prob_vec = np.array(
636628
_check.float_vec_sum_1(
637629
h_metatree_prob_vec,
638630
'h_metatree_prob_vec',
@@ -1318,11 +1310,10 @@ def _set_h0_params_recursion(self,node:_Node,original_tree_node:_Node):
13181310
node.k = original_tree_node.k
13191311
node.children = [None for i in range(self.c_num_children_vec[node.k])]
13201312
if node.k < self.c_dim_continuous:
1321-
node.thresholds = np.empty(self.c_num_children_vec[node.k]+1)
1322-
node.thresholds[:] = original_tree_node.thresholds
1313+
node.thresholds = np.array(original_tree_node.thresholds)
13231314
else:
13241315
node.thresholds = None
1325-
child_k_candidates = node.k_candidates + []
1316+
child_k_candidates = node.k_candidates.copy()
13261317
child_k_candidates.remove(node.k)
13271318
node.leaf = False
13281319
for i in range(self.c_num_children_vec[node.k]):
@@ -1332,8 +1323,7 @@ def _set_h0_params_recursion(self,node:_Node,original_tree_node:_Node):
13321323
sub_model=self.SubModel.LearnModel(**self.sub_h0_params),
13331324
)
13341325
node.children[i].k_candidates = child_k_candidates
1335-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
1336-
node.children[i].ranges[:] = node.ranges
1326+
node.children[i].ranges = np.array(node.ranges)
13371327
if node.thresholds is not None:
13381328
node.children[i].ranges[node.k,0] = node.thresholds[i]
13391329
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -1382,11 +1372,10 @@ def _set_hn_params_recursion(self,node:_Node,original_tree_node:_Node):
13821372
node.k = original_tree_node.k
13831373
node.children = [None for i in range(self.c_num_children_vec[node.k])]
13841374
if node.k < self.c_dim_continuous:
1385-
node.thresholds = np.empty(self.c_num_children_vec[node.k]+1)
1386-
node.thresholds[:] = original_tree_node.thresholds
1375+
node.thresholds = np.array(original_tree_node.thresholds)
13871376
else:
13881377
node.thresholds = None
1389-
child_k_candidates = node.k_candidates + []
1378+
child_k_candidates = node.k_candidates.copy()
13901379
child_k_candidates.remove(node.k)
13911380
node.leaf = False
13921381
for i in range(self.c_num_children_vec[node.k]):
@@ -1396,8 +1385,7 @@ def _set_hn_params_recursion(self,node:_Node,original_tree_node:_Node):
13961385
sub_model=self.SubModel.LearnModel(**self.sub_hn_params),
13971386
)
13981387
node.children[i].k_candidates = child_k_candidates
1399-
node.children[i].ranges = np.empty([self.c_dim_continuous,2])
1400-
node.children[i].ranges[:] = node.ranges
1388+
node.children[i].ranges = np.array(node.ranges)
14011389
if node.thresholds is not None:
14021390
node.children[i].ranges[node.k,0] = node.thresholds[i]
14031391
node.children[i].ranges[node.k,1] = node.thresholds[i+1]
@@ -1480,7 +1468,7 @@ def set_h0_params(self,
14801468
for i in range(len(self.h0_metatree_list)):
14811469
self._set_h0_params_recursion(self.h0_metatree_list[i],h0_metatree_list[i])
14821470
if h0_metatree_prob_vec is not None:
1483-
self.h0_metatree_prob_vec = np.copy(
1471+
self.h0_metatree_prob_vec = np.array(
14841472
_check.float_vec_sum_1(
14851473
h0_metatree_prob_vec,
14861474
'h0_metatree_prob_vec',
@@ -1494,7 +1482,7 @@ def set_h0_params(self,
14941482
else:
14951483
self.h0_metatree_prob_vec = None
14961484
elif h0_metatree_prob_vec is not None:
1497-
self.h0_metatree_prob_vec = np.copy(
1485+
self.h0_metatree_prob_vec = np.array(
14981486
_check.float_vec_sum_1(
14991487
h0_metatree_prob_vec,
15001488
'h0_metatree_prob_vec',
@@ -1614,7 +1602,7 @@ def set_hn_params(self,
16141602
for i in range(len(self.hn_metatree_list)):
16151603
self._set_hn_params_recursion(self.hn_metatree_list[i],hn_metatree_list[i])
16161604
if hn_metatree_prob_vec is not None:
1617-
self.hn_metatree_prob_vec = np.copy(
1605+
self.hn_metatree_prob_vec = np.array(
16181606
_check.float_vec_sum_1(
16191607
hn_metatree_prob_vec,
16201608
'hn_metatree_prob_vec',
@@ -1628,7 +1616,7 @@ def set_hn_params(self,
16281616
else:
16291617
self.hn_metatree_prob_vec = None
16301618
elif hn_metatree_prob_vec is not None:
1631-
self.hn_metatree_prob_vec = np.copy(
1619+
self.hn_metatree_prob_vec = np.array(
16321620
_check.float_vec_sum_1(
16331621
hn_metatree_prob_vec,
16341622
'hn_metatree_prob_vec',
@@ -1676,7 +1664,7 @@ def get_hn_params(self):
16761664
def _copy_tree_from_sklearn_tree(self,new_node:_Node, original_tree,node_id):
16771665
if original_tree.children_left[node_id] != sklearn_tree._tree.TREE_LEAF: # inner node
16781666
new_node.k = original_tree.feature[node_id]
1679-
child_k_candidates = new_node.k_candidates + []
1667+
child_k_candidates = new_node.k_candidates.copy()
16801668
child_k_candidates.remove(new_node.k)
16811669
new_node.children[0] = _Node(
16821670
new_node.depth+1,
@@ -1857,7 +1845,7 @@ def _map_recursion_add_nodes(self,node:_Node):
18571845
node.map_leaf = True
18581846
else: # inner node
18591847
node.k = node.k_candidates[0]
1860-
child_k_candidates = node.k_candidates + []
1848+
child_k_candidates = node.k_candidates.copy()
18611849
child_k_candidates.remove(node.k)
18621850
for i in range(self.c_num_children):
18631851
node.children[i] = _Node(
@@ -1896,7 +1884,7 @@ def _copy_map_tree_recursion(self,copyed_node:_Node,original_node:_Node):
18961884
copyed_node.h_g = original_node.h_g
18971885
if original_node.map_leaf == False:
18981886
copyed_node.k = original_node.k
1899-
child_k_candidates = copyed_node.k_candidates + []
1887+
child_k_candidates = copyed_node.k_candidates.copy()
19001888
child_k_candidates.remove(copyed_node.k)
19011889
for i in range(self.c_num_children):
19021890
copyed_node.children[i] = _Node(
@@ -2026,8 +2014,7 @@ def _visualize_model_recursion_none(self,tree_graph,depth,k_candidates,ranges,no
20262014
k = k_candidates[self.hn_k_weight_vec[k_candidates].argmax()]
20272015
label_string = f'k={k}\\l'
20282016
if k < self.c_dim_continuous:
2029-
thresholds = np.empty(self.c_num_children_vec[k]+1)
2030-
thresholds[:] = np.linspace(ranges[k,0],ranges[k,1],self.c_num_children_vec[k]+1)
2017+
thresholds = np.linspace(ranges[k,0],ranges[k,1],self.c_num_children_vec[k]+1)
20312018
label_string += 'thresholds=\\l{'
20322019
for i in range(self.c_num_children_vec[k]-1):
20332020
if i == 0:
@@ -2037,7 +2024,7 @@ def _visualize_model_recursion_none(self,tree_graph,depth,k_candidates,ranges,no
20372024
label_string += '}\\l'
20382025
else:
20392026
thresholds = None
2040-
child_k_candidates = k_candidates + []
2027+
child_k_candidates = k_candidates.copy()
20412028
child_k_candidates.remove(k)
20422029
label_string += f'hn_g={self.hn_g:.2f}\\lp_v={tmp_p_v:.2f}\\lsub_params={{'
20432030

@@ -2072,8 +2059,7 @@ def _visualize_model_recursion_none(self,tree_graph,depth,k_candidates,ranges,no
20722059

20732060
if depth < self.c_max_depth and k_candidates:
20742061
for i in range(self.c_num_children_vec[k]):
2075-
child_ranges = np.empty([self.c_dim_continuous,2])
2076-
child_ranges[:] = ranges
2062+
child_ranges = np.array(ranges)
20772063
if thresholds is not None:
20782064
child_ranges[k,0] = thresholds[i]
20792065
child_ranges[k,1] = thresholds[i+1]

0 commit comments

Comments
 (0)