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
15 changes: 13 additions & 2 deletions boruta/boruta_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def _fit(self, X, y):

# set n_estimators
if self.n_estimators != 'auto':
self.estimator.set_params(n_estimators=self.n_estimators)
self._set_n_estimators(self.n_estimators)

# main feature selection loop
while np.any(dec_reg == 0) and _iter < self.max_iter:
Expand All @@ -335,7 +335,7 @@ def _fit(self, X, y):
# number of features that aren't rejected
not_rejected = np.where(dec_reg >= 0)[0].shape[0]
n_tree = self._get_tree_num(not_rejected)
self.estimator.set_params(n_estimators=n_tree)
self._set_n_estimators(n_estimators=n_tree)

# make sure we start with a new tree in each iteration
if self._is_lightgbm:
Expand Down Expand Up @@ -454,6 +454,17 @@ def _transform(self, X, weak=False, return_df=False):
X = X[:, indices]
return X

def _set_n_estimators(self, n_estimators):
try:
self.estimator.set_params(n_estimators=n_estimators)
except ValueError:
raise ValueError(
f"The estimator {self.estimator} does not take the parameter "
"n_estimators. Use Random Forests or gradient boosting machines "
"instead."
)
return self

def _get_tree_num(self, n_feat):
depth = None
try:
Expand Down
20 changes: 18 additions & 2 deletions boruta/test/test_boruta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier

from boruta import BorutaPy

Expand All @@ -26,8 +27,8 @@ def Xy():

# 5 relevant features
X[:, 0] = z
X[:, 1] = (y * np.abs(np.random.normal(0, 1, 1000))
+ np.random.normal(0, 0.1, 1000))
X[:, 1] = (y * np.abs(np.random.normal(0, 1, 1000)) +
np.random.normal(0, 0.1, 1000))
X[:, 2] = y + np.random.normal(0, 1, 1000)
X[:, 3] = y**2 + np.random.normal(0, 1, 1000)
X[:, 4] = np.sqrt(y) + np.random.binomial(2, 0.1, 1000)
Expand Down Expand Up @@ -65,3 +66,18 @@ def test_dataframe_is_returned(Xy):
bt = BorutaPy(rfc)
bt.fit(X_df, y_df)
assert isinstance(bt.transform(X_df, return_df=True), pd.DataFrame)


@pytest.mark.parametrize("tree", [ExtraTreeClassifier(), DecisionTreeClassifier()])
def test_boruta_with_decision_trees(tree, Xy):
msg = (
f"The estimator {tree} does not take the parameter "
"n_estimators. Use Random Forests or gradient boosting machines "
"instead."
)
X, y = Xy
bt = BorutaPy(tree)
with pytest.raises(ValueError) as record:
bt.fit(X, y)

assert str(record.value) == msg