我想检查一个 scipy
分布是离散的还是连续的。测试应该适用于对象是来自命名分布的冻结分布对象,还是自定义分布的实例。rv_discrete
或 rv_continuous
的分布。
我首先想到的是检查变量的类型,但这似乎并不能整齐地对应连续与离散。例如,这里有四个分布。
from scipy.stats import *
import numpy as np
dist_norm = norm(10, 2)
dist_poisson = poisson(10)
class continuous_gen(rv_continuous):
def _pdf(self, x, *args):
if x >= 0 and x <= 1:
return 1
else:
return 0
dist_contin = continuous_gen()
xk = np.arange(7)
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2)
dist_discrete = rv_discrete(values=(xk, pk))
而这里是它们的类型。
type(dist_norm)
Out[59]: scipy.stats._distn_infrastructure.rv_frozen
type(dist_poisson)
Out[60]: scipy.stats._distn_infrastructure.rv_frozen
type(dist_contin)
Out[61]: __main__.continuous_gen
type(dist_discrete)
Out[62]: scipy.stats._distn_infrastructure.rv_sample
目前我已经实现了一个 is_discrete()
功能,试图访问 pmf()
方法(只有离散分布才有),但我不知道这是不是最干净或最可靠的方法。有没有更好的方法?
def is_discrete(dist):
try:
_ = dist.pmf(0)
return True
except:
return False
你可以使用 是实例 内置函数来定义自定义检查。
from scipy.stats import poisson, norm, rv_discrete, rv_continuous
def is_discrete(dist):
if hasattr(dist, 'dist'):
return isinstance(dist.dist, rv_discrete)
else: return isinstance(dist, rv_discrete)
def is_continuous(dist):
if hasattr(dist, 'dist'):
return isinstance(dist.dist, rv_continuous)
else: return isinstance(dist, rv_continuous)
这将导致:
class continuous_gen(rv_continuous):
def _pdf(self, x, *args):
if x >= 0 and x <= 1:
return 1
else:
return 0
dist_contin = continuous_gen()
dist_poisson = poisson(10)
is_discrete(dist_contin)
#False
is_continuous(dist_contin)
#True
is_discrete(dist_poisson)
#True
is_continuous(dist_poisson)
#False