code: def skltree_train_and_validate(
      method,
      X_tr,
      Y_tr,
      X_va,
      Y_va,
      hyperparam_dict,
      num_tr_data_to_use=None,
      num_va_data_to_use=None,
      shuffle_data_when_using_a_subset=False,
      error_or_accuracy="accuracy",
  ):
      """Inputs:
      method: should be one of gbc, gbr, rfc, rfr which stand for
        gradient boosted classification, gradient boosted regression,
        random forest classification, random forest regression respectively.
      X_tr, Y_tr, X_va, Y_va: Data.
      hyperparam_dict: dictionary of hyper-parameters.
      num_tr_data_to_use, num_va_data_to_use: how much of the training and validation
        sets to use for training.
      shuffle_data_when_using_a_subset: if True, will shuffle dataset.
      """
      # Prelims
      if method in ["gbc", "rfc"]:
          prob_type = "classification"
      elif method in ["gbr", "rfr"]:
          prob_type = "regression"
      else:
          raise ValueError("Unknown method %s." % (method))
      if num_tr_data_to_use is None:
          num_tr_data_to_use = len(X_tr)
      if num_va_data_to_use is None:
          num_va_data_to_use = len(X_va)
      num_tr_data_to_use = int(num_tr_data_to_use)
      num_va_data_to_use = int(num_va_data_to_use)
      if (
          num_tr_data_to_use < len(X_tr)
          and shuffle_data_when_using_a_subset
      ):
          X_tr = np.copy(X_tr)
          np.random.shuffle(X_tr)
      if (
          num_va_data_to_use < len(X_va)
          and shuffle_data_when_using_a_subset
      ):
          X_va = np.copy(X_va)
          np.random.shuffle(X_va)
      # Get relevant subsets
      X_tr = X_tr[:num_tr_data_to_use]
      Y_tr = Y_tr[:num_tr_data_to_use]
      X_va = X_va[:num_va_data_to_use]
      Y_va = Y_va[:num_va_data_to_use]
      # Train method
      method_to_construct_dict = {
          "gbr": ensemble.GradientBoostingRegressor,
          "gbc": ensemble.GradientBoostingClassifier,
          "rfr": ensemble.RandomForestRegressor,
          "rfc": ensemble.RandomForestClassifier,
      }
      model_constructor = method_to_construct_dict[method]
      model = model_constructor(**hyperparam_dict)
      model.fit(X_tr, Y_tr)
      # Compute validation error
      predictions = model.predict(X_va)
      if prob_type == "classification":
          ret = _get_avg_classification_error(predictions, Y_va)
          ret = ret if error_or_accuracy == "error" else (1 - ret)
      elif prob_type == "regression":
          ret = _get_avg_regression_error(predictions, Y_va)
          ret = ret if error_or_accuracy == "error" else -ret
      # Return
      return ret
  
file_path: dragonfly/examples/tree_reg/skltree.py
imports: from __future__ import print_function
  import numpy
  from sklearn import ensemble
label_desc: Use image node_id 4 for calling a global function with example usage: skltree_train_and_validate(method, X_tr, Y_tr, X_va, Y_va, hyperparam_dict, num_tr_data_to_use, num_va_data_to_use, shuffle_data_when_using_a_subset, error_or_accuracy) and returns: ret