jmschrei / pomegranate

Fast, flexible and easy to use probabilistic modelling in Python.
http://pomegranate.readthedocs.org/en/latest/
MIT License
3.34k stars 589 forks source link

BayesianNetwork.from_samples throws error on Python 3x #903

Closed ryankarlos closed 2 years ago

ryankarlos commented 3 years ago

Hello @jmschrei. Firstly, thanks for the great package !

i was trying to use the from_samples option and was getting the following error (example code snippet to reproduce below) - using python 3.8 and latest pomegranate version 0.14.4.

is this a bug as python 3x uses inbuilt zip rather than itertools.izip ?

>>>  from pomegranate import *
>>>  import numpy as np
>>>  x = np.random.choice(20,(5, 5))
>>>  model = BayesianNetwork.from_samples(x)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-219-58576a66c4d7> in <module>
      4 x = np.random.choice(20,(5, 5))
      5 
----> 6 bnet = BayesianNetwork.from_samples(x)

pomegranate/BayesianNetwork.pyx in pomegranate.BayesianNetwork.BayesianNetwork.from_samples()

pomegranate/BayesianNetwork.pyx in pomegranate.BayesianNetwork.BayesianNetwork.from_structure()

pomegranate/distributions.pyx in pomegranate.distributions.DiscreteDistribution.from_samples()

AttributeError: module 'itertools' has no attribute 'izip'
ryankarlos commented 3 years ago

Just an update, I tried downgrading to pomegranate 0.10.0 and python 3.6 as per the version used in #500. --- this works for me and returns the structure, which is confusing as to why it wouldnt work for the latest version and python 3.8 -- unless there are some versions of other packages installed which are causing the pomegranate build to not complete properly ?

>>> import pomegranate
>>> pomegranate.__version__
'0.10.0'
 >>> from pomegranate import BayesianNetwork
>>> import numpy as np
>>> x = np.random.choice(20,(2, 2))
>>> BayesianNetwork.from_samples(x)
{
    "class" : "BayesianNetwork",
    "name" : "140243768229056",
    "structure" : [
        [],
        [
            0
        ]
    ],
    "states" : [
        {
            "class" : "State",
            "distribution" : {
                "class" : "Distribution",
                "dtype" : "numpy.int64",
                "name" : "DiscreteDistribution",
                "parameters" : [
                    {
                        "3" : 0.5,
                        "18" : 0.5
                    }
                ],
                "frozen" : false
            },
            "name" : "0",
            "weight" : 1.0
        },
        {
            "class" : "State",
            "distribution" : {
                "class" : "Distribution",
                "name" : "ConditionalProbabilityTable",
                "table" : [
                    [
                        "3",
                        "1",
                        "0.0"
                    ],
                    [
                        "3",
                        "16",
                        "1.0"
                    ],
                    [
                        "18",
                        "1",
                        "1.0"
                    ],
                    [
                        "18",
                        "16",
                        "0.0"
                    ]
                ],
                "dtypes" : [
                    "numpy.int64",
                    "numpy.int64",
                    "float"
                ],
                "parents" : [
                    {
                        "class" : "Distribution",
                        "dtype" : "numpy.int64",
                        "name" : "DiscreteDistribution",
                        "parameters" : [
                            {
                                "3" : 0.5,
                                "18" : 0.5
                            }
                        ],
                        "frozen" : false
                    }
                ]
            },
            "name" : "1",
            "weight" : 1.0
        }
    ]
}
jmschrei commented 2 years ago

pomegranate hasn't used izip for a long time. I'm guessing that had an older version installed that was not being properly tracked by the package manager.