尝试使用 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 类型。

后者不适用于拟合函数。所以,这个要换算:

解决方案 #1:

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

解决方案 #2:

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

确保在将 y 传递给 fit 函数之前使用 y.dtype 检查其输入类型。