繁体   English   中英

尝试使用Python将指数分布拟合到数据时出错

[英]Error when trying to fit an exponential distribution to data using Python

我正在努力拟合指数分布,例如:

p(x)= lambda * exp(-lambda * x)

到以下数据集:

y = [1.0 0.6477204485713643 0.5740407723417333 0.5615636591299797
 0.4387673913917531 0.40091370516517705 0.3557117110132781
 0.3511355853065214 0.340307340921202 0.30971826267036 0.292891403862456
 0.2467743233580864 0.2266046083821163 0.21720917030949116
 0.20043871451165768 0.1973221582209641 0.15362640051482043
 0.15354057195269372 0.14662893640148297 0.14317539711005514
 0.11807717376369412 0.10370859482950556 0.10093677560919409
 0.09749966756865966 0.08933073156729388 0.0804544088313465
 0.0761310467698398 0.0761310467698398 0.0560272314739388
 0.054424966400080085 0.04830594736471987 0.04614770045710077
 0.04596980503252967 0.03156586444228013 0.031247687808906088
 0.02034784444410579 0.01648966866320673 0.016039148970427325
 0.014211493240353406 0.011831876417890502 0.010697649767138983
 0.008203782016905766 0.00668207490883694 0.006385472749205766
 0.004654267333597479 0.003107036846521259 0.0022789222224917083
 0.0018466098136964094 0.0012186574760257173 0.0009770057014847977
 0.0007631859286121455 0.0006151192857729099 0.0006139225669891776
 0.0005757713710793208 0.00037491693850308307 0.00037491693850308307
 0.00020552292850767692 9.51226931808377e-05]

我尝试了以下方法:

from scipy.stats import expon

loc, scale = expon.fit(y, floc=0)

这应该超级简单,但是由于某些原因,我收到以下错误:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

实际上,感谢rojanjosh的注释,输入是一个object -type。

后者不适用于fit函数。 因此,必须将其转换为:

解决方案1:

loc, scale = expon.fit(y.astype(np.float64), floc=0)

解决方案2:

loc, scale = expon.fit(y.tolist(), floc=0)

确保检查的输入型y使用y.dtype它传递给前fit函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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