到系列/数据框,pandas,numpy,lmfit"/>

首页 > 解决方案 > 无法强制列表到系列/数据框

问题描述

我正在尝试使用带有以下代码的 lmfit 来拟合 x 和 y 数据

mod = ExpressionModel(
    self._expr,
    independent_vars=[x],
)
# pars = mod.guess(raw_y, x=raw_x)
if guess:
    pars = mod.make_params(**guess)
else:
    # TODO custom error needs to be implemented
    print("No guess was passed")
    raise Exception
# thinnen the fit data to avoid over fitting
# x_fit = np.array_split(x_data.iloc[::2], 2)[0]
# y_fit = np.array_split(y_data.iloc[::2], 2)[0]
print(x_data)
x_fit = np.array(x_data.to_numpy(copy=True)).transpose()
y_fit = y_data
print(x_fit.shape)
print(x_fit.T)
print(y_fit)
stuff = {x_data.name:x_fit}
# stuff = {x_data.name:x_data}
out = mod.fit(y_fit, pars,**stuff)

以下是我拥有的数据的打印:

0    3.0
1    1.5
2    1.0
Name: r, dtype: float64
(3,)
[3.  1.5 1. ]
         na
0  0.050000
1  0.025000
2  0.016667

我收到以下错误:

文件“/home/bob/Programming/Lab-Tool/env/lib/python3.8/site-packages/pandas/core/frame.py”,第 6864 行,在 _arith_method self, other = ops.align_method_FRAME(self, other , 轴, flex=True, level=None) 文件“/home/bob/Programming/Lab-Tool/env/lib/python3.8/site-packages/pandas/core/ops/init .py ”,第 277 行,在 align_method_FRA ME 中引发 ValueError(ValueError: Unable to coerce list of <class 'pandas.core.series.Series'> to Series/DataFrame

或者如果我使用 x_fit:

raise ValueError(ValueError: Unable to force to list of <class 'numpy.ndarray'> to Series/DataFrame

有人知道我的错误吗?

标签: pandasnumpylmfit

解决方案


推荐阅读