title: “UserWarning: One or more of the test scores are non-finite” warning only when adding RandomForest max_features parameter to RandomizedSearchCV
body: <pre><code>from sklearn.model_selection import RandomizedSearchCV
  
  # --initialise classifier
  classifier = RandomForestClassifier(n_estimators=300)
  
  # -- set hyperparameters to tune
  param_grid = {
     &quot;max_depth&quot;: np.arange(20, 60, 10),
     &quot;min_samples_leaf&quot;: np.arange(1, 15),
     'max_features': np.arange(0, 1, 0.05),
  }
  
  random = np.random.RandomState(42)
  
  # -- initialise grid search
  random_model_search = RandomizedSearchCV(
      estimator=classifier,
      param_distributions=param_grid,
      n_iter=100,
      scoring=&quot;f1&quot;,
      return_train_score=True,
      n_jobs=-1,
      cv=3,
      random_state=random
  )
  
  # -- fit the model and extract best score
  random_model_search.fit(X_train_encoded, Y_train)
  print(f&quot;Best score: {random_model_search.best_score_}&quot;)
  
  print(&quot;Best parameters set:&quot;)
  best_parameters_random = random_model_search.best_estimator_.get_params()
  for param_name in sorted(param_grid.keys()):
      print(f&quot;\t{param_name}: {best_parameters_random[param_name]}&quot;)
  </code></pre>
  <p>When I run this code with <code>max_depth</code> in <code>param_grid</code>, I get a UserWarning saying some test scores are nan values. However, if I take this hyperparameter out, random search runs just fine, no warning. I understand that this warning comes up when there's categories in the validation/test set that are not present in the training set and so have not been properly encoded. I am using the train set for this random search with the whole of the train set encoded, so I'm not sure why this warning comes up? Can anyone advise on this?</p>
  <p>The encoding and scaling code is below:</p>
  <pre><code># Set encoding and scaling instructions
  column_transform = make_column_transformer(
      (OneHotEncoder(handle_unknown = &quot;ignore&quot;), columns_for_onehot),
      (OrdinalEncoder(categories=[ordinal_order], handle_unknown='use_encoded_value', unknown_value=3), columns_for_ordinal),
      remainder=MinMaxScaler()
  )
  
  # Apply column transformer to features
  X_train_encoded = column_transform.fit_transform(X_train)
  </code></pre>