本文整理汇总了Python中symfit.Fit.model方法的典型用法代码示例。如果您正苦于以下问题:Python Fit.model方法的具体用法?Python Fit.model怎么用?Python Fit.model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类symfit.Fit
的用法示例。
在下文中一共展示了Fit.model方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_2D_fitting
# 需要导入模块: from symfit import Fit [as 别名]
# 或者: from symfit.Fit import model [as 别名]
def test_2D_fitting(self):
"""
Makes sure that a scalar model with 2 independent variables has the
proper signature, and that the fit result is of the correct type.
"""
xdata = np.random.randint(-10, 11, size=(2, 400))
zdata = 2.5*xdata[0]**2 + 7.0*xdata[1]**2
a = Parameter()
b = Parameter()
x = Variable()
y = Variable()
new = a*x**2 + b*y**2
fit = Fit(new, xdata[0], xdata[1], zdata)
result = fit.model(xdata[0], xdata[1], 2, 3)
self.assertIsInstance(result, tuple)
for arg_name, name in zip(('x', 'y', 'a', 'b'), inspect_sig.signature(fit.model).parameters):
self.assertEqual(arg_name, name)
fit_result = fit.execute()
self.assertIsInstance(fit_result, FitResults)
示例2: parameters
# 需要导入模块: from symfit import Fit [as 别名]
# 或者: from symfit.Fit import model [as 别名]
:param x: Independent variable
:param f: Frequency of the fourier series
"""
# Make the parameter objects for all the terms
a0, *cos_a = parameters(','.join(['a{}'.format(i) for i in range(0, n + 1)]))
sin_b = parameters(','.join(['b{}'.format(i) for i in range(1, n + 1)]))
# Construct the series
series = a0 + sum(ai * cos(i * f * x) + bi * sin(i * f * x)
for i, (ai, bi) in enumerate(zip(cos_a, sin_b), start=1))
return series
x, y = variables('x, y')
w, = parameters('w')
model_dict = {y: fourier_series(x, f=w, n=3)}
print(model_dict)
# Make step function data
xdata = np.linspace(-np.pi, np.pi)
ydata = np.zeros_like(xdata)
ydata[xdata > 0] = 1
# Define a Fit object for this model and data
fit = Fit(model_dict, x=xdata, y=ydata)
fit_result = fit.execute()
print(fit_result)
# Plot the result
plt.plot(xdata, ydata)
plt.plot(xdata, fit.model(x=xdata, **fit_result.params).y, ls=':')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
示例3: test_error_advanced
# 需要导入模块: from symfit import Fit [as 别名]
# 或者: from symfit.Fit import model [as 别名]
def test_error_advanced(self):
"""
Compare the error propagation of Fit against
NumericalLeastSquares.
Models an example from the mathematica docs and try's to replicate it:
http://reference.wolfram.com/language/howto/FitModelsWithMeasurementErrors.html
"""
data = [
[0.9, 6.1, 9.5], [3.9, 6., 9.7], [0.3, 2.8, 6.6],
[1., 2.2, 5.9], [1.8, 2.4, 7.2], [9., 1.7, 7.],
[7.9, 8., 10.4], [4.9, 3.9, 9.], [2.3, 2.6, 7.4],
[4.7, 8.4, 10.]
]
xdata, ydata, zdata = [np.array(data) for data in zip(*data)]
# errors = np.array([.4, .4, .2, .4, .1, .3, .1, .2, .2, .2])
a = Parameter('a', 3.0)
b = Parameter('b', 0.9)
c = Parameter('c', 5.0)
x = Variable('x')
y = Variable('y')
z = Variable('z')
model = {z: a * log(b * x + c * y)}
const_fit = Fit(model, xdata, ydata, zdata, absolute_sigma=False)
self.assertEqual(len(const_fit.model(x=xdata, y=ydata, a=2, b=2, c=5)), 1)
self.assertEqual(
const_fit.model(x=xdata, y=ydata, a=2, b=2, c=5)[0].shape,
(10,)
)
self.assertEqual(len(const_fit.model.eval_jacobian(x=xdata, y=ydata, a=2, b=2, c=5)), 1)
self.assertEqual(
const_fit.model.eval_jacobian(x=xdata, y=ydata, a=2, b=2, c=5)[0].shape,
(3, 10)
)
self.assertEqual(len(const_fit.model.eval_hessian(x=xdata, y=ydata, a=2, b=2, c=5)), 1)
self.assertEqual(
const_fit.model.eval_hessian(x=xdata, y=ydata, a=2, b=2, c=5)[0].shape,
(3, 3, 10)
)
self.assertEqual(const_fit.objective(a=2, b=2, c=5).shape,
tuple())
self.assertEqual(
const_fit.objective.eval_jacobian(a=2, b=2, c=5).shape,
(3,)
)
self.assertEqual(
const_fit.objective.eval_hessian(a=2, b=2, c=5).shape,
(3, 3)
)
self.assertNotEqual(
const_fit.objective.eval_hessian(a=2, b=2, c=5).dtype,
object
)
const_result = const_fit.execute()
fit = Fit(model, xdata, ydata, zdata, absolute_sigma=False, minimizer=MINPACK)
std_result = fit.execute()
self.assertEqual(const_fit.absolute_sigma, fit.absolute_sigma)
self.assertAlmostEqual(const_result.value(a), std_result.value(a), 4)
self.assertAlmostEqual(const_result.value(b), std_result.value(b), 4)
self.assertAlmostEqual(const_result.value(c), std_result.value(c), 4)
# This used to be a tighter equality test, but since we now use the
# Hessian we actually get a more accurate value from the standard fit
# then for MINPACK. Hence we check if it is roughly equal, and if our
# stdev is greater than that of minpack.
self.assertAlmostEqual(const_result.stdev(a) / std_result.stdev(a), 1, 2)
self.assertAlmostEqual(const_result.stdev(b) / std_result.stdev(b), 1, 1)
self.assertAlmostEqual(const_result.stdev(c) / std_result.stdev(c), 1, 2)
self.assertGreaterEqual(const_result.stdev(a), std_result.stdev(a))
self.assertGreaterEqual(const_result.stdev(b), std_result.stdev(b))
self.assertGreaterEqual(const_result.stdev(c), std_result.stdev(c))