<h3 dir="auto">Describe the bug</h3>
<p dir="auto">I'm trying to make a simple stacking and getting the cross validation score but an error raises:</p>
<p dir="auto"><code class="notranslate">NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.</code></p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sklearn.ensemble import StackingRegressor, RandomForestRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import cross_val_score
X, y = make_regression()
rf = RandomForestRegressor(n_jobs=-1, random_state=42)
rf.fit(X, y)
stack = StackingRegressor([(&quot;rf&quot;, rf)], cv=&quot;prefit&quot;)
cross_val_score(estimator=stack, X=X, y=y, scoring=&quot;neg_mean_absolute_error&quot;, cv=5, n_jobs=-1, error_score=&quot;raise&quot;)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">ensemble</span> <span class="pl-k">import</span> <span class="pl-v">StackingRegressor</span>, <span class="pl-v">RandomForestRegressor</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">datasets</span> <span class="pl-k">import</span> <span class="pl-s1">make_regression</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">model_selection</span> <span class="pl-k">import</span> <span class="pl-s1">cross_val_score</span>
<span class="pl-v">X</span>, <span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-en">make_regression</span>()
<span class="pl-s1">rf</span> <span class="pl-c1">=</span> <span class="pl-v">RandomForestRegressor</span>(<span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">42</span>)
<span class="pl-s1">rf</span>.<span class="pl-en">fit</span>(<span class="pl-v">X</span>, <span class="pl-s1">y</span>)
<span class="pl-s1">stack</span> <span class="pl-c1">=</span> <span class="pl-v">StackingRegressor</span>([(<span class="pl-s">"rf"</span>, <span class="pl-s1">rf</span>)], <span class="pl-s1">cv</span><span class="pl-c1">=</span><span class="pl-s">"prefit"</span>)
<span class="pl-en">cross_val_score</span>(<span class="pl-s1">estimator</span><span class="pl-c1">=</span><span class="pl-s1">stack</span>, <span class="pl-v">X</span><span class="pl-c1">=</span><span class="pl-v">X</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s1">y</span>, <span class="pl-s1">scoring</span><span class="pl-c1">=</span><span class="pl-s">"neg_mean_absolute_error"</span>, <span class="pl-s1">cv</span><span class="pl-c1">=</span><span class="pl-c1">5</span>, <span class="pl-s1">n_jobs</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>, <span class="pl-s1">error_score</span><span class="pl-c1">=</span><span class="pl-s">"raise"</span>)</pre></div>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">It should work fine if I am understanding everything right.</p>
<h3 dir="auto">Actual Results</h3>
<p dir="auto"><code class="notranslate">NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.</code></p>
<details>
  <summary>Full traceback</summary>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---------------------------------------------------------------------------
_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback: 
&quot;&quot;&quot;
Traceback (most recent call last):
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/externals/loky/process_executor.py&quot;, line 436, in _process_worker
  r = call_item()
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/externals/loky/process_executor.py&quot;, line 288, in __call__
  return self.fn(*self.args, **self.kwargs)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/_parallel_backends.py&quot;, line 595, in __call__
  return self.func(*args, **kwargs)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py&quot;, line 262, in __call__
  return [func(*args, **kwargs)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py&quot;, line 262, in &lt;listcomp&gt;
  return [func(*args, **kwargs)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/utils/fixes.py&quot;, line 117, in __call__
  return self.function(*args, **kwargs)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py&quot;, line 686, in _fit_and_score
  estimator.fit(X_train, y_train, **fit_params)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/ensemble/_stacking.py&quot;, line 879, in fit
  return super().fit(X, y, sample_weight)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/ensemble/_stacking.py&quot;, line 189, in fit
  check_is_fitted(estimator)
File &quot;/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/utils/validation.py&quot;, line 1352, in check_is_fitted
  raise NotFittedError(msg % {&quot;name&quot;: type(estimator).__name__})
sklearn.exceptions.NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
&quot;&quot;&quot;

The above exception was the direct cause of the following exception:

NotFittedError                            Traceback (most recent call last)
Cell In [3], line 8
    6 rf.fit(X, y)
    7 stack = StackingRegressor([(&quot;rf&quot;, rf)], cv=&quot;prefit&quot;)
----&gt; 8 cross_val_score(estimator=stack, X=X, y=y, scoring=&quot;neg_mean_absolute_error&quot;, cv=5, n_jobs=-1, error_score=&quot;raise&quot;)

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:515, in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, error_score)
  512 # To ensure multimetric format is not supported
  513 scorer = check_scoring(estimator, scoring=scoring)
--&gt; 515 cv_results = cross_validate(
  516     estimator=estimator,
  517     X=X,
  518     y=y,
  519     groups=groups,
  520     scoring={&quot;score&quot;: scorer},
  521     cv=cv,
  522     n_jobs=n_jobs,
  523     verbose=verbose,
  524     fit_params=fit_params,
  525     pre_dispatch=pre_dispatch,
  526     error_score=error_score,
  527 )
  528 return cv_results[&quot;test_score&quot;]

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:266, in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score, return_estimator, error_score)
  263 # We clone the estimator to make sure that all the folds are
  264 # independent, and that it is pickle-able.
  265 parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch)
--&gt; 266 results = parallel(
  267     delayed(_fit_and_score)(
  268         clone(estimator),
  269         X,
  270         y,
  271         scorers,
  272         train,
  273         test,
  274         verbose,
  275         None,
  276         fit_params,
  277         return_train_score=return_train_score,
  278         return_times=True,
  279         return_estimator=return_estimator,
  280         error_score=error_score,
  281     )
  282     for train, test in cv.split(X, y, groups)
  283 )
  285 _warn_or_raise_about_fit_failures(results, error_score)
  287 # For callabe scoring, the return type is only know after calling. If the
  288 # return type is a dictionary, the error scores can now be inserted with
  289 # the correct key.

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py:1056, in Parallel.__call__(self, iterable)
 1053     self._iterating = False
 1055 with self._backend.retrieval_context():
-&gt; 1056     self.retrieve()
 1057 # Make sure that we get a last message telling us we are done
 1058 elapsed_time = time.time() - self._start_time

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py:935, in Parallel.retrieve(self)
  933 try:
  934     if getattr(self._backend, 'supports_timeout', False):
--&gt; 935         self._output.extend(job.get(timeout=self.timeout))
  936     else:
  937         self._output.extend(job.get())

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/_parallel_backends.py:542, in LokyBackend.wrap_future_result(future, timeout)
  539 &quot;&quot;&quot;Wrapper for Future.result to implement the same behaviour as
  540 AsyncResults.get from multiprocessing.&quot;&quot;&quot;
  541 try:
--&gt; 542     return future.result(timeout=timeout)
  543 except CfTimeoutError as e:
  544     raise TimeoutError from e

File ~/miniconda3/envs/skbug/lib/python3.10/concurrent/futures/_base.py:446, in Future.result(self, timeout)
  444     raise CancelledError()
  445 elif self._state == FINISHED:
--&gt; 446     return self.__get_result()
  447 else:
  448     raise TimeoutError()

File ~/miniconda3/envs/skbug/lib/python3.10/concurrent/futures/_base.py:391, in Future.__get_result(self)
  389 if self._exception:
  390     try:
--&gt; 391         raise self._exception
  392     finally:
  393         # Break a reference cycle with the exception in self._exception
  394         self = None

NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator."><pre class="notranslate"><code class="notranslate">---------------------------------------------------------------------------
_RemoteTraceback                          Traceback (most recent call last)
_RemoteTraceback: 
"""
Traceback (most recent call last):
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/externals/loky/process_executor.py", line 436, in _process_worker
  r = call_item()
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/externals/loky/process_executor.py", line 288, in __call__
  return self.fn(*self.args, **self.kwargs)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/_parallel_backends.py", line 595, in __call__
  return self.func(*args, **kwargs)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py", line 262, in __call__
  return [func(*args, **kwargs)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py", line 262, in &lt;listcomp&gt;
  return [func(*args, **kwargs)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/utils/fixes.py", line 117, in __call__
  return self.function(*args, **kwargs)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py", line 686, in _fit_and_score
  estimator.fit(X_train, y_train, **fit_params)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/ensemble/_stacking.py", line 879, in fit
  return super().fit(X, y, sample_weight)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/ensemble/_stacking.py", line 189, in fit
  check_is_fitted(estimator)
File "/home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1352, in check_is_fitted
  raise NotFittedError(msg % {"name": type(estimator).__name__})
sklearn.exceptions.NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
"""

The above exception was the direct cause of the following exception:

NotFittedError                            Traceback (most recent call last)
Cell In [3], line 8
    6 rf.fit(X, y)
    7 stack = StackingRegressor([("rf", rf)], cv="prefit")
----&gt; 8 cross_val_score(estimator=stack, X=X, y=y, scoring="neg_mean_absolute_error", cv=5, n_jobs=-1, error_score="raise")

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:515, in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, error_score)
  512 # To ensure multimetric format is not supported
  513 scorer = check_scoring(estimator, scoring=scoring)
