问题
I constructed a Bayesian network using from_samples()
in pomegranate. I'm able to get maximally likely predictions from the model using model.predict()
. I wanted to know if there is a way to sample from this Bayesian network conditionally(or unconditionally)? i.e. is there a get random samples from the network and not the maximally likely predictions?
I looked at model.sample()
, but it was raising NotImplementedError
.
Also if this is not possible to do using pomegranate
, what other libraries are great for Bayesian networks in Python?
回答1:
The model.sample()
should have been implemented by now if I see the commit history correctly.
You can have a look at PyMC which supports distribution mixtures as well.
However, I dont know any other toolbox with a similar factory method like from_samples()
in pomogranate.
回答2:
One way to sample from a 'baked' BayesianNetwork is using the predict_proba method. predict_proba returns a list of distributions corresponding to each node for which information was not provided, conditioned on the information that was provided.
e.g. :
bn = BayesianNetwork.from_samples(X)
proba = bn.predict_proba({"1":1,"2":0}) # proba will be an array of dists
samples = np.empty_like(proba)
for i in np.arange(proba.shape[0]):
for j in np.arange(proba.shape[1]):
if hasattr(proba[i][j],'sample'):
samples[i,j] = proba[i][j].sample(10000).mean() #sample and aggregate however you want
else:
samples[i,j] = proba[i][j]
pd.Series(samples,index=X.columns) #convert samples to a pandas.Series with column labels as index
来源:https://stackoverflow.com/questions/51035303/sample-from-a-bayesian-network-in-pomegranate