简体   繁体   中英

Type check scipy.stats distributions with isinstance

I have several scipy.stats distributions in a list. I want to check if each distribution is eg uniform, normal or something else by isinstance. However, the type of all these distributions seem to be scipy.stats._distn_infrastructure.rv_continuous_frozen. How can I separate a uniform from a normal distribution?

A minimum example is below.

import scipy.stats
dist_u = scipy.stats.uniform(loc = 0, scale = 2) #an example distribution
# isinstance(dist_u, scipy.stats.uniform) #this does not work
isinstance(dist_u, type(scipy.stats.uniform(0,1))) #This returns True
isinstance(dist_u, type(scipy.stats.norm(0,1))) #This also returns True, I expected it to return False
type(dist_u) # gives scipy.stats._distn_infrastructure.rv_continuous_frozen

I used python 3.10 and scipy 1.9.0.

Answering my own question in case other need to know.

dists = [scipy.stats.uniform(loc=-2, scale = 2), scipy.stats.norm(0, 1)] #some example distributions
for dist in dists:
    if isinstance(dist.dist, type(scipy.stats.uniform)):
        print("unif")
    elif isinstance(dist.dist, type(scipy.stats.norm)):
        print("norm")

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