--&gt; 515 cv_results = cross_validate(
  516     estimator=estimator,
  517     X=X,
  518     y=y,
  519     groups=groups,
  520     scoring={"score": scorer},
  521     cv=cv,
  522     n_jobs=n_jobs,
  523     verbose=verbose,
  524     fit_params=fit_params,
  525     pre_dispatch=pre_dispatch,
  526     error_score=error_score,
  527 )
  528 return cv_results["test_score"]

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/sklearn/model_selection/_validation.py:266, in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score, return_estimator, error_score)
  263 # We clone the estimator to make sure that all the folds are
  264 # independent, and that it is pickle-able.
  265 parallel = Parallel(n_jobs=n_jobs, verbose=verbose, pre_dispatch=pre_dispatch)
--&gt; 266 results = parallel(
  267     delayed(_fit_and_score)(
  268         clone(estimator),
  269         X,
  270         y,
  271         scorers,
  272         train,
  273         test,
  274         verbose,
  275         None,
  276         fit_params,
  277         return_train_score=return_train_score,
  278         return_times=True,
  279         return_estimator=return_estimator,
  280         error_score=error_score,
  281     )
  282     for train, test in cv.split(X, y, groups)
  283 )
  285 _warn_or_raise_about_fit_failures(results, error_score)
  287 # For callabe scoring, the return type is only know after calling. If the
  288 # return type is a dictionary, the error scores can now be inserted with
  289 # the correct key.

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py:1056, in Parallel.__call__(self, iterable)
 1053     self._iterating = False
 1055 with self._backend.retrieval_context():
