Classifier using Bayesian networks¶
- class pyAgrum.skbn.BNClassifier(learningMethod='GHC', aPriori=None, scoringType='BIC', constraints=None, aPrioriWeight=1, possibleSkeleton=None, DirichletCsv=None, discretizationStrategy='quantile', discretizationNbBins=5, discretizationThreshold=25, usePR=False, significant_digit=10)¶
Represents a (scikit-learn compliant) classifier wich uses a BN to classify. A BNClassifier is build using
a Bayesian network,
a database and a learning algorithm and parameters
the use of BNDiscretizer to discretize with different algorithms some variables.
- parameters:
- learningMethod: str
A string designating which type of learning we want to use. Possible values are: Chow-Liu, NaiveBayes, TAN, MIIC + (MDL ou NML), GHC, 3off2 + (MDL ou NML), Tabu. GHC designates Greedy Hill Climbing. MIIC designates Multivariate Information based Inductive Causation TAN designates Tree-augmented NaiveBayes Tabu designated Tabu list searching
- aPriori: str
A string designating the type of a priori smoothing we want to use. Possible values are Smoothing, BDeu, Dirichlet and NoPrior . Note: if using Dirichlet smoothing DirichletCsv cannot be set to none By default (when aPriori is None) : a smoothing(0.01) is applied.
- scoringType: str
A string designating the type of scoring we want to use. Since scoring is used while constructing the network and not when learning its parameters, the scoring will be ignored if using a learning algorithm with a fixed network structure such as Chow-Liu, TAN or NaiveBayes. possible values are: AIC, BIC, BD, BDeu, K2, Log2 AIC means Akaike information criterion BIC means Bayesian Information criterion BD means Bayesian-Dirichlet scoring BDeu means Bayesian-Dirichlet equivalent uniform Log2 means log2 likelihood ratio test
- constraints: dict()
A dictionary designating the constraints that we want to put on the structure of the Bayesian network. Ignored if using a learning algorithm where the structure is fixed such as TAN or NaiveBayes. the keys of the dictionary should be the strings “PossibleEdges” , “MandatoryArcs” and “ForbiddenArcs”. The format of the values should be a tuple of strings (tail,head) which designates the string arc from tail to head. For example if we put the value (“x0”.”y”) in MandatoryArcs the network will surely have an arc going from x0 to y. Note: PossibleEdge allows between nodes x and y allows for either (x,y) or (y,x) (or none of them) to be added to the Bayesian network, while the others are not symmetric.
- aPrioriWeight: double
The weight used for a priori smoothing.
- possibleSkeleton: pyagrum.undigraph
An undirected graph that serves as a possible skeleton for the Bayesian network
- DirichletCsv: str
the file name of the csv file we want to use for the dirichlet prior. Will be ignored if aPriori is not set to Dirichlet.
- discretizationStrategy: str
sets the default method of discretization for this discretizer. This method will be used if the user has not specified another method for that specific variable using the setDiscretizationParameters method possible values are: ‘quantile’, ‘uniform’, ‘kmeans’, ‘NML’, ‘CAIM’ and ‘MDLP’
- defaultNumberOfBins: str or int
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 discetization method is used, this parameter is ignored.
- discretizationThreshold: int or float
When using default parameters a variable will be treated as continous 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 instance, if you have entered 0.95, the variable will be treated as continous only if more than 95% of its values are unique.
- usePR: bool
indicates if the threshold to choose is Prevision-Recall curve’s threhsold or ROC’s threshold by default. ROC curves should be used when there are roughly equal numbers of observations for each class. Precision-Recall curves should be used when there is a moderate to large class imbalance especially for the target’s class.
- significant_digit:
number of significant digits when computing probabilities
- XYfromCSV(filename, with_labels=True, target=None)¶
- parameters:
- filename: str
the name of the csv file
- with_labels: bool
tells us whether the csv includes the labels themselves or their indexes.
- target: str or None
The name of the column that will be put in the dataframe y. If target is None, we use the target that is already specified in the classifier
- returns:
- X: pandas.dataframe
Matrix containing the data
- y: pandas.dataframe
Column-vector containing the class for each data vector in X
Reads the data from a csv file and separates it into a X matrix and a y column vector.
- fit(X=None, y=None, filename=None, targetName=None)¶
- parameters:
- X: {array-like, sparse matrix} of shape (n_samples, n_features)
training data. Warning: Raises ValueError if either filename or targetname is not None. Raises ValueError if y is None.
- y: array-like of shape (n_samples)
Target values. Warning: Raises ValueError if either filename or targetname is not None. Raises ValueError if X is None
- filename: str
specifies the csv file where the training data and target values are located. Warning: Raises ValueError if either X or y is not None. Raises ValueError if targetName is None
- targetName: str
specifies the name of the targetVariable in the csv file. Warning: Raises ValueError if either X or y is not None. Raises ValueError if filename is None.
- returns:
void
Fits the model to the training data provided. The two possible uses of this function are fit(X,y) and fit(filename, targetName). Any other combination will raise a ValueError
- fromTrainedModel(bn, targetAttribute, targetModality='', copy=False, threshold=0.5, variableList=None)¶
- parameters:
- bn: pyagrum.BayesNet
The Bayesian network we want to use for this classifier
- targetAttribute: str
the attribute that will be the target in this classifier
- targetModality: str
If this is a binary classifier we have to specify which modality we are looking at if the target attribute has more than 2 possible values if !=””, a binary classifier is created. if ==””, a classifier is created that can be non binary depending on the number of modalities for targetAttribute. If binary, the second one is taken as targetModality.
- copy: bool
Indicates whether we want to put a copy of bn in the classifier, or bn itself.
- threshold: double
The classification threshold. If the probability that the target modality is true is larger than this threshold we predict that modality
- variableList: list(str)
A list of strings. variableList[i] is the name of the variable that has the index i. We use this information when calling predict to know which column corresponds to which variable. If this list is set to none, then we use the order in which the variables were added to the network.
- returns:
void
Creates a BN classifier from an already trained pyAgrum Bayesian network
- get_params(deep=True)¶
Get parameters for this estimator.
- Parameters
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
dict
- predict(X, with_labels=True)¶
- parameters:
- X: {array-like, sparse matrix} of shape (n_samples, n_features) or str
test data, can be either dataFrame, matrix or name of a csv file
- with_labels: bool
tells us whether the csv includes the labels themselves or their indexes.
- returns:
- y: array-like of shape (n_samples,)
Predicted classes
Predicts the most likely class for each row of input data, with bn’s Markov Blanket
- predict_proba(X)¶
- parameters:
- X: {array-like, sparse matrix} of shape (n_samples, n_features) or str
test data, can be either dataFrame, matrix or name of a csv file
- returns:
- y: array-like of shape (n_samples,)
Predicted probability for each classes
Predicts the probability of classes for each row of input data, with bn’s Markov Blanket
- score(X, y, sample_weight=None)¶
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns
score – Mean accuracy of
self.predict(X)
wrt. y.- Return type
float
- set_params(**params)¶
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
**params (dict) – Estimator parameters.
- Returns
self – Estimator instance.
- Return type
estimator instance
- showROC_PR(filename, save_fig=False, show_progress=False)¶
Use the pyAgrum.lib.bn2roc tools to create ROC and Precision-Recall curve
- parameters:
- csv_namestr
a csv filename
- save_figbool
whether the graph soulb de saved
- show_progressbool
indicates if the resulting curve must be printed