|
24 | 24 |
|
25 | 25 | from pgmpy.models import BayesianNetwork, NaiveBayes, DynamicBayesianNetwork, MarkovNetwork |
26 | 26 | from pgmpy.factors.discrete import TabularCPD |
27 | | -from pgmpy.sampling import BayesianModelSampling, GibbsSampling |
28 | 27 | from pgmpy.metrics import structure_score |
29 | 28 |
|
30 | 29 | from setgraphviz import setgraphviz |
@@ -592,76 +591,76 @@ def adjmat2dict(adjmat): |
592 | 591 |
|
593 | 592 |
|
594 | 593 | # %% Sampling from model |
595 | | -def sampling(DAG, n=1000, methodtype='bayes', verbose=0): |
596 | | - """Generate sample(s) using the joint distribution of the network. |
| 594 | +# def sampling(DAG, n=1000, methodtype='bayes', verbose=0): |
| 595 | +# """Generate sample(s) using the joint distribution of the network. |
597 | 596 |
|
598 | | - Parameters |
599 | | - ---------- |
600 | | - DAG : dict |
601 | | - Contains model and the adjmat of the DAG. |
602 | | - methodtype : str (default: 'bayes') |
603 | | - * 'bayes': Forward sampling using Bayesian. |
604 | | - * 'gibbs' : Gibbs sampling. |
605 | | - n : int, optional |
606 | | - Number of samples to generate. The default is 1000. |
607 | | - verbose : int, optional |
608 | | - Print progress to screen. The default is 3. |
609 | | - 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE |
| 597 | +# Parameters |
| 598 | +# ---------- |
| 599 | +# DAG : dict |
| 600 | +# Contains model and the adjmat of the DAG. |
| 601 | +# methodtype : str (default: 'bayes') |
| 602 | +# * 'bayes': Forward sampling using Bayesian. |
| 603 | +# * 'gibbs' : Gibbs sampling. |
| 604 | +# n : int, optional |
| 605 | +# Number of samples to generate. The default is 1000. |
| 606 | +# verbose : int, optional |
| 607 | +# Print progress to screen. The default is 3. |
| 608 | +# 0: None, 1: ERROR, 2: WARN, 3: INFO (default), 4: DEBUG, 5: TRACE |
610 | 609 |
|
611 | | - Returns |
612 | | - ------- |
613 | | - df : pd.DataFrame(). |
614 | | - Dataframe containing sampled data from the input DAG model. |
| 610 | +# Returns |
| 611 | +# ------- |
| 612 | +# df : pd.DataFrame(). |
| 613 | +# Dataframe containing sampled data from the input DAG model. |
615 | 614 |
|
616 | | - Example |
617 | | - ------- |
618 | | - >>> # Example 1 |
619 | | - >>> |
620 | | - >>> # Import library |
621 | | - >>> import bnlearn as bn |
622 | | - >>> # Load DAG with model |
623 | | - >>> DAG = bn.import_DAG('sprinkler') |
624 | | - >>> # Sampling |
625 | | - >>> df = bn.sampling(DAG, n=1000, methodtype='bayes') |
626 | | - >>> |
627 | | - >>> # Example 2: |
628 | | - >>> |
629 | | - >>> # Load example dataset |
630 | | - >>> df = bn.import_example('sprinkler') |
631 | | - >>> edges = [('Cloudy', 'Sprinkler'), |
632 | | - >>> ('Cloudy', 'Rain'), |
633 | | - >>> ('Sprinkler', 'Wet_Grass'), |
634 | | - >>> ('Rain', 'Wet_Grass')] |
635 | | - >>> |
636 | | - >>> # Make the actual Bayesian DAG |
637 | | - >>> DAG = bn.make_DAG(edges, verbose=3, methodtype='bayes') |
638 | | - >>> # Fit model |
639 | | - >>> model = bn.parameter_learning.fit(DAG, df, verbose=3, methodtype='bayes') |
640 | | - >>> # Sampling using gibbs |
641 | | - >>> df = bn.sampling(model, n=100, methodtype='gibbs', verbose=0) |
| 615 | +# Example |
| 616 | +# ------- |
| 617 | +# >>> # Example 1 |
| 618 | +# >>> |
| 619 | +# >>> # Import library |
| 620 | +# >>> import bnlearn as bn |
| 621 | +# >>> # Load DAG with model |
| 622 | +# >>> DAG = bn.import_DAG('sprinkler') |
| 623 | +# >>> # Sampling |
| 624 | +# >>> df = bn.sampling(DAG, n=1000, methodtype='bayes') |
| 625 | +# >>> |
| 626 | +# >>> # Example 2: |
| 627 | +# >>> |
| 628 | +# >>> # Load example dataset |
| 629 | +# >>> df = bn.import_example('sprinkler') |
| 630 | +# >>> edges = [('Cloudy', 'Sprinkler'), |
| 631 | +# >>> ('Cloudy', 'Rain'), |
| 632 | +# >>> ('Sprinkler', 'Wet_Grass'), |
| 633 | +# >>> ('Rain', 'Wet_Grass')] |
| 634 | +# >>> |
| 635 | +# >>> # Make the actual Bayesian DAG |
| 636 | +# >>> DAG = bn.make_DAG(edges, verbose=3, methodtype='bayes') |
| 637 | +# >>> # Fit model |
| 638 | +# >>> model = bn.parameter_learning.fit(DAG, df, verbose=3, methodtype='bayes') |
| 639 | +# >>> # Sampling using gibbs |
| 640 | +# >>> df = bn.sampling(model, n=100, methodtype='gibbs', verbose=0) |
642 | 641 |
|
643 | | - """ |
644 | | - if n<=0: raise ValueError('Number of samples (n) must be 1 or larger!') |
645 | | - if (DAG is None) or ('BayesianNetwork' not in str(type(DAG['model']))): |
646 | | - raise ValueError('The input model (DAG) must contain BayesianNetwork.') |
647 | | - |
648 | | - if len(DAG['model'].get_cpds())==0: |
649 | | - raise Exception('[bnlearn] >Error! This is a Bayesian DAG containing only edges, and no CPDs. Tip: you need to specify or learn the CPDs. Try: DAG=bn.parameter_learning.fit(DAG, df). At this point you can make a plot with: bn.plot(DAG).') |
650 | | - return |
651 | | - |
652 | | - if methodtype=='bayes': |
653 | | - if verbose>=3: print('[bnlearn] >Bayesian forward sampling for %.0d samples..' %(n)) |
654 | | - # Bayesian Forward sampling and make dataframe |
655 | | - infer_model = BayesianModelSampling(DAG['model']) |
656 | | - df = infer_model.forward_sample(size=n, seed=None, show_progress=(True if verbose>=3 else False)) |
657 | | - elif methodtype=='gibbs': |
658 | | - if verbose>=3: print('[bnlearn] >Gibbs sampling for %.0d samples..' %(n)) |
659 | | - # Gibbs sampling |
660 | | - gibbs = GibbsSampling(DAG['model']) |
661 | | - df = gibbs.sample(size=n, seed=None) |
662 | | - else: |
663 | | - if verbose>=3: print('[bnlearn] >Methodtype [%s] unknown' %(methodtype)) |
664 | | - return df |
| 642 | +# """ |
| 643 | +# if n<=0: raise ValueError('Number of samples (n) must be 1 or larger!') |
| 644 | +# if (DAG is None) or ('BayesianNetwork' not in str(type(DAG['model']))): |
| 645 | +# raise ValueError('The input model (DAG) must contain BayesianNetwork.') |
| 646 | + |
| 647 | +# if len(DAG['model'].get_cpds())==0: |
| 648 | +# raise Exception('[bnlearn] >Error! This is a Bayesian DAG containing only edges, and no CPDs. Tip: you need to specify or learn the CPDs. Try: DAG=bn.parameter_learning.fit(DAG, df). At this point you can make a plot with: bn.plot(DAG).') |
| 649 | +# return |
| 650 | + |
| 651 | +# if methodtype=='bayes': |
| 652 | +# if verbose>=3: print('[bnlearn] >Bayesian forward sampling for %.0d samples..' %(n)) |
| 653 | +# # Bayesian Forward sampling and make dataframe |
| 654 | +# infer_model = BayesianModelSampling(DAG['model']) |
| 655 | +# df = infer_model.forward_sample(size=n, seed=None, show_progress=(True if verbose>=3 else False)) |
| 656 | +# elif methodtype=='gibbs': |
| 657 | +# if verbose>=3: print('[bnlearn] >Gibbs sampling for %.0d samples..' %(n)) |
| 658 | +# # Gibbs sampling |
| 659 | +# gibbs = GibbsSampling(DAG['model']) |
| 660 | +# df = gibbs.sample(size=n, seed=None) |
| 661 | +# else: |
| 662 | +# if verbose>=3: print('[bnlearn] >Methodtype [%s] unknown' %(methodtype)) |
| 663 | +# return df |
665 | 664 |
|
666 | 665 |
|
667 | 666 | # %% Convert BIF model to bayesian model |
@@ -1630,7 +1629,7 @@ def import_example(data='sprinkler', url=None, sep=',', n=10000, verbose=3): |
1630 | 1629 | if (data=='alarm') or (data=='andes') or (data=='asia') or (data=='sachs') or (data=='water'): |
1631 | 1630 | try: |
1632 | 1631 | DAG = import_DAG(data, verbose=2) |
1633 | | - df = sampling(DAG, n=n, verbose=2) |
| 1632 | + df = bnlearn.sampling(DAG, n=n, verbose=2) |
1634 | 1633 | except: |
1635 | 1634 | print('[bnlearn] >Error: Loading data not possible!') |
1636 | 1635 | df = None |
|
0 commit comments