-&gt; 1056     self.retrieve()
 1057 # Make sure that we get a last message telling us we are done
 1058 elapsed_time = time.time() - self._start_time

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/parallel.py:935, in Parallel.retrieve(self)
  933 try:
  934     if getattr(self._backend, 'supports_timeout', False):
--&gt; 935         self._output.extend(job.get(timeout=self.timeout))
  936     else:
  937         self._output.extend(job.get())

File ~/miniconda3/envs/skbug/lib/python3.10/site-packages/joblib/_parallel_backends.py:542, in LokyBackend.wrap_future_result(future, timeout)
  539 """Wrapper for Future.result to implement the same behaviour as
  540 AsyncResults.get from multiprocessing."""
  541 try:
--&gt; 542     return future.result(timeout=timeout)
  543 except CfTimeoutError as e:
  544     raise TimeoutError from e

File ~/miniconda3/envs/skbug/lib/python3.10/concurrent/futures/_base.py:446, in Future.result(self, timeout)
  444     raise CancelledError()
  445 elif self._state == FINISHED:
--&gt; 446     return self.__get_result()
  447 else:
  448     raise TimeoutError()

File ~/miniconda3/envs/skbug/lib/python3.10/concurrent/futures/_base.py:391, in Future.__get_result(self)
  389 if self._exception:
  390     try:
--&gt; 391         raise self._exception
  392     finally:
  393         # Break a reference cycle with the exception in self._exception
  394         self = None

NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
</code></pre></div>
</details>
<h3 dir="auto">Versions</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="I installed the latest nightly


System:
    python: 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]
executable: /home/guillem.garcia/miniconda3/envs/skbug/bin/python
   machine: Linux-5.15.0-47-generic-x86_64-with-glibc2.35

