code: def _encode_target(X_ordinal, y_numeric, n_categories, smooth):
      """Simple Python implementation of target encoding."""
      cur_encodings = np.zeros(n_categories, dtype=np.float64)
      y_mean = np.mean(y_numeric)
  
      if smooth == "auto":
          y_variance = np.var(y_numeric)
          for c in range(n_categories):
              y_subset = y_numeric[X_ordinal == c]
              n_i = y_subset.shape[0]
  
              if n_i == 0:
                  cur_encodings[c] = y_mean
                  continue
  
              y_subset_variance = np.var(y_subset)
              m = y_subset_variance / y_variance
              lambda_ = n_i / (n_i + m)
  
              cur_encodings[c] = (
                  lambda_ * np.mean(y_subset) + (1 - lambda_) * y_mean
              )
          return cur_encodings
      else:  # float
          for c in range(n_categories):
              y_subset = y_numeric[X_ordinal == c]
              current_sum = np.sum(y_subset) + y_mean * smooth
              current_cnt = y_subset.shape[0] + smooth
              cur_encodings[c] = current_sum / current_cnt
          return cur_encodings
  
imports: import re
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal
  from sklearn.ensemble import RandomForestRegressor
  from sklearn.linear_model import Ridge
  from sklearn.model_selection import KFold, ShuffleSplit, StratifiedKFold, cross_val_score, train_test_split
  from sklearn.pipeline import make_pipeline
  from sklearn.preprocessing import KBinsDiscretizer, LabelBinarizer, LabelEncoder, TargetEncoder
label_desc: Use image node_id 1 for calling a global function with example usage: _encode_target(X_ordinal, y_numeric, n_categories, smooth) and returns: cur_encodings, cur_encodings