DiscreteTypeProcessor for graphical Models

pyagrum.lib.discreteTypeProcessor.DiscreteTypeProcessor preprocesses columns of a pandas DataFrame (or CSV file) to map them to discrete random variables suitable for use in graphical models. It is used internally by pyagrum.BNLearner when learning from data.

class pyagrum.lib.discreteTypeProcessor.DiscreteTypeProcessor(defaultDiscretizationMethod='quantile', defaultNumberOfBins=10, discretizationThreshold=25)

Represents a tool to process the type of the variables in a database in order to obtain a way to learn a pyAgrum’s discrete Graphical Model. Particularly, the DiscreteTypeProcessor class is used to discretize some continuous variables in a database.

Warning

  • The data are represented by tabular data (X and possibly y) where the columns are the variables and the rows are the samples. Generally, X can be replaced by a the name of a csv file.

  • In the case of a classification, y is the class variable and X are the features. y may have any number of classes.

Parameters:
  • defaultDiscretizationMethod (str) – sets the default method of discretization for this discretizer. Possible values are: quantile, uniform, kmeans, NML, CAIM and MDLP. This method will be used if the user has not specified another method for that specific variable using the setDiscretizationParameters method. With quantile, if a single value holds so many samples that all the quantiles fall on it, this value gets its own bin (with a UserWarning).

  • defaultNumberOfBins (int | str) – sets the number of bins if the method used is quantile, kmeans, uniform. In this case this parameter can also be set to the string elbowMethod so that the best number of bins is found automatically. If the method used is NML, this parameter sets the the maximum number of bins up to which the NML algorithm searches for the optimal number of bins. In this case this parameter must be an int If any other discretization method is used, this parameter is ignored.

  • discretizationThreshold (int or float) – When using default parameters a variable will be treated as continuous only if it has more unique values than this number (if the number is an int greater than 1). If the number is a float between 0 and 1, we will test if the proportion of unique values is bigger than this number. For example if you have entered 0.95, the variable will be treated as continuous only if more than 95% of its values are unique.

Scientific notes

DiscreteTypeProcessor turns the columns of a database into discrete variables. A numerical column is discretized when it has more than discretizationThreshold distinct values (25 by default; a threshold below 1 is a proportion of distinct values), otherwise its values become labels. The default method is quantile with 10 intervals; the candidate cut points of the supervised methods are the midpoints between consecutive distinct values.

  • quantile, uniform, kmeans: intervals of equal frequency, of equal width, or the clusters of a one-dimensional k-means, with the given number of intervals.

  • MDLP [1] (supervised): an interval \(S\) of \(N\) samples is split recursively at the cut point \(T\) that minimizes the class entropy \(E(T) = \frac{|S_1|}{N} H(S_1) + \frac{|S_2|}{N} H(S_2)\), while the gain satisfies the minimum description length criterion

    \[H(S) - E(T) > \frac{\log_2(N-1)}{N} + \frac{\log_2(3^k - 2) - [k H(S) - k_1 H(S_1) - k_2 H(S_2)]}{N},\]

    \(k, k_1, k_2\) being the numbers of classes in \(S, S_1, S_2\).

  • CAIM [2] (supervised): cut points are added greedily, each maximizing \(\mathrm{CAIM} = \frac{1}{n} \sum_{r=1}^{n} \max_r^2 / M_r\) (\(n\) intervals, \(M_r\) samples in the interval \(r\), \(\max_r\) the count of its most frequent class), as long as it increases.

  • NML [3] (unsupervised): among the histograms with at most defaultNumberOfBins (10) intervals, the one that minimizes the NML code length of the data plus \(\log\binom{E}{K-1}\) for the choice of its \(K-1\) cut points among the \(E\) candidates; the global optimum is found by dynamic programming in \(O(E^2 K)\). The limit of 10 intervals is often reached: on the mixtures of Gaussians below, it is reached from 1000 samples on (for 5 to 10 samples out of 10 with 1000 samples, for all of them with 3000). The result is then the best histogram with 10 intervals, not the one chosen by the criterion: a larger limit (the parameter of the method) lets the criterion choose the number of intervals, at a cost linear in this limit.

Choices of pyAgrum: MDLP and CAIM accept any number of classes. As in [2], cut points are added to CAIM at least until there are as many intervals as classes; MDLP makes no forced cut and raises an error when no cut point satisfies the criterion (another method must then be chosen for this column); NML returns at least two intervals. The candidate cut points of NML are the midpoints of consecutive distinct values, one of the choices discussed in [3] (section 4), and not the points \(x \pm \varepsilon/2\) around each value, \(\varepsilon\) being the precision of the data: with an \(\varepsilon\) smaller than the gaps between values, these allow bins of width \(\varepsilon\) around isolated values, which the criterion favours. On the protocol of the article (mixtures of Gaussians close to those of its figures, data recorded with precision 0.1, 10 samples per size), the midpoints give the same squared Hellinger distance to the generating density as \(\varepsilon = 0.1\) (0.021 against 0.023 for 1000 samples, 0.0054 against 0.0057 for 10000, with the same numbers of bins), while \(\varepsilon\) set to half the smallest gap gives 0.20 with about 20 bins narrower than the precision; on continuous data (1000 samples), the midpoints give 0.027 against 0.039. The midpoints exclude empty bins.

Methods

audit()

Audits the passed values of X and y.

clear()

Sets the number of continuous variables and the total number of bins created by this discretizer to 0.

discretizedTemplate()

return a graphical model discretized using the suggestion of the Discretized for date source X (and for target y).

setDiscretizationParameters()

Sets the discretization parameters for a variable.