Fitting a curve with a pivot point Python

不打扰是莪最后的温柔 提交于 2019-12-05 21:03:20

A possible stepwise parametrisation of your model in log-space is something like:

(x>q)*((x-q)*a)+(x<q)*((x-q)*c)+b

Where q is the position of the kink, a, and c are the slopes of both parts and b is a global y-offset. Since the model has a discontinuity a gradient based minimizer might not be the best choice to find a best fit. Nevertheless I tried both scipy.optimize.leastsq and scipy.odr and got good results.

You can take a look at piecewise and pwlf. Below is an example using piecewise which can automatically fit multiple segments.

from piecewise.regressor import piecewise
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15,16,17,18], dtype=float)
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 98.36, 112.25, 126.14, 140.03,120,112,110])

model = piecewise(x, y)

Evaluate'model' to get the following output:

FittedModel with segments:
* FittedSegment(start_t=1.0, end_t=7.0, coeffs=(2.9999999999999996, 2.0000000000000004))
* FittedSegment(start_t=7.0, end_t=16.0, coeffs=(-68.2972222222222, 13.888333333333332))
* FittedSegment(start_t=16.0, end_t=18.0, coeffs=(198.99999999999997, -5.000000000000001))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!