MCPcopy
hub / github.com/scikit-learn/scikit-learn / _check_n_features

Function _check_n_features

sklearn/utils/validation.py:2868–2926  ·  view source on GitHub ↗

Set the `n_features_in_` attribute, or check against it on an estimator. .. note:: To only check n_features without conducting a full data validation, prefer using `validate_data(..., skip_check_array=True)` if possible. .. versionchanged:: 1.6 Moved from :class:`~s

(estimator, X, reset)

Source from the content-addressed store, hash-verified

2866
2867
2868def _check_n_features(estimator, X, reset):
2869 """Set the `n_features_in_` attribute, or check against it on an estimator.
2870
2871 .. note::
2872 To only check n_features without conducting a full data validation, prefer
2873 using `validate_data(..., skip_check_array=True)` if possible.
2874
2875 .. versionchanged:: 1.6
2876 Moved from :class:`~sklearn.base.BaseEstimator` to
2877 :mod:`~sklearn.utils.validation`.
2878
2879 Parameters
2880 ----------
2881 estimator : estimator instance
2882 The estimator to validate the input for.
2883
2884 X : {ndarray, sparse matrix} of shape (n_samples, n_features)
2885 The input samples.
2886
2887 reset : bool
2888 Whether to reset the `n_features_in_` attribute.
2889 If True, the `n_features_in_` attribute is set to `X.shape[1]`.
2890 If False and the attribute exists, then check that it is equal to
2891 `X.shape[1]`. If False and the attribute does *not* exist, then
2892 the check is skipped.
2893
2894 .. note::
2895 It is recommended to call `reset=True` in `fit` and in the first
2896 call to `partial_fit`. All other methods that validate `X`
2897 should set `reset=False`.
2898 """
2899 try:
2900 n_features = _num_features(X)
2901 except TypeError as e:
2902 if not reset and hasattr(estimator, "n_features_in_"):
2903 raise ValueError(
2904 "X does not contain any features, but "
2905 f"{estimator.__class__.__name__} is expecting "
2906 f"{estimator.n_features_in_} features"
2907 ) from e
2908 # If the number of features is not defined and reset=True,
2909 # then we skip this check
2910 return
2911
2912 if reset:
2913 estimator.n_features_in_ = n_features
2914 return
2915
2916 if not hasattr(estimator, "n_features_in_"):
2917 # Skip this check if the expected number of expected input features
2918 # was not recorded by calling fit first. This is typically the case
2919 # for stateless transformers.
2920 return
2921
2922 if n_features != estimator.n_features_in_:
2923 raise ValueError(
2924 f"X has {n_features} features, but {estimator.__class__.__name__} "
2925 f"is expecting {estimator.n_features_in_} features as input."

Callers 7

_joint_log_likelihoodMethod · 0.90
transformMethod · 0.90
_fitMethod · 0.90
_validate_X_predictMethod · 0.90
validate_dataFunction · 0.85

Calls 1

_num_featuresFunction · 0.85

Tested by 2

Used in the wild real call sites across dependent graphs

searching dependent graphs…