Replies: 1 comment
-
@ebraun19 Sorry for the late reply. In [19]: from pgmpy.models import BayesianNetwork
In [20]: from pgmpy.utils import get_example_model
In [21]: model = get_example_model('cancer')
In [22]: model_latent = BayesianNetwork(model.edges(), latents={'Smoker'})
In [23]: model_latent.add_cpds(*model.cpds)
In [24]: data = model_latent.simulate(int(1e4), seed=42)
Generating for node: Dyspnoea: 100%|____________________________| 5/5 [00:00<00:00, 154.04it/s]
In [25]: est = EM(model_latent, data)
In [26]: est.get_parameters(seed=42, latent_card={'Smoker': 3})
WARNING:pgmpy:Replacing existing CPD for Xray
WARNING:pgmpy:Replacing existing CPD for Dyspnoea
WARNING:pgmpy:Replacing existing CPD for Pollution
WARNING:pgmpy:Replacing existing CPD for Smoker
WARNING:pgmpy:Replacing existing CPD for Cancer
4%|___ | 4/100 [00:00<00:04, 19.31it/s]
Out[26]:
[<TabularCPD representing P(Xray:2 | Cancer:2) at 0x7f399b67de10>,
<TabularCPD representing P(Dyspnoea:2 | Cancer:2) at 0x7f39a0250ac0>,
<TabularCPD representing P(Pollution:2) at 0x7f399b52de40>,
<TabularCPD representing P(Smoker:3) at 0x7f398f63a860>,
<TabularCPD representing P(Cancer:2 | Pollution:2, Smoker:3) at 0x7f398dce7eb0>] or model_latent.fit(data, estimator=EM, latent_card={'Smoker': 3}) About the loading bar stuck, if you have a large dataset it can take some time to compute the CPDs. You can also set |
Beta Was this translation helpful? Give feedback.
-
Hi!
I am trying to do parameter estimation with the expectation maximization estimator and I currently have three latent nodes.
I have tried a lot and searched on the internet for solutions however, I cannot seem to make it work and there is not much to find online.
My model exists of 23 nodes:
I have the following code:
Model = BayesianNetwork(bn_edges_list, latents = {'AR', 'Be', 'Hi'})
estimator = ExpectationMaximization(Model, data, state_names = {'AR':2, 'Be':5, 'Hi':6})
estimator.get_parameters()
It starts with running and there is a loading bar but it always stops around 12%, I am not sure what that means?
There is an output of all the TabularCPD there should be, with the correct number of states for each node.
When I run Model.get_cpds() it returns an empty list. I thought I might have missed a step and there are just no elaborate examples of the Expectation Maximization estimator to check.
Instead, I also tried to run:
Model.fit(data, estimator = ExpectationMaximization, state_names = {'AR':2, 'Be':5, 'Hi':6})
But it then outputs an error: 'Maximum Likelihood Estimator works only for models with all observed variables. Found latent variables: {'AR', 'Be', 'Hi'}.
I am also not sure what exactly the difference is between running ExpectationMaximization(Model,data) and Model.fit(data, estimator = ExpectationMaximization)
In addition, I also tried to give the model a prior by adding a cpd thinking this would solve something by not having any latent variables. This did also not work. But I am still wondering if it is possible to add a prior for the latent variables.
A lot of questions, but I sincerely hope someone can help me solve this :). Any help is greatly valued!!
Beta Was this translation helpful? Give feedback.
All reactions