class_docstr: The DecisionTreeDiscretiser() replaces numerical variables by discrete, i.e.,
  finite variables, which values are the predictions of a decision tree.
  
  The method is inspired by the following article from the winners of the KDD
  2009 competition:
  http://www.mtome.com/Publications/CiML/CiML-v3-book.pdf
  
  The DecisionTreeDiscretiser() trains a decision tree per variable. Then, it
  transforms the variables, with predictions of the decision tree.
  
  The DecisionTreeDiscretiser() works only with numerical variables. A list of
  variables to transform can be indicated. Alternatively, the discretiser will
  automatically select all numerical variables.
  
  More details in the :ref:`User Guide <decisiontree_discretiser>`.
  
  Parameters
  ----------
  {variables}
  
  cv: int, cross-validation generator or an iterable, default=3
      Determines the cross-validation splitting strategy. Possible inputs for cv are:
  
          - None, to use cross_validate's default 5-fold cross validation
  
          - int, to specify the number of folds in a (Stratified)KFold,
  
          - CV splitter
              - (https://scikit-learn.org/stable/glossary.html#term-CV-splitter)
  
          - An iterable yielding (train, test) splits as arrays of indices.
  
      For int/None inputs, if the estimator is a classifier and y is either binary or
      multiclass, StratifiedKFold is used. In all other cases, KFold is used. These
      splitters are instantiated with `shuffle=False` so the splits will be the same
      across calls. For more details check Scikit-learn's `cross_validate`'s
      documentation.
  
  scoring: str, default='neg_mean_squared_error'
      Desired metric to optimise the performance of the tree. Comes from
      sklearn.metrics. See the DecisionTreeRegressor or DecisionTreeClassifier
      model evaluation documentation for more options:
      https://scikit-learn.org/stable/modules/model_evaluation.html
  
  param_grid: dictionary, default=None
      The hyperparameters for the decision tree to test with a grid search. The
      `param_grid` can contain any of the permitted hyperparameters for Scikit-learn's
      DecisionTreeRegressor() or DecisionTreeClassifier(). If None, then param_grid
      will optimise the 'max_depth' over `[1, 2, 3, 4]`.
  
  regression: boolean, default=True
      Indicates whether the discretiser should train a regression or a classification
      decision tree.
  
  random_state : int, default=None
      The random_state to initialise the training of the decision tree. It is one
      of the parameters of the Scikit-learn's DecisionTreeRegressor() or
      DecisionTreeClassifier(). For reproducibility it is recommended to set
      the random_state to an integer.
  
  Attributes
  ----------
  binner_dict_:
      Dictionary containing the fitted tree per variable.
  
  scores_dict_:
      Dictionary with the score of the best decision tree per variable.
  
  {variables_}
  
  {feature_names_in_}
  
  {n_features_in_}
  
  Methods
  -------
  fit:
      Fit a decision tree per variable.
  
  {fit_transform}
  
  transform:
      Replace continuous variable values by the predictions of the decision tree.
  
  See Also
  --------
  sklearn.tree.DecisionTreeClassifier
  sklearn.tree.DecisionTreeRegressor
  
  References
  ----------
  .. [1] Niculescu-Mizil, et al. "Winning the KDD Cup Orange Challenge with Ensemble
      Selection". JMLR: Workshop and Conference Proceedings 7: 23-34. KDD 2009
      http://proceedings.mlr.press/v7/niculescu09/niculescu09.pdf
  
  Examples
  --------
  
  >>> import pandas as pd
  >>> import numpy as np
  >>> from feature_engine.discretisation import DecisionTreeDiscretiser
  >>> np.random.seed(42)
  >>> X = pd.DataFrame(dict(x= np.random.randint(1,100, 100)))
  >>> y_reg = pd.Series(np.random.randn(100))
  >>> dtd = DecisionTreeDiscretiser(random_state=42)
  >>> dtd.fit(X, y_reg)
  >>> dtd.transform(X)["x"].value_counts()
  -0.090091    90
  0.479454    10
  Name: x, dtype: int64
  
  You can also apply this for classification problems adjusting the scoring metric.
  
  >>> y_clf = pd.Series(np.random.randint(0,2,100))
  >>> dtd = DecisionTreeDiscretiser(regression=False, scoring="f1", random_state=42)
  >>> dtd.fit(X, y_clf)
  >>> dtd.transform(X)["x"].value_counts()
  0.480769    52
  0.687500    48
  Name: x, dtype: int64
class_docstr_tok: ["The","DecisionTreeDiscretiser","(",")","replaces","numerical","variables","by","discrete",",","i.e.",",","finite","variables",",","which","values","are","the","predictions","of","a","decision","tree",".","The","method","is","inspired","by","the","following","article","from","the","winners","of","the","KDD","2009","competition",":","http",":","\/\/www.mtome.com\/Publications\/CiML\/CiML-v3-book.pdf","The","DecisionTreeDiscretiser","(",")","trains","a","decision","tree","per","variable",".","Then",",","it","transforms","the","variables",",","with","predictions","of","the","decision","tree",".","The","DecisionTreeDiscretiser","(",")","works","only","with","numerical","variables",".","A","list","of","variables","to","transform","can","be","indicated",".","Alternatively",",","the","discretiser","will","automatically","select","all","numerical","variables",".","More","details","in","the",":","ref",":","`","User","Guide","<","decisiontree_discretiser",">","`",".","Parameters","--","--","--","--","--","{","variables","}","cv",":","int",",","cross-validation","generator","or","an","iterable",",","default=3","Determines","the","cross-validation","splitting","strategy",".","Possible","inputs","for","cv","are",":","-","None",",","to","use","cross_validate","'s","default","5-fold","cross","validation","-","int",",","to","specify","the","number","of","folds","in","a","(","Stratified",")","KFold",",","-","CV","splitter","-","(","https",":","\/\/scikit-learn.org\/stable\/glossary.html","#","term-CV-splitter",")","-","An","iterable","yielding","(","train",",","test",")","splits","as","arrays","of","indices",".","For","int\/None","inputs",",","if","the","estimator","is","a","classifier","and","y","is","either","binary","or","multiclass",",","StratifiedKFold","is","used",".","In","all","other","cases",",","KFold","is","used",".","These","splitters","are","instantiated","with","`","shuffle=False","`","so","the","splits","will","be","the","same","across","calls",".","For","more","details","check","Scikit-learn","'s","`","cross_validate","`","'s","documentation",".","scoring",":","str",",","default='neg_mean_squared_error'","Desired","metric","to","optimise","the","performance","of","the","tree",".","Comes","from","sklearn.metrics",".","See","the","DecisionTreeRegressor","or","DecisionTreeClassifier","model","evaluation","documentation","for","more","options",":","https",":","\/\/scikit-learn.org\/stable\/modules\/model_evaluation.html","param_grid",":","dictionary",",","default=None","The","hyperparameters","for","the","decision","tree","to","test","with","a","grid","search",".","The","`","param_grid","`","can","contain","any","of","the","permitted","hyperparameters","for","Scikit-learn's","DecisionTreeRegressor","(",")","or","DecisionTreeClassifier","(",")",".","If","None",",","then","param_grid","will","optimise","the","'max_depth","'","over","`","[","1",",","2",",","3",",","4","]","`",".","regression",":","boolean",",","default=True","Indicates","whether","the","discretiser","should","train","a","regression","or","a","classification","decision","tree",".","random_state",":","int",",","default=None","The","random_state","to","initialise","the","training","of","the","decision","tree",".","It","is","one","of","the","parameters","of","the","Scikit-learn","'s","DecisionTreeRegressor","(",")","or","DecisionTreeClassifier","(",")",".","For","reproducibility","it","is","recommended","to","set","the","random_state","to","an","integer",".","Attributes","--","--","--","--","--","binner_dict_",":","Dictionary","containing","the","fitted","tree","per","variable",".","scores_dict_",":","Dictionary","with","the","score","of","the","best","decision","tree","per","variable",".","{","variables_","}","{","feature_names_in_","}","{","n_features_in_","}","Methods","--","--","--","-","fit",":","Fit","a","decision","tree","per","variable",".","{","fit_transform","}","transform",":","Replace","continuous","variable","values","by","the","predictions","of","the","decision","tree",".","See","Also","--","--","--","--","sklearn.tree.DecisionTreeClassifier","sklearn.tree.DecisionTreeRegressor","References","--","--","--","--","--","..","[","1","]","Niculescu-Mizil",",","et","al",".","``","Winning","the","KDD","Cup","Orange","Challenge","with","Ensemble","Selection","''",".","JMLR",":","Workshop","and","Conference","Proceedings","7",":","23-34",".","KDD","2009","http",":","\/\/proceedings.mlr.press\/v7\/niculescu09\/niculescu09.pdf","Examples","--","--","--","--",">",">",">","import","pandas","as","pd",">",">",">","import","numpy","as","np",">",">",">","from","feature_engine.discretisation","import","DecisionTreeDiscretiser",">",">",">","np.random.seed","(","42",")",">",">",">","X","=","pd.DataFrame","(","dict","(","x=","np.random.randint","(","1,100",",","100",")",")",")",">",">",">","y_reg","=","pd.Series","(","np.random.randn","(","100",")",")",">",">",">","dtd","=","DecisionTreeDiscretiser","(","random_state=42",")",">",">",">","dtd.fit","(","X",",","y_reg",")",">",">",">","dtd.transform","(","X",")","[","``","x","''","]",".value_counts","(",")","-0.090091","90","0.479454","10","Name",":","x",",","dtype",":","int64","You","can","also","apply","this","for","classification","problems","adjusting","the","scoring","metric",".",">",">",">","y_clf","=","pd.Series","(","np.random.randint","(","0,2,100",")",")",">",">",">","dtd","=","DecisionTreeDiscretiser","(","regression=False",",","scoring=","''","f1","''",",","random_state=42",")",">",">",">","dtd.fit","(","X",",","y_clf",")",">",">",">","dtd.transform","(","X",")","[","``","x","''","]",".value_counts","(",")","0.480769","52","0.687500","48","Name",":","x",",","dtype",":","int64"]
docstr: Fit one decision tree per variable to discretize with cross-validation and
  grid-search for hyperparameters.
  
  Parameters
  ----------
  
  X: pandas dataframe of shape = [n_samples, n_features]
      The training dataset. Can be the entire dataframe, not just the
      variables to be transformed.
  
  y: pandas series.
      Target variable. Required to train the decision tree.
docstr_tok: ["Fit","one","decision","tree","per","variable","to","discretize","with","cross-validation","and","grid-search","for","hyperparameters",".","Parameters","--","--","--","--","--","X",":","pandas","dataframe","of","shape","=","[","n_samples",",","n_features","]","The","training","dataset",".","Can","be","the","entire","dataframe",",","not","just","the","variables","to","be","transformed",".","y",":","pandas","series",".","Target","variable",".","Required","to","train","the","decision","tree","."]
code: def fit(self, X: pd.DataFrame, y: pd.Series):  # type: ignore
      """
      Fit one decision tree per variable to discretize with cross-validation and
      grid-search for hyperparameters.
  
      Parameters
      ----------
  
      X: pandas dataframe of shape = [n_samples, n_features]
          The training dataset. Can be the entire dataframe, not just the
          variables to be transformed.
  
      y: pandas series.
          Target variable. Required to train the decision tree.
      """
      # confirm model type and target variables are compatible.
      if self.regression is True:
          if type_of_target(y) == "binary":
              raise ValueError(
                  "Trying to fit a regression to a binary target is not "
                  "allowed by this transformer. Check the target values "
                  "or set regression to False."
              )
      else:
          check_classification_targets(y)
  
      # check input dataframe
      X = super().fit(X)
  
      if self.param_grid:
          param_grid = self.param_grid
      else:
          param_grid = {"max_depth": [1, 2, 3, 4]}
  
      self.binner_dict_ = {}
      self.scores_dict_ = {}
  
      for var in self.variables_:
          if self.regression:
              model = DecisionTreeRegressor(
                  random_state=self.random_state
              )
          else:
              model = DecisionTreeClassifier(
                  random_state=self.random_state
              )
  
          tree_model = GridSearchCV(
              model,
              cv=self.cv,
              scoring=self.scoring,
              param_grid=param_grid,
          )
  
          # fit the model to the variable
          tree_model.fit(X[var].to_frame(), y)
  
          self.binner_dict_[var] = tree_model
          self.scores_dict_[var] = tree_model.score(
              X[var].to_frame(), y
          )
  
      return self
  
file_path: feature_engine/feature_engine/discretisation/decision_tree.py
imports: from typing import Dict, List, Optional, Union
  import pandas
  from sklearn.model_selection import GridSearchCV
  from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
  from sklearn.utils.multiclass import check_classification_targets, type_of_target
  from feature_engine._base_transformers.base_numerical import BaseNumericalTransformer
  from feature_engine._docstrings.fit_attributes import _feature_names_in_docstring, _n_features_in_docstring, _variables_attribute_docstring
  from feature_engine._docstrings.init_parameters import _variables_numerical_docstring
  from feature_engine._docstrings.methods import _fit_transform_docstring
  from feature_engine._docstrings.substitute import Substitution
  from feature_engine._variable_handling.init_parameter_checks import _check_init_parameter_variables
  from feature_engine.tags import _return_tags
label_desc: Use image node_id 2 for calling the DecisionTreeDiscretiser obj's underlying member method code with example usage: obj.fit(X, y) and returns: self