Python dependencies:
      sklearn: 1.2.dev0
          pip: 22.1.2
   setuptools: 63.4.1
        numpy: 1.24.0.dev0+703.gb2fbc4349
        scipy: 1.10.0.dev0
       Cython: None
       pandas: None
   matplotlib: None
       joblib: 1.1.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-742d56dc.3.20.so
        version: 0.3.20
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8"><pre class="notranslate">I installed the latest nightly


System:
    python: 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]
executable: /home/guillem.garcia/miniconda3/envs/skbug/bin/python
   machine: Linux-5.15.0-47-generic-x86_64-with-glibc2.35

Python dependencies:
      sklearn: 1.2.dev0
          pip: 22.1.2
   setuptools: 63.4.1
        numpy: 1.24.0.dev0+703.gb2fbc4349
        scipy: 1.10.0.dev0
       Cython: None
       pandas: None
   matplotlib: None
       joblib: 1.1.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-742d56dc.3.20.so
        version: 0.3.20
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /home/guillem.garcia/miniconda3/envs/skbug/lib/python3.10/site-packages/scipy.libs/libopenblasp-r0-41284840.3.18.so
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8</pre></div>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">There seems to be a bug with the combination of <code class="notranslate">GridSearchCV</code> and <code class="notranslate">StackingClassifier</code> when the parameter <code class="notranslate">cv</code> of  <code class="notranslate">StackingClassifier</code> is set to 'prefit'. With this option, the estimators of the <code class="notranslate">StackingClassifier</code> should be fitted before fitting the stacked model, and only the final_estimator would then be fitted. When including the <code class="notranslate">StackingClassifier</code> within <code class="notranslate">GridSearchCV</code> however, the fact that estimators have already been fitted does not seem to be recognized.</p>
<h3 dir="auto">Steps/Code to Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import GradientBoostingClassifier, StackingClassifier
from sklearn.model_selection import GridSearchCV

# Creating toy data set
n_features = 3
n_instances = 40
train = np.random.rand(n_instances, n_features)
label = np.random.randint(0,2,n_instances)

# Declaring estimators
log_clf = LogisticRegression()
gau_clf = GaussianNB()

# Fitting estimators
log_clf.fit(train, label)
gau_clf.fit(train, label)

# Creating stacked model
estimators = [
    (&quot;log&quot;, log_clf),
    (&quot;gau&quot;, gau_clf)
]

boost = GradientBoostingClassifier()
stack = StackingClassifier(estimators=estimators,
                          final_estimator=boost,
                          cv = 'prefit')

# Creating the Grid CV
param_search = {
    'final_estimator__max_depth': [1]
    }

gridcv = GridSearchCV(stack, 
                      param_grid=param_search)

# Fitting the stack and gridcv models
stack.fit(train, label) # works fine 
gridcv.fit(train, label) # sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. 
                         # Call 'fit' with appropriate arguments before using this estimator
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">linear_model</span> <span class="pl-k">import</span> <span class="pl-v">LogisticRegression</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">naive_bayes</span> <span class="pl-k">import</span> <span class="pl-v">GaussianNB</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">ensemble</span> <span class="pl-k">import</span> <span class="pl-v">GradientBoostingClassifier</span>, <span class="pl-v">StackingClassifier</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">model_selection</span> <span class="pl-k">import</span> <span class="pl-v">GridSearchCV</span>

<span class="pl-c"># Creating toy data set</span>
<span class="pl-s1">n_features</span> <span class="pl-c1">=</span> <span class="pl-c1">3</span>
<span class="pl-s1">n_instances</span> <span class="pl-c1">=</span> <span class="pl-c1">40</span>
<span class="pl-s1">train</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-s1">n_instances</span>, <span class="pl-s1">n_features</span>)
<span class="pl-s1">label</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">0</span>,<span class="pl-c1">2</span>,<span class="pl-s1">n_instances</span>)

<span class="pl-c"># Declaring estimators</span>
<span class="pl-s1">log_clf</span> <span class="pl-c1">=</span> <span class="pl-v">LogisticRegression</span>()
<span class="pl-s1">gau_clf</span> <span class="pl-c1">=</span> <span class="pl-v">GaussianNB</span>()

