How to programatically get parameter names and values in scipy

Is there any way to get the parameters of a distribution? I know almost every distribution has "loc" and "scale" but theres differences between them, for example alpha has "a", beta has "a" ,"b".

What i want to do is programatically print(after fiting a distribution) key value pairs of parameter,value.

But i dont want to write a print routine for every possible distribution.

2 answers

  • answered 2017-11-23 08:03 Paul Panzer

    inspecting the _pdf method appears to work:

    import inspect
    
    # keys
    [p for p in inspect.signature(stats.beta._pdf).parameters if not p=='x']
    # ['a', 'b']
    
    # keys and values
    dist = stats.alpha(a=1)
    inspect.signature(stats.alpha._pdf).bind('x', *dist.args, **dist.kwds).arguments
    # OrderedDict([('x', 'x'), ('a', 1)])
    # 'x' probably doesn't count as a parameter
    

  • answered 2017-11-24 06:55 Luis Leal

    In the end what i did was:

    parameter_names = [p for p in inspect.signature(distribution._pdf).parameters if not p=='x'] + ["loc","scale"]
    parameters = distribution.fit(pd_series)
    distribution_parameters_dictionary =dict(zip(parameter_names,parameters))
    

    Where pd_series is a pandas series of the data being fitted.