python编程分段函数,在Python中拟合分段函数

I'm trying to fit a piecewise defined function to a data set in Python. I've searched for quite a while now, but I haven't found an answer whether it is possible or not.

To get an impression of what I am trying to do, look at the following example (which is not working for me). Here I'm trying to fit a shifted absolute value function (f(x) = |x-p|) to a dataset with p as the fit parameter.

import scipy.optimize as so

import numpy as np

def fitfunc(x,p):

if x>p:

return x-p

else:

return -(x-p)

fitfunc = np.vectorize(fitfunc) #vectorize so you can use func with array

x=np.arange(1,10)

y=fitfunc(x,6)+0.1*np.random.randn(len(x))

popt, pcov = so.curve_fit(fitfunc, x, y) #fitting routine that gives error

Is there any way of accomplishing this in Python?

A way of doing this in R is :

# Fit of a absolute value function f(x)=|x-p|

f.lr

ifelse(x>p, x-p,-(x-p))

}

x

y

plot(y ~ x)

fit.lr

summary(fit.lr)

coefficients(fit.lr)

p.fit

x_fine

lines(x_fine,f.lr(x_fine,p.fit),type='l',col='red')

lines(x,f.lr(x,6),type='l',col='blue')

After even more research I found a way of doing it. In this solution, I don't like the fact that I have to define the error function myself. Further I'm not really sure why it has to be in this lambda-style. Therefore any kind of suggestions or more sophisticated solutions are very welcome.

import scipy.optimize as so

import numpy as np

import matplotlib.pyplot as plt

def fitfunc(p,x): return x - p if x > p else p - x

def array_fitfunc(p,x):

y = np.zeros(x.shape)

for i in range(len(y)):

y[i]=fitfunc(x[i],p)

return y

errfunc = lambda p, x, y: array_fitfunc(p, x) - y # Distance to the target function

x=np.arange(1,10)

x_fine=np.arange(1,10,0.1)

y=array_fitfunc(6,x)+1*np.random.randn(len(x)) #data with noise

p1, success = so.leastsq(errfunc, -100, args=(x, y), epsfcn=1.) # -100 is the initial value for p; epsfcn sets the step width

plt.plot(x,y,'o') # fit data

plt.plot(x_fine,array_fitfunc(6,x_fine),'r-') #original function

plt.plot(x_fine,array_fitfunc(p1[0],x_fine),'b-') #fitted version

plt.show()

解决方案

To finish this up here, I'll share my own final solution to the problem. In order to stay close to my original question, you just have to define the vectorized function yourself and not use np.vectorize.

import scipy.optimize as so

import numpy as np

def fitfunc(x,p):

if x>p:

return x-p

else:

return -(x-p)

fitfunc_vec = np.vectorize(fitfunc) #vectorize so you can use func with array

def fitfunc_vec_self(x,p):

y = np.zeros(x.shape)

for i in range(len(y)):

y[i]=fitfunc(x[i],p)

return y

x=np.arange(1,10)

y=fitfunc_vec_self(x,6)+0.1*np.random.randn(len(x))

popt, pcov = so.curve_fit(fitfunc_vec_self, x, y) #fitting routine that gives error

print popt

print pcov

Output:

[ 6.03608994]

[[ 0.00124934]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值