title: Sklearn FitFailedWarning: Estimator fit failed. ValueError: kth(=-9) out of bounds (1)
body: <p>I am currently working through a sample tutorial on the california-housing-prices dataste and I am facing this issue when trying to do a grid search for data preparation choices.</p>
  <p>Code</p>
  <pre><code>num_pipeline = Pipeline([
      ('imputer', SimpleImputer(strategy=&quot;median&quot;)),
      ('attribs_adder', CombinedAttributesAdder()),
      ('std_scaler', StandardScaler()),
  ])
  
  full_pipeline = ColumnTransformer([
      (&quot;num&quot;, num_pipeline, num_attribs),
      (&quot;cat&quot;, OneHotEncoder(), cat_attribs)
  ])
  
  def indices_of_top_k(arr, k):
      return np.sort(np.argpartition(np.array(arr), -k)[-k:])
  
  class TopFeatureSelector(BaseEstimator, TransformerMixin):
      def __init__(self, feature_importances, k):
          self.ft_imp = feature_importances
          self.k = k
      def fit(self, X, y=None):
          self.feature_indices_ = indices_of_top_k(self.ft_imp, k)
          return self
      def transform(self, X):
          return X[:, self.feature_indices_]
  
  prepare_select_and_predict_pipeline = Pipeline([
      ('preparation', full_pipeline),
      ('feature_selection', TopFeatureSelector(feature_importances, k)),
      ('forest_reg', RandomForestRegressor(**grid_search.best_params_))
  ])
  
  param_grid = {
      'preparation__num__imputer__strategy': ['mean', 'median'],
      'feature_selection__k': list(range(1, len(feature_importances) + 1))
  }
  
  grid_search_prep = GridSearchCV(prepare_select_and_predict_pipeline, param_grid, cv=5, scoring='neg_mean_squared_error', verbose=1)
  
  grid_search_prep.fit(housing, housing_labels)
  </code></pre>
  <p>Error</p>
  <pre><code>/opt/conda/lib/python3.7/site-packages/sklearn/base.py:213: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.
    FutureWarning)
  [Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
  /opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_validation.py:552: FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details: 
  Traceback (most recent call last):
    File &quot;/opt/conda/lib/python3.7/site-packages/sklearn/model_selection/_validation.py&quot;, line 531, in _fit_and_score
      estimator.fit(X_train, y_train, **fit_params)
    File &quot;/opt/conda/lib/python3.7/site-packages/sklearn/pipeline.py&quot;, line 330, in fit
      Xt = self._fit(X, y, **fit_params_steps)
    File &quot;/opt/conda/lib/python3.7/site-packages/sklearn/pipeline.py&quot;, line 296, in _fit
      **fit_params_steps[name])
    File &quot;/opt/conda/lib/python3.7/site-packages/joblib/memory.py&quot;, line 352, in __call__
      return self.func(*args, **kwargs)
    File &quot;/opt/conda/lib/python3.7/site-packages/sklearn/pipeline.py&quot;, line 740, in _fit_transform_one
      res = transformer.fit_transform(X, y, **fit_params)
    File &quot;/opt/conda/lib/python3.7/site-packages/sklearn/base.py&quot;, line 693, in fit_transform
      return self.fit(X, y, **fit_params).transform(X)
    File &quot;&lt;ipython-input-129-8ca16960996c&gt;&quot;, line 11, in fit
      self.feature_indices_ = indices_of_top_k(self.ft_imp, k)
    File &quot;&lt;ipython-input-129-8ca16960996c&gt;&quot;, line 4, in indices_of_top_k
      return np.sort(np.argpartition(np.array(arr), -k)[-k:])
    File &quot;&lt;__array_function__ internals&gt;&quot;, line 6, in argpartition
    File &quot;/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py&quot;, line 832, in argpartition
      return _wrapfunc(a, 'argpartition', kth, axis=axis, kind=kind, order=order)
    File &quot;/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py&quot;, line 58, in _wrapfunc
      return bound(*args, **kwds)
  ValueError: kth(=-9) out of bounds (1)
  
  ...
  
    FitFailedWarning)
  /opt/conda/lib/python3.7/site-packages/sklearn/base.py:213: FutureWarning: From version 0.24, get_params will raise an AttributeError if a parameter cannot be retrieved as an instance attribute. Previously it would return None.
    FutureWarning)
  </code></pre>
  <p>Data</p>
  <p>Housing is a (16512, 9) dataframe
  Housing labels is a (16512,) series</p>
  <p>Any idea why this error is happening?</p>
  <p>EDIT: feature_importances definition</p>
  <pre><code>param_grid = [
      {'n_estimators': [3,10,30], 'max_features': [2,4,6,8,10]},
      {'bootstrap':[False], 'n_estimators': [3,10], 'max_features':[2,3,4]},
  ]
  
  forest_reg = RandomForestRegressor()
  
  grid_search = GridSearchCV(forest_reg, param_grid, cv=5, scoring = 'neg_mean_squared_error', return_train_score=True)
  
  grid_search.fit(housing_prepared, housing_labels)
  
  feature_importances = grid_search.best_estimator_.feature_importances_
  </code></pre>