<span class="pl-c"># Fitting estimators</span>
<span class="pl-s1">log_clf</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>, <span class="pl-s1">label</span>)
<span class="pl-s1">gau_clf</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>, <span class="pl-s1">label</span>)

<span class="pl-c"># Creating stacked model</span>
<span class="pl-s1">estimators</span> <span class="pl-c1">=</span> [
    (<span class="pl-s">"log"</span>, <span class="pl-s1">log_clf</span>),
    (<span class="pl-s">"gau"</span>, <span class="pl-s1">gau_clf</span>)
]

<span class="pl-s1">boost</span> <span class="pl-c1">=</span> <span class="pl-v">GradientBoostingClassifier</span>()
<span class="pl-s1">stack</span> <span class="pl-c1">=</span> <span class="pl-v">StackingClassifier</span>(<span class="pl-s1">estimators</span><span class="pl-c1">=</span><span class="pl-s1">estimators</span>,
                          <span class="pl-s1">final_estimator</span><span class="pl-c1">=</span><span class="pl-s1">boost</span>,
                          <span class="pl-s1">cv</span> <span class="pl-c1">=</span> <span class="pl-s">'prefit'</span>)

<span class="pl-c"># Creating the Grid CV</span>
<span class="pl-s1">param_search</span> <span class="pl-c1">=</span> {
    <span class="pl-s">'final_estimator__max_depth'</span>: [<span class="pl-c1">1</span>]
    }

<span class="pl-s1">gridcv</span> <span class="pl-c1">=</span> <span class="pl-v">GridSearchCV</span>(<span class="pl-s1">stack</span>, 
                      <span class="pl-s1">param_grid</span><span class="pl-c1">=</span><span class="pl-s1">param_search</span>)

<span class="pl-c"># Fitting the stack and gridcv models</span>
<span class="pl-s1">stack</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>, <span class="pl-s1">label</span>) <span class="pl-c"># works fine </span>
<span class="pl-s1">gridcv</span>.<span class="pl-en">fit</span>(<span class="pl-s1">train</span>, <span class="pl-s1">label</span>) <span class="pl-c"># sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. </span>
                         <span class="pl-c"># Call 'fit' with appropriate arguments before using this estimator</span></pre></div>
<h3 dir="auto">Expected Results</h3>
<p dir="auto">In the above code, the <code class="notranslate">stack</code> model works fine and no error related to the estimators' previous fitting is thrown.</p>
<p dir="auto">However, when included in the GridSearchCV, the fact that estimator models have been fitted already does not seem to be recognized and a <code class="notranslate">sklearn.exceptions.NotFittedError</code> error is prompted.</p>
<h3 dir="auto">Actual Results</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;c:\Users\levesque\Documents\Python\AMEX\errorExample.py&quot;, line 39, in &lt;module&gt;
    gridcv.fit(train, label) # sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate
arguments before using this estimator
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py&quot;, line 875, in fit
    self._run_search(evaluate_candidates)
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py&quot;, line 1379, in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid))
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py&quot;, line 852, in evaluate_candidates
    _warn_or_raise_about_fit_failures(out, self.error_score)
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_validation.py&quot;, line 367, in _warn_or_raise_about_fit_failures
    raise ValueError(all_fits_failed_message)
ValueError:
All the 5 fits failed.
It is very likely that your model is misconfigured.
You can try to debug the error by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
5 fits failed with the following error:
Traceback (most recent call last):
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_validation.py&quot;, line 686, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\ensemble\_stacking.py&quot;, line 584, in fit
    return super().fit(X, self._le.transform(y), sample_weight)
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\ensemble\_stacking.py&quot;, line 183, in fit
    check_is_fitted(estimator)
  File &quot;C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\utils\validation.py&quot;, line 1345, in check_is_fitted
    raise NotFittedError(msg % {&quot;name&quot;: type(estimator).__name__})
sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator."><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "c:\Users\levesque\Documents\Python\AMEX\errorExample.py", line 39, in &lt;module&gt;
    gridcv.fit(train, label) # sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate
