使用寄宿生作为拟合参数拟合两个不同的函数 [英] Fit of two different functions with boarder as fit parameter

查看:74
本文介绍了使用寄宿生作为拟合参数拟合两个不同的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python中的简单fit函数有疑问.我正在尝试在数据集上拟合两个不同的函数,并且两个体系之间的边界也应该是拟合参数.我很天真地尝试这样的事情:

I have a question about a simple fit function in Python. I am trying to fit two different functions on a data set and the border between the two regimes should be also a fit parameter. Very naively I was trying something like this:

def FitFunc(x, a, b, c, d, e, border):
    if x < border:
        return a * x + b
    if x > border:
        return c * x**2 + d * x + e

但是我遇到了ValueError:

But I got a ValueError:

系列的真值不明确.使用a.empty(),a.bool(),a.item(),a.any()或a.all()

The truth value of a Series is ambiguous. Use a.empty(), a.bool(), a.item(), a.any() or a.all()

我知道您无法将我的x数组与一个整数值进行比较(该整数值甚至没有分配给固定值,这应该由curve_fit过程完成).

I understand that you can not compare my x array to an integer value (which is not even assigned to a fixed value, this should be done by the curve_fit procedure).

我无法找到这个简单问题的解决方案.有人可以帮我吗?

I was not able to find a solution for this supposedly simple problem. Can someone help me out of this?

提前谢谢!

我构建了一个示例:对于x <5的数据,可以通过线性函数(y = x-3)完美描述;对于x> 5,这可以通过平方函数(y = x ** 2/5-3 * x/5)完美描述).假设您不知道x是完美的边界",有没有办法让curve_fit找出?

I constructed an example: For the data with x<5 this is perfectly described by a linear function (y=x-3), For x>5 this is perfectly described by a square function (y=x**2/5-3*x/5). Assume you don't know that x is the perfect 'border', is there a way to let curve_fit find out?

推荐答案

错误消息是说将数组( x )的值与标量值( border )是模棱两可的.您是说 x any 值是否小于 border ,还是 x 的 all 值?/code>小于 border ?

The error message is saying that comparing the values of an array (x) with a scalar value (border) is ambiguous. Do you mean if any values of x are less than border or if all values of x are less than border?

我怀疑您真正想要的是numpy的 where 函数,这有点像循环遍历 x 并决定仅逐点使用哪个表达式更快:

I suspect that what you really want is numpy's where function, which is sort of like looping over x and deciding which expression to use point-by-point, only much faster:

def FitFunc(x, a, b, c, d, e, border):
    out = a * x + b
    out[np.where(x > border)] = c * x**2 + d * x + e
    return out

我认为这就是您想要的,并且应该根据 border 选择合适的功能形式.

I think that is what you want, and it should work to select the proper functional form based on border.

但是,我要提醒您,它可能不会您想要的.也就是说,您似乎希望 border 是一个合适的变量.

However, I would caution that it may not do what you want. That is, it looks like you intend border to be a fitting variable.

scipy optimize 例程中的拟合变量(以及大多数曲线拟合"上下文)需要为连续浮点变量.您的 border 变量将用作 x 索引的离散整数值,以更改功能形式.拟合算法将首先选择一个值(假设为5.0),然后对该值进行非常小的更改(例如为5.0000002).这不会改变两个函数形式之间的断点的位置,并且拟合将决定 border 不会改变拟合.我希望我对此有所了解.

Fitting variables in scipy optimize routines (and most "curve fitting" contexts) need to be continuous floating point variables. Your border variable would be used as a discrete, integer value for the index of x to change functional form. The fitting algorithm will first choose a value (let's say 5.0), then make a very small change in that value (to, say, 5.0000002). That will not change where the breakpoint between your two functional forms is, and the fit will decide that border does not change the fit. I wish I knew a way around this.

这篇关于使用寄宿生作为拟合参数拟合两个不同的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