Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion smarttree/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,16 @@ def tree_(self) -> Tree:
assert self._tree is not None
return self._tree

def get_n_leaves(self) -> int:
return self.tree_.leaf_counter

def get_depth(self) -> int:
return self.tree_.max_depth

@property
def feature_importances_(self) -> dict[str, float]:
self._check_is_fitted()
return self.tree_.feature_importances
return self.tree_.compute_feature_importances()

@abstractmethod
def fit(self, X: pd.DataFrame, y: pd.Series) -> Self:
Expand Down
13 changes: 13 additions & 0 deletions smarttree/_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ def create_node(
self.root = node

return node

def compute_feature_importances(self) -> dict[str, float]:

amount = 0.0
for importance in self.feature_importances.values():
amount += importance

normalized_feature_importances = dict()
for feature, importance in self.feature_importances.items():
normalized_feature_importances[feature] = importance / amount

return normalized_feature_importances

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ def test__not_fitted__method(not_fitted_tree, X, y, method_call):
)
def test__not_fitted__property(not_fitted_tree, property_name):
with pytest.raises(NotFittedError):
property_ = getattr(not_fitted_tree, property_name)
_ = property_
getattr(not_fitted_tree, property_name)
Loading