Exemplo n.º 1
0
    def random_splittable_value(self, variable: int) -> Any:
        """
        Return a random value of a variable
        Useful for choosing a variable to split on

        Parameters
        ----------
        variable - str
            Name of the variable to split on

        Returns
        -------
        Any

        Notes
        -----
          - Won't create degenerate splits, all splits will have at least one row on both sides of the split
        """
        if variable not in self.splittable_variables():
            raise NoSplittableVariableException()
        max_value = self.max_value_of_column(variable)
        candidate = np.random.choice(self.get_column(variable))
        while candidate == max_value:
            candidate = np.random.choice(self.get_column(variable))
        return candidate
Exemplo n.º 2
0
def random_splittable_leaf_node(tree: Tree) -> LeafNode:
    """
    Returns a random leaf node that can be split in a non-degenerate way
    i.e. a random draw from the set of leaf nodes that have at least two distinct values in their covariate matrix
    """
    splittable_nodes = tree.splittable_leaf_nodes
    if len(splittable_nodes) == 0:
        raise NoSplittableVariableException()
    else:
        return np.random.choice(splittable_nodes)
Exemplo n.º 3
0
 def random_splittable_variable(self) -> str:
     """
     Choose a variable at random from the set of splittable variables
     Returns
     -------
         str - a variable name that can be split on
     """
     splittable_variables = list(self.splittable_variables())
     if len(splittable_variables) == 0:
         raise NoSplittableVariableException()
     return np.random.choice(np.array(list(splittable_variables)), 1)[0]
Exemplo n.º 4
0
 def random_splittable_variable(self) -> str:
     """
     Choose a variable at random from the set of splittable variables
     Returns
     -------
         str - a variable name that can be split on
     """
     if self.is_at_least_one_splittable_variable():
         return np.random.choice(np.array(self.splittable_variables()),
                                 1)[0]
     else:
         raise NoSplittableVariableException()