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","."]
docstr: Find weak segment based on scorer for specified features.
  
  In each iteration build a decision tree with a set of parameters with the goal of grouping samples with
  similar loss_per_sample values together. Then, the generated tree is values based only on the quality of
  the worst leaf in the tree (the rest are ignored). The leaf score is calculated by the scorer
  if provided, otherwise by the average score_per_sample value of the leaf.
  
  After all the iterations are done, the tree with the best score (the one with the worst leaf) is selected, and
  the worst leaf of it is extracted and returned as a deepchecks filter.
docstr_tok: ["Find","weak","segment","based","on","scorer","for","specified","features",".","In","each","iteration","build","a","decision","tree","with","a","set","of","parameters","with","the","goal","of","grouping","samples","with","similar","loss_per_sample","values","together",".","Then",",","the","generated","tree","is","values","based","only","on","the","quality","of","the","worst","leaf","in","the","tree","(","the","rest","are","ignored",")",".","The","leaf","score","is","calculated","by","the","scorer","if","provided",",","otherwise","by","the","average","score_per_sample","value","of","the","leaf",".","After","all","the","iterations","are","done",",","the","tree","with","the","best","score","(","the","one","with","the","worst","leaf",")","is","selected",",","and","the","worst","leaf","of","it","is","extracted","and","returned","as","a","deepchecks","filter","."]
code: def _find_weak_segment(
      self,
      data: pd.DataFrame,
      features_for_segment: List[str],
      score_per_sample: pd.Series,
      label_col: Optional[pd.Series] = None,
      dummy_model: Optional[_DummyModel] = None,
      scorer: Optional[DeepcheckScorer] = None,
  ) -> Tuple[Optional[float], Optional[DeepchecksFilter]]:
      """Find weak segment based on scorer for specified features.
  
      In each iteration build a decision tree with a set of parameters with the goal of grouping samples with
      similar loss_per_sample values together. Then, the generated tree is values based only on the quality of
      the worst leaf in the tree (the rest are ignored). The leaf score is calculated by the scorer
      if provided, otherwise by the average score_per_sample value of the leaf.
  
      After all the iterations are done, the tree with the best score (the one with the worst leaf) is selected, and
      the worst leaf of it is extracted and returned as a deepchecks filter.
      """
      # Remove rows with na values in the relevant columns
      data_for_search = data[features_for_segment].dropna()
      if len(data_for_search) == 0:
          return None, None
      segment_minimum_size_ratio = (
          self.segment_minimum_size_ratio
          * len(data)
          / len(data_for_search)
      )
      score_per_sample_for_search = score_per_sample.loc[
          data_for_search.index
      ]
      if label_col is not None:
          label_col_for_search = label_col.loc[data_for_search.index]
  
      if version.parse(sklearn.__version__) < version.parse("1.0.0"):
          criterion = ["mse", "mae"]
      else:
          criterion = ["squared_error", "absolute_error"]
      search_space = {
          "max_depth": [5],
          "min_weight_fraction_leaf": [segment_minimum_size_ratio],
          "min_samples_leaf": [5],
          "criterion": criterion,
          "min_impurity_decrease": [0.003],
      }
  
      # In a given tree finds the leaf with the worst score (the rest are ignored)
      def get_worst_leaf_filter(tree):
          leaves_filters = convert_tree_leaves_into_filters(
              tree, features_for_segment
          )
          min_score, min_score_leaf_filter = np.inf, None
          for leaf_filter in leaves_filters:
              if (
                  scorer is not None
                  and dummy_model is not None
                  and label_col is not None
              ):
                  leaf_data, leaf_labels = leaf_filter.filter(
                      data_for_search, label_col_for_search
                  )
                  leaf_score = scorer.run_on_data_and_label(
                      dummy_model, leaf_data, leaf_labels
                  )
              else:  # if no scorer is provided, use the average loss_per_sample of samples in the leaf as the score
                  leaf_score = score_per_sample_for_search[
                      list(leaf_filter.filter(data_for_search).index)
                  ].mean()
  
              if leaf_score < min_score:
                  min_score, min_score_leaf_filter = (
                      leaf_score,
                      leaf_filter,
                  )
          return min_score, min_score_leaf_filter
  
      def neg_worst_segment_score(
          clf: DecisionTreeRegressor, x, y
      ) -> float:  # pylint: disable=unused-argument
          return -get_worst_leaf_filter(clf.tree_)[0]
  
      if hasattr(self, "random_state"):
          random_state = self.random_state
      elif hasattr(self, "context"):
          random_state = self.context.random_state
      else:
          random_state = None
      grid_searcher = GridSearchCV(
          DecisionTreeRegressor(random_state=random_state),
          scoring=neg_worst_segment_score,
          param_grid=search_space,
          n_jobs=-1,
          cv=3,
      )
      try:
          grid_searcher.fit(
              data_for_search, score_per_sample_for_search
          )
          # Get the worst leaf filter out of the selected tree
          segment_score, segment_filter = get_worst_leaf_filter(
              grid_searcher.best_estimator_.tree_
          )
      except ValueError:
          return None, None
  
      return segment_score, segment_filter
  
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 4 for calling the WeakSegmentAbstract obj's underlying member method code with example usage: obj._find_weak_segment(data, features_for_segment, score_per_sample, label_col, dummy_model, scorer) and returns: segment_score,  segment_filter, None,  None, min_score,  min_score_leaf_filter, unknown, None,  None