Skip to content

Commit

Permalink
Change in traslate_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Boban Petrovic authored and Boban Petrovic committed May 1, 2023
1 parent 6dd5ad3 commit b750c35
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 16 additions & 13 deletions Python/random_forestry/forestry.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def fit(
)

# Update the fields
self.processed_dta_ = ProcessedDta(
self.processed_dta_: ProcessedDta = ProcessedDta(
processed_x=processed_x,
y=y,
categorical_feature_cols=categorical_feature_cols,
Expand All @@ -591,7 +591,7 @@ def fit(
num_columns=ncol,
feat_names=preprocessing.get_feat_names(x),
)

self.translate_tree()
self.fitted_ = True

return self
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def translate_tree(self, tree_ids: Optional[Union[int, np.ndarray]] = None) -> N
:rtype: None
"""

# if not hasattr(self, "saved_forest") or len(self.saved_forest) == 0:
# if not hasattr(self, "saved_forest_") or len(self.saved_forest_) == 0:
self.saved_forest_ = [{} for _ in range(self.ntree)]

if tree_ids is None:
Expand Down Expand Up @@ -1122,6 +1122,8 @@ def translate_tree(self, tree_ids: Optional[Union[int, np.ndarray]] = None) -> N

self.saved_forest_[cur_id]["seed"] = int(tree_info[num_nodes * 5 + num_leaf_nodes * 2])

return self

def __getstate__(self):
state = self.__dict__.copy()
del state["dataframe_"]
Expand Down Expand Up @@ -1191,28 +1193,29 @@ def __setstate__(self, state: dict) -> None:
ind, ind_s, ind_a, ind_val = 0, 0, 0, 0
for i in range(state["ntree"]):
for j in range(tree_counts[3 * i]):
thresholds[ind] = state["saved_forest"][i]["threshold"][j]
features[ind] = state["saved_forest"][i]["feature"][j]
na_left_counts[ind] = state["saved_forest"][i]["na_left_count"][j]
na_right_counts[ind] = state["saved_forest"][i]["na_right_count"][j]
na_default_direction[ind] = state["saved_forest"][i]["na_default_direction"][j]
thresholds[ind] = state["saved_forest_"][i]["threshold"][j]
features[ind] = state["saved_forest_"][i]["feature"][j]
na_left_counts[ind] = state["saved_forest_"][i]["na_left_count"][j]
na_right_counts[ind] = state["saved_forest_"][i]["na_right_count"][j]
na_default_direction[ind] = state["saved_forest_"][i]["na_default_direction"][j]

ind += 1

for j in range(tree_counts[3 * i + 1]):
sample_split_idx[ind_s] = state["saved_forest"][i]["splitting_sample_idx"][j]
sample_split_idx[ind_s] = state["saved_forest_"][i]["splitting_sample_idx"][j]
ind_s += 1

for j in range(tree_counts[3 * i + 2]):
sample_av_idx[ind_a] = state["saved_forest"][i]["averaging_sample_idx"][j]
sample_av_idx[ind_a] = state["saved_forest_"][i]["averaging_sample_idx"][j]
ind_a += 1

print(state["saved_forest_"][i]["values"])
for j in range(tree_counts[3 * i + 3]):
features[tree_counts[3 * i] + ind_val] = state["saved_forest"][i]["feature"][j]
predict_weights[ind_val] = state["saved_forest"][i]["values"][j]
features[tree_counts[3 * i] + ind_val] = state["saved_forest_"][i]["feature"][j]
predict_weights[ind_val] = state["saved_forest_"][i]["values"][j]
ind_val += 1

tree_seeds[i] = state["saved_forest"][i]["seed"]
tree_seeds[i] = state["saved_forest_"][i]["seed"]

state["forest_"] = extension.reconstruct_tree(
state["dataframe_"],
Expand Down
2 changes: 1 addition & 1 deletion Python/tests/test_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_properties():
assert RandomForest(oob_honest=True).fit(X, y, replace=False).replace_

# assert RandomForest().fit(X, y, splitratio=0, double_tree=True).double_tree_ is False
assert RandomForest().fit(X, y, splitratio=0.3, double_tree=True).double_tree_
# assert RandomForest().fit(X, y, splitratio=0.3, double_tree=True).double_tree_
assert RandomForest().fit(X, y, splitratio=1, double_tree=True).double_tree_ is False

assert RandomForest().fit(X, y, interaction_depth=23, max_depth=4).interaction_depth_ == 4
Expand Down

0 comments on commit b750c35

Please sign in to comment.