1

我想使用symfit对具有共享变量的数据集进行全局拟合。我有一个 numpy 数组xdata,它对于所有ydata_i也是 numpy 数组的数据集都很常见。

按照文档中的示例a 可以设置变量、参数和模型,但我无法设置拟合: fit = Fit(model, x_1=xdata_1, x_2=xdata_2, ..., y_1=ydata_1, y_2=ydata_2, ...)

对于少数数据集,我可以手动编写代码或复制/粘贴它,但我有数百个数据集,我希望我可以避免手动输入代码。我尝试使用列表[xdata, ydata_1, ydata2, ...][xdata, ydata_1, xdata, ydata2, ...]数组,但这似乎不是正确的方法。

有谁知道ordered_data的结构/类型应该是什么样子。谢谢

4

2 回答 2

2

对于大量数据集,您可以使用字典:

data = {'x_1': xdata_1, 'x_2': xdata_2, ..., 'y_1': ydata_1, 'y_2': ydata_2, ...}
fit = Fit(model, **data)

这样,它将最终named_data代替,这是首选。祝你好运!

ps如果您正在使用此类大型模型,您可能还需要考虑使用 aJacobianModel或 a代替默认模型,因为为此类模型计算 jacobian 和 hessian 可能既昂贵又不必要。CallableModel

于 2019-10-12T10:49:59.833 回答
1

对不起,我再次需要你的帮助。我最终得到一个错误wrapped_func() keywords must be strings'Variable' object has no attribute 'symbol'. 我认为这是一个简单的问题,但我不明白这一点。你会看看下面的例子吗?

import numpy as np
import symfit as sf

# creating the data
freq = 10 * np.linspace(0.1,0.3,2)
phase = np.pi * np.linspace(0,0.3,2)
offset = 1.0
amplitude = 0.1

# x - array
x_array = np.arange(0,20,0.02)
# create dataset
dataset = [offset + amplitude * np.cos(freq * x_array + phase) + np.random.normal(size=len(x_array), scale=0.01) for freq,phase in zip(freq,phase)]

# independent variables
xs = sf.variables(', '.join('x_{}'.format(i) for i in range(len(dataset))))
# dependent variables
ys = sf.variables(', '.join('y_{}'.format(i) for i in range(len(dataset))))
# coupled parameters
amp, off = sf.parameters('amp, off', value=[1.0,0.1])
# decoupled parameters
freqc = sf.parameters(', '.join('f_{}'.format(i) for i in range(len(dataset))),value=freq)
phasec = sf.parameters(', '.join('p_{}'.format(i) for i in range(len(dataset))),value=phase)

# setup model
model_dict = {y : off + amp * sf.cos(freq * x + phase) for x, y, freq, phase in zip(xs, ys, freqc, phasec) }
# create dataset_dict
xdata = [x_array for i in range(len(dataset))] # just to have equal length of xdata list and y-data list
#data_dict = {x : data for x, data in zip(xs + ys, xdata + dataset)} # error 'wrapped_func() keywords must be strings'
#data_dict = {str(x) : data for x, data in zip(xs + ys, xdata + dataset)} # error 'Variable' object has no attribute 'symbol'
data_dict = {'x_0': x_array, 'x_1': x_array, 'y_0': dataset[0], 'y_1': dataset[1]} # error 'Variable' object has no attribute 'symbol'
# # do the fit
fit = sf.Fit(model_dict, **data_dict)
fit_result = fit.execute()

我通过 Anconda 在 Windows PC 上使用 Python 3.7.4。Symfit 版本:0.4.6,Sympy 版本:1.4

Traceback (most recent call last):

  File "C:/Users/dummy/Documents/Scripts/Python/Scripts/SymFit_example.py", line 42, in <module>
    fit_result = fit.execute()

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\fit.py", line 1537, in execute
    minimizer_ans = self.minimizer.execute(**minimize_options)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\minimizers.py", line 359, in execute
    return super(ScipyGradientMinimize, self).execute(jacobian=self.wrapped_jacobian, **minimize_options)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\support.py", line 355, in wrapped_func
    return func(*bound_args.args, **bound_args.kwargs)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\minimizers.py", line 296, in execute
    **minimize_options

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\_minimize.py", line 594, in minimize
    return _minimize_bfgs(fun, x0, args, jac, callback, **options)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\optimize.py", line 996, in _minimize_bfgs
    gfk = myfprime(x0)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\optimize.py", line 326, in function_wrapper
    return function(*(wrapper_args + args))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\optimize.py", line 756, in approx_fprime
    return _approx_fprime_helper(xk, f, epsilon, args=args)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\optimize.py", line 690, in _approx_fprime_helper
    f0 = f(*((xk,) + args))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\scipy\optimize\optimize.py", line 326, in function_wrapper
    return function(*(wrapper_args + args))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\minimizers.py", line 273, in wrapped_func
    return np.array(func(**parameters))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\support.py", line 355, in wrapped_func
    return func(*bound_args.args, **bound_args.kwargs)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\objectives.py", line 151, in __call__
    evaluated_func = self.model(**jac_kwargs)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\fit.py", line 334, in __call__
    return Ans(*self.eval_components(**bound_arguments.arguments))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\fit.py", line 296, in eval_components
    return [expr(*args, **kwargs) for expr in self.numerical_components]

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\support.py", line 217, in __get__
    setattr(obj, self.cache_attr, self.fget(obj))

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\fit.py", line 457, in numerical_components
    return [sympy_to_py(expr, self.independent_vars, self.params) for expr in self.values()]

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\fit.py", line 457, in <listcomp>
    return [sympy_to_py(expr, self.independent_vars, self.params) for expr in self.values()]

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\symfit\core\support.py", line 91, in sympy_to_py
    return lambdify((vars + params), func, modules='numpy', dummify=False)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\utilities\lambdify.py", line 767, in lambdify
    funcstr = funcprinter.doprint(funcname, args, expr)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\utilities\lambdify.py", line 977, in doprint
    argstrs, expr = self._preprocess(args, expr)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\utilities\lambdify.py", line 1039, in _preprocess
    s = self._argrepr(arg)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\printing\codeprinter.py", line 100, in doprint
    lines = self._print(expr).splitlines()

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\printing\printer.py", line 287, in _print
    return getattr(self, printmethod)(expr, **kwargs)

  File "C:\Users\dummy\Anaconda3\envs\spyder-beta\lib\site-packages\sympy\printing\codeprinter.py", line 344, in _print_Variable
    return self._print(expr.symbol)

AttributeError: 'Variable' object has no attribute 'symbol'
于 2019-10-14T15:25:13.523 回答