Skip to content

add error message when user passes decision trees #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -452,6 +452,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
83 changes: 83 additions & 0 deletions boruta/test/test_boruta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import numpy as np
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier

from boruta import BorutaPy


@pytest.mark.parametrize("tree_n,expected", [(10, 44), (100, 141)])
def test_get_tree_num(tree_n, expected):
rfc = RandomForestClassifier(max_depth=10)
bt = BorutaPy(rfc)
assert bt._get_tree_num(tree_n) == expected


@pytest.fixture(scope="module")
def Xy():
np.random.seed(42)
y = np.random.binomial(1, 0.5, 1000)
X = np.zeros((1000, 10))

z = (y - np.random.binomial(1, 0.1, 1000) +
np.random.binomial(1, 0.1, 1000))
z[z == -1] = 0
z[z == 2] = 1

# 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[:, 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)

# 5 irrelevant features
X[:, 5] = np.random.normal(0, 1, 1000)
X[:, 6] = np.random.poisson(1, 1000)
X[:, 7] = np.random.binomial(1, 0.3, 1000)
X[:, 8] = np.random.normal(0, 1, 1000)
X[:, 9] = np.random.poisson(1, 1000)

return X, y


def test_if_boruta_extracts_relevant_features(Xy):
X, y = Xy
rfc = RandomForestClassifier()
bt = BorutaPy(rfc)
bt.fit(X, y)
assert list(range(5)) == list(np.where(bt.support_)[0])


def test_if_it_works_with_dataframe_input(Xy):
X, y = Xy
X_df, y_df = pd.DataFrame(X), pd.Series(y)
bt = BorutaPy(RandomForestClassifier())
bt.fit(X_df, y_df)
assert list(range(5)) == list(np.where(bt.support_)[0])


def test_dataframe_is_returned(Xy):
X, y = Xy
X_df, y_df = pd.DataFrame(X), pd.Series(y)
rfc = RandomForestClassifier()
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
57 changes: 0 additions & 57 deletions boruta/test/unit_tests.py

This file was deleted.

7 changes: 7 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-r requirements.txt
pytest>=5.4.1

# repo maintenance tooling
black>=21.5b1
flake8>=3.9.2
isort>=5.8.0