ladiesman ladiesman - 3 months ago 5x
Python Question

Trouble extending scipy.stats.multivariate_normal [__init__() takes from 1 to 2 positional arguments]

I'm trying to extend a base class (scipy.stats.multivariate_normal) but I'm having some difficulty with calling


from scipy.stats import multivariate_normal
class MVGaussian(multivariate_normal):
def __init__(self, *args, **kwargs): # I want base class to handle all the arguments
super(MVGaussian, self).__init__(*args,**kwargs)
# def myMethod ...

I always get
TypeError: __init__() takes from 1 to 2 positional arguments but 4 were given

I've also tried
, or just
multivariate_normal.__init__(*args, **kwargs)


You're trying to subclass multivariate_normal, but it isn't a class, it's an instance of a class that has defined a __call__ method:

In [24]: isinstance(multivariate_normal, scipy.stats._multivariate.multivariate_normal_gen)
Out[24]: True

You'll have to subclass scipy.stats._multivariate.multivariate_normal_gen and make an instance of your class for it to behave like multivariate_normal:

from scipy.stats._multivariate import multivariate_normal_gen

class MVGaussian(multivariate_normal_gen):
    def __init__(self, *args, **kwargs):
        super(MVGaussian, self).__init__(*args,**kwargs)

mv_gaussian = MVGaussian()

Look at the source code for more info: