code: def salsa_train_and_validate(
      X_tr,
      Y_tr,
      X_va,
      Y_va,
      kernel_type,
      add_order,
      kernel_scale,
      bandwidths,
      l2_reg,
      num_tr_data_to_use=None,
      num_va_data_to_use=None,
      shuffle_data_when_using_a_subset=True,
  ):
      """Train and return validation error for SALSA."""
      # pylint: disable=too-many-arguments
      # Prelims
      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]
      # Get estimator
      salsa_estimator = get_salsa_estimator_from_data_and_hyperparams(
          X_tr,
          Y_tr,
          kernel_type,
          add_order,
          kernel_scale,
          bandwidths,
          l2_reg,
      )
      valid_predictions = salsa_estimator(X_va)
      valid_diffs = (valid_predictions - Y_va) ** 2
      avg_valid_err = valid_diffs.mean()
      print(
          "Trained with %d data, and validated with %d data. err=%0.4f"
          % (len(X_tr), len(X_va), avg_valid_err)
      )
      return avg_valid_err
  
imports: from __future__ import print_function
  import numpy
  from scipy.linalg import solve_triangular
label_desc: Use image node_id 9 for calling a global function with example usage: salsa_train_and_validate(X_tr, Y_tr, X_va, Y_va, kernel_type, add_order, kernel_scale, bandwidths, l2_reg, num_tr_data_to_use, num_va_data_to_use, shuffle_data_when_using_a_subset) and returns: avg_valid_err