Thanks to visit codestin.com
Credit goes to github.com

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
28 changes: 26 additions & 2 deletions sklearn/multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ class OneVsRestClassifier(

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n_features_in_ without ticks? See #20818 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the ticks sphinx will give a warning: " WARNING: Unknown target name: "n_features_in"." because rst is considering it a Hyperlink Target

Names of features seen during :term:`fit`. Only defined if the
underlying estimator exposes such an attribute when fit.

.. versionadded:: 1.0

Examples
--------
>>> import numpy as np
Expand Down Expand Up @@ -342,6 +348,8 @@ def fit(self, X, y):

if hasattr(self.estimators_[0], "n_features_in_"):
self.n_features_in_ = self.estimators_[0].n_features_in_
if hasattr(self.estimators_[0], "feature_names_in_"):
self.feature_names_in_ = self.estimators_[0].feature_names_in_

return self

Expand Down Expand Up @@ -664,6 +672,12 @@ class OneVsOneClassifier(MetaEstimatorMixin, ClassifierMixin, BaseEstimator):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.

.. versionadded:: 1.0

Examples
--------
>>> from sklearn.datasets import load_iris
Expand Down Expand Up @@ -762,7 +776,8 @@ def partial_fit(self, X, y, classes=None):
-------
self
"""
if _check_partial_fit_first_call(self, classes):
first_call = _check_partial_fit_first_call(self, classes)
if first_call:
self.estimators_ = [
clone(self.estimator)
for _ in range(self.n_classes_ * (self.n_classes_ - 1) // 2)
Expand All @@ -780,7 +795,7 @@ def partial_fit(self, X, y, classes=None):
y,
accept_sparse=["csr", "csc"],
force_all_finite=False,
reset=_check_partial_fit_first_call(self, classes),
reset=first_call,
)
check_classification_targets(y)
combinations = itertools.combinations(range(self.n_classes_), 2)
Expand Down Expand Up @@ -842,6 +857,7 @@ def decision_function(self, X):
scikit-learn conventions for binary classification.
"""
check_is_fitted(self)
self._check_feature_names(X, reset=False)

indices = self.pairwise_indices_
if indices is None:
Expand Down Expand Up @@ -936,6 +952,12 @@ class OutputCodeClassifier(MetaEstimatorMixin, ClassifierMixin, BaseEstimator):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimator exposes such an attribute when fit.

.. versionadded:: 1.0

Examples
--------
>>> from sklearn.multiclass import OutputCodeClassifier
Expand Down Expand Up @@ -1032,6 +1054,8 @@ def fit(self, X, y):

if hasattr(self.estimators_[0], "n_features_in_"):
self.n_features_in_ = self.estimators_[0].n_features_in_
if hasattr(self.estimators_[0], "feature_names_in_"):
self.feature_names_in_ = self.estimators_[0].feature_names_in_

return self

Expand Down
11 changes: 11 additions & 0 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class Pipeline(_BaseComposition):

.. versionadded:: 0.24

feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Only defined if the
underlying estimator exposes such an attribute when fit.

.. versionadded:: 1.0

See Also
--------
make_pipeline : Convenience function for simplified pipeline construction.
Expand Down Expand Up @@ -672,6 +678,11 @@ def n_features_in_(self):
# delegate to first step (which will call _check_is_fitted)
return self.steps[0][1].n_features_in_

@property
def feature_names_in_(self):
# delegate to first step (which will call _check_is_fitted)
return self.steps[0][1].feature_names_in_

def __sklearn_is_fitted__(self):
"""Indicate whether pipeline has been fit."""
try:
Expand Down
10 changes: 6 additions & 4 deletions sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
import pkgutil
from inspect import isgenerator, signature
from itertools import product
from itertools import product, chain
from functools import partial

import pytest
Expand Down Expand Up @@ -329,16 +329,18 @@ def test_check_n_features_in_after_fitting(estimator):
"feature_extraction",
"kernel_approximation",
"model_selection",
"multiclass",
"multioutput",
"pipeline",
"semi_supervised",
}

_estimators_to_test = list(
chain(_tested_estimators(), [make_pipeline(LogisticRegression(C=1))])
)


column_name_estimators = [
est
for est in _tested_estimators()
for est in _estimators_to_test
if est.__module__.split(".")[1] not in COLUMN_NAME_MODULES_TO_IGNORE
]

Expand Down