Skip to content

Commit

Permalink
ENH Adds n_feature_in_ checking to tree (scikit-learn#18726)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjpfan authored Nov 2, 2020
1 parent 1ee4561 commit 4d7e611
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ def test_search_cv(estimator, check, request):
'random_projection',
'semi_supervised',
'svm',
'tree',
}

N_FEATURES_IN_AFTER_FIT_ESTIMATORS = [
Expand Down
16 changes: 6 additions & 10 deletions sklearn/tree/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from ..base import is_classifier
from ..base import MultiOutputMixin
from ..utils import Bunch
from ..utils import check_array
from ..utils import check_random_state
from ..utils.validation import _check_sample_weight
from ..utils import compute_sample_weight
Expand Down Expand Up @@ -174,6 +173,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,

# Determine output settings
n_samples, self.n_features_ = X.shape
self.n_features_in_ = self.n_features_
is_classification = is_classifier(self)

y = np.atleast_1d(y)
Expand Down Expand Up @@ -394,19 +394,15 @@ def fit(self, X, y, sample_weight=None, check_input=True,
def _validate_X_predict(self, X, check_input):
"""Validate the training data on predict (probabilities)."""
if check_input:
X = check_array(X, dtype=DTYPE, accept_sparse="csr")
X = self._validate_data(X, dtype=DTYPE, accept_sparse="csr",
reset=False)
if issparse(X) and (X.indices.dtype != np.intc or
X.indptr.dtype != np.intc):
raise ValueError("No support for np.int64 index based "
"sparse matrices")

n_features = X.shape[1]
if self.n_features_ != n_features:
raise ValueError("Number of features of the model must "
"match the input. Model n_features is %s and "
"input n_features is %s "
% (self.n_features_, n_features))

else:
# The number of features is checked regardless of `check_input`
self._check_n_features(X, reset=False)
return X

def predict(self, X, check_input=True):
Expand Down

0 comments on commit 4d7e611

Please sign in to comment.