简体   繁体   中英

How to get distribution class type from scipy.stats object?

I want to calculate the entropy of some samples from a distribution. So we might have some distributions a and b and some corresponding random samples.

from scipy.stats import norm, beta

a = norm(0, 1)
a_samples = a.rvs(10)
a_samples

b = beta(1, 10)
b_samples = b.rvs(10)
b_samples

You can calculate the entropy of these samples, but it looks like you have to manually specify the distribution types.

dist = norm
dist.entropy(*dist.fit(a_samples))

dist = beta
dist.entropy(*dist.fit(b_samples))

But is there a way of doing this where I do not have to manually specify the distribution type? In other words, is there a way of getting that from the a and b distributions? When I look at the class type, they are scipy.stats._distn_infrastructure.rv_frozen . Looking at the properties of a or b there doesn't seem to be anything which tells you which distribution type they are.

So an ideal solution would be something that allows you to get the entropy from the distribution object and the samples:

def get_entropy(distribution_object, samples):
    dist = #  Get distribution class from distribution_object somehow here !
    return dist.entropy(*dist.fit(samples))

a_entropy = get_entropy(a, a_samples)
b_entropy = get_entropy(b, b_samples)

You can simply do:

def get_entropy(obj, samples):
    return obj.dist.entropy(*obj.dist.fit(samples))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM