Getting scipy.stats distribution parameter names

I am writing a script to find the most suitable dataset distribution using scipy.stats. First I have a list of distribution names for which I repeat:

dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm']
for d in dists:
    dist = getattr(scipy.stats, d)
    ps = dist.fit(selected_data)
    errors.loc[d,['D-Value','P-Value']] = kstest(selected.tolist(), d, args=ps)
    errors.loc[d,'Params'] = ps

Now, after this loop, I choose the minimum value of D to get the best fit. Now each distribution returns a specific set of parameters in ps, each with their names, etc. (For example, for "alpha" it will be alpha, while for "normal" they will also have std).

Is there a way to get evaluation parameter names in scipy.stats?

Thank you in advance

+5
source share
2 answers

, ev-br , - .

>>> from scipy import stats
>>> dists = ['alpha', 'anglit', 'arcsine', 'beta', 'betaprime', 'bradford', 'norm']
>>> for d in dists:
...     dist = getattr(scipy.stats, d)
...     dist.name, dist.shapes
... 
('alpha', 'a')
('anglit', None)
('arcsine', None)
('beta', 'a, b')
('betaprime', 'a, b')
('bradford', 'c')
('norm', None)

, None , , .

+1

:

import sys
import scipy.stats

def list_parameters(distribution):
    """List parameters for scipy.stats.distribution.
    # Arguments
        distribution: a string or scipy.stats distribution object.
    # Returns
        A list of distribution parameter strings.
    """
    if isinstance(distribution, str):
        distribution = getattr(scipy.stats, distribution)
    if distribution.shapes:
        shapenames = [name.strip() for name in distribution.shapes.split(',')]
    else:
        shapenames = []
    if distribution.name in scipy.stats._discrete_distns._distn_names:
        shapenames += ['loc']
    elif distribution.name in scipy.stats._continuous_distns._distn_names:
        shapenames += ['loc', 'scale']
    else:
        sys.exit("Distribution name not found in discrete or continuous lists.")
    return shapenames

.

0

All Articles