arguments before using this estimator
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py", line 875, in fit
    self._run_search(evaluate_candidates)
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py", line 1379, in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid))
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_search.py", line 852, in evaluate_candidates
    _warn_or_raise_about_fit_failures(out, self.error_score)
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_validation.py", line 367, in _warn_or_raise_about_fit_failures
    raise ValueError(all_fits_failed_message)
ValueError:
All the 5 fits failed.
It is very likely that your model is misconfigured.
You can try to debug the error by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
5 fits failed with the following error:
Traceback (most recent call last):
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\model_selection\_validation.py", line 686, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\ensemble\_stacking.py", line 584, in fit
    return super().fit(X, self._le.transform(y), sample_weight)
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\ensemble\_stacking.py", line 183, in fit
    check_is_fitted(estimator)
  File "C:\Users\levesque\Documents\Python\AMEX\env\lib\site-packages\sklearn\utils\validation.py", line 1345, in check_is_fitted
    raise NotFittedError(msg % {"name": type(estimator).__name__})
sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
</code></pre></div>
<h3 dir="auto">Versions</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="System:
    python: 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
executable: C:\Users\levesque\Documents\Python\AMEX\env\Scripts\python.exe
   machine: Windows-10-10.0.17763-SP0

Python dependencies:
      sklearn: 1.1.2
          pip: 22.2.2
   setuptools: 57.4.0
        numpy: 1.23.1
        scipy: 1.9.0
       Cython: None
       pandas: 1.4.3
   matplotlib: 3.5.2
       joblib: 1.1.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:\Users\levesque\Documents\Python\AMEX\env\Lib\site-packages\sklearn\.libs\vcomp140.dll
        version: None
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\Users\levesque\Documents\Python\AMEX\env\Lib\site-packages\numpy\.libs\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll
        version: 0.3.20
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:\Users\levesque\Documents\Python\AMEX\env\Lib\site-packages\scipy\.libs\libopenblas.PZA5WNOTOH6FZLB2KBVKAURAKVTFSNNU.gfortran-win_amd64.dll
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8"><pre class="notranslate">System:
    python: 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
executable: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\l</span>evesque<span class="pl-cce">\D</span>ocuments<span class="pl-cce">\P</span>ython<span class="pl-cce">\A</span>MEX<span class="pl-cce">\e</span>nv<span class="pl-cce">\S</span>cripts<span class="pl-cce">\p</span>ython.exe
   machine: Windows-10-10.0.17763-SP0

Python dependencies:
      sklearn: 1.1.2
          pip: 22.2.2
   setuptools: 57.4.0
        numpy: 1.23.1
        scipy: 1.9.0
       Cython: None
       pandas: 1.4.3
   matplotlib: 3.5.2
       joblib: 1.1.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
         prefix: vcomp
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\l</span>evesque<span class="pl-cce">\D</span>ocuments<span class="pl-cce">\P</span>ython<span class="pl-cce">\A</span>MEX<span class="pl-cce">\e</span>nv<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\s</span>klearn<span class="pl-cce">\.</span>libs<span class="pl-cce">\v</span>comp140.dll
        version: None
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\l</span>evesque<span class="pl-cce">\D</span>ocuments<span class="pl-cce">\P</span>ython<span class="pl-cce">\A</span>MEX<span class="pl-cce">\e</span>nv<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\n</span>umpy<span class="pl-cce">\.</span>libs<span class="pl-cce">\l</span>ibopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll
        version: 0.3.20
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8

       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: C:<span class="pl-cce">\U</span>sers<span class="pl-cce">\l</span>evesque<span class="pl-cce">\D</span>ocuments<span class="pl-cce">\P</span>ython<span class="pl-cce">\A</span>MEX<span class="pl-cce">\e</span>nv<span class="pl-cce">\L</span>ib<span class="pl-cce">\s</span>ite-packages<span class="pl-cce">\s</span>cipy<span class="pl-cce">\.</span>libs<span class="pl-cce">\l</span>ibopenblas.PZA5WNOTOH6FZLB2KBVKAURAKVTFSNNU.gfortran-win_amd64.dll
        version: 0.3.18
threading_layer: pthreads
   architecture: Haswell
    num_threads: 8</pre></div>