class_docstr: Abstract class with common methods to be inherited by WeakSegmentsPerformance checks.
class_docstr_tok: ["Abstract","class","with","common","methods","to","be","inherited","by","WeakSegmentsPerformance","checks","."]
code: def _target_encode_categorical_features_fill_na(
      self,
      data: pd.DataFrame,
      label_col: pd.Series,
      cat_features: List[str],
      is_cat_label: bool = True,
  ) -> Dataset:
      values_mapping = defaultdict(
          list
      )  # mapping of per feature of original values to their encoded value
      label_col = pd.Series(label_col, index=data.index)
      df_aggregated = default_fill_na_per_column_type(
          data, cat_features
      )
      cat_features = [
          col for col in cat_features if col in df_aggregated.columns
      ]
      # Merging small categories into Other
      for col in cat_features:
          categories_to_mask = [
              k
              for k, v in df_aggregated[col].value_counts().items()
              if v / data.shape[0]
              < self.categorical_aggregation_threshold
          ]
          df_aggregated.loc[
              np.isin(df_aggregated[col], categories_to_mask), col
          ] = "Other"
  
      # Target encoding of categorical features based on label col (when unavailable we use loss_per_sample)
      if len(cat_features) > 0:
          t_encoder = TargetEncoder(cols=cat_features)
          if is_cat_label:
              label_no_none = label_col.astype("object").fillna("None")
              label_as_int = pd.Categorical(
                  label_no_none,
                  categories=sorted(label_no_none.unique()),
              ).codes
          else:
              label_as_int = pd.cut(
                  label_col.astype("float64").fillna(label_col.mean()),
                  bins=10,
                  labels=False,
              )
          df_encoded = t_encoder.fit_transform(
              df_aggregated,
              pd.Series(label_as_int, index=df_aggregated.index),
          )
          # Convert categorical features to ordinal based on their encoded values and store the mapping
          for col in cat_features:
              df_encoded[col] = df_encoded[col].apply(
                  sorted(df_encoded[col].unique()).index
              )
              mapping = pd.concat(
                  [df_encoded[col], df_aggregated[col]], axis=1
              ).drop_duplicates()
              mapping.columns = ["encoded_value", "original_category"]
              values_mapping[col] = mapping.sort_values(
                  by="encoded_value"
              )
      else:
          df_encoded = df_aggregated
      self.encoder_mapping = values_mapping
      return Dataset(
          df_encoded, cat_features=cat_features, label=label_col
      )
  
imports: import abc
  from collections import defaultdict
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import pandas
  import plotly.express
  import sklearn
  from category_encoders import TargetEncoder
  from packaging import version
  from sklearn.model_selection import GridSearchCV
  from sklearn.tree import DecisionTreeRegressor
  from deepchecks import ConditionCategory, ConditionResult
  from deepchecks.tabular import Dataset
  from deepchecks.tabular.context import _DummyModel
  from deepchecks.tabular.metric_utils.scorers import DeepcheckScorer
  from deepchecks.utils.dataframes import default_fill_na_per_column_type
  from deepchecks.utils.performance.partition import DeepchecksFilter, convert_tree_leaves_into_filters, partition_numeric_feature_around_segment
  from deepchecks.utils.strings import format_number, format_percent
label_desc: Use image node_id 1 for calling the WeakSegmentAbstract obj's underlying member method code with example usage: obj._target_encode_categorical_features_fill_na(data, label_col, cat_features, is_cat_label) and returns: Dataset