-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pyAgrum package and MIIC algorithm? #115
Comments
Hi, that sounds like a good idea. In pyAgrum they call the |
@felixleopoldo The There are examples of using #we use now another method to learn the BN (MIIC)
BNTest= skbn.BNClassifier(learningMethod = 'MIIC', prior= 'Smoothing', priorWeight = 0.5,
discretizationStrategy = 'quantile', usePR = True, significant_digit = 13)
xTrain, yTrain = BNTest.XYfromCSV(filename = 'res/creditCardTest.csv', target = 'Class') More examples using I have only used pyAgrum because I don't know R so, I have never directly compared the two. pyAgrum is a Python wrapper around the aGrum C++ library where their MIIC implementation is sourced in C++. It looks similar to how the original authors of MIIC provide a C++ implementation wrapped in R, but I don't know for sure. Let me know if you need any more help. =) |
Thanks. It seems like they refer to the Bayesian network as a classifier, where one is specified as Target? It would be nice if you could show how to do the following two steps:
|
I hope the example below demos what you need.
From what I know, there isn't any convenient The example assumes you have the following installed in your environment: import csv
from pathlib import Path
import pandas as pd
import pyAgrum.skbn as skbn
from pyAgrum import BayesNet
def adjacency_to_csv(bn: BayesNet, *, to_file: str):
id_to_name = {bn.idFromName(name): name for name in bn.names()}
with Path(to_file).open(mode="w", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
# write header
writer.writerow(id_to_name[col_id] for col_id in range(bn.size()))
#write rows
adj_mat = bn.adjacencyMatrix()
writer.writerows(row for row in adj_mat)
data = pd.read_csv(
"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv"
).dropna()
data.to_csv("fully_obs_titanic.csv", index=False)
classifier = skbn.BNClassifier(learningMethod="MIIC", scoringType="BIC")
xdata, ydata = classifier.XYfromCSV(filename="fully_obs_titanic.csv", target="survived")
classifier.fit(xdata, ydata)
adjacency_to_csv(classifier.bn, to_file="resulting_adjacency.csv") Here is the resulting adjacency matrix:
I ran this example with the following environment:
|
Thanks a lot. So for the target variable (survived), can we just choose the first one in the order? |
For the
|
Ok! |
Hi @felixleopoldo , many thanks to @bdatko for this "issue". Actually, BNClassifier is based on the BNLearner class. If you want to test the learning algorithms of pyAgrum, you should use BNLearner. import pyAgrum as gum
learner=gum.BNLearner("test.csv") # MIIC is used as default (some score-based are also implented)
learner.useMDLCorrection() # for small dataset
learner.useSmoothingPrior() # smoothing (default weight=1) for parameters
bn=learner.learnBN() # learning Thanks again to @bdatko. Please tell me if you need some other snippets :-) |
Hi @phwuil, |
hi @felixleopoldo , thank you for that. pyAgrum is mainly about discrete variables. However there are 2 solutions for continuous data : 1- automatic discretisation with pyAgrum.skbn.BNDiscretizer import pyAgrum as gum
import pyAgrum.skbn as skbn
filename="test.csv"
# BNDiscretizer has many options
disc=skbn.BNDiscretizer()
template=disc.discretizedBN(filename)
# template contains all the (discrete variables)
# that will be used for the learning
learner=gum.BNLearner(filename,template)
learner.useMDLCorrection()
learner.useSmoothingPrior()
bn=learner.learnBN() |
2- CLG : new CLG implementation in pyAgrum 1.14.0
|
OK. There is a new pyagrum branch, where you can try pyagrum by |
Hi @felixleopoldo, thank you for this. I have to admit that I did not know before it was pointed out to me by @bdatko. Thanks for both of you. |
I see, no worries:) If you mean the main reference to Benchpress it is here. It is not mentioned there, but you can also run it under WSL on Windows. |
Hi, I have added pyagrum to benchpress.
Feel free to update it with more of the parameters/functionalities from pyagrum. |
I think pyAgrum would be a great addition to the list of algorithms. To my eyes, it did not look like there was a comparison in
benchpress
using the Multivariate Information-based Inductive Causation (MIIC) algorithm which pyAgrum has implemented. The library also offer a scikit-learn interface to learn classifiers which should help with the integration intobenchpress
.The text was updated successfully, but these errors were encountered: