将 statsmodels 回归参数格式化为文本字符串以获得拟合方程

Formatting statsmodels regression parameters to a text string to obtain the fitted equation

我正在尝试使用 statsmodel 结果屏幕的合适参数构建文本字符串。到目前为止,我已经做到了:

results.params

const    18.352876
x         2.729230
x^2      -0.284947
x^3       0.017321
x^4      -0.000359
dtype: float64

"".join(['{coef}{var}+'.format(coef=i, var=j) for i,j in zip(results.params.values,results.params.index.values)])

'18.35287604545434const+2.7292297979902465x+-0.2849465918536549x^2+0.017320680602094476x^3+-0.00035853905617123927x^4+'

这是我目前有限的知识所能做的最好的,但显然还有改进的余地。

有没有更好的方法来实现更优雅的结果,例如 y=-3.59E-4x^4 +0.017x^3 -0.285x^2 +2.729x +18.353 使用 built-in来自 statsmodels 的方法还是上述代码的改进版本?我的目标是获得一个字符串,我可以将其用作绘图跟踪的标题或名称

先谢谢你。

您可以按照以下方式格式化您的字符串 f'{i:.2E}{j}+' 并以科学计数法显示它们