用Python / Sicpy拟合仲裁函数(模型)

时间:2019-01-18 09:03:51

标签: python scikit-learn scipy data-fitting model-fitting

我想知道是否有可能使用scipy.optimize.curve_fit或任何其他不同的即用方法来适应某些任意定义的函数(模型),例如:

def model_smooth_ramp(x, x0, x1, a, b, s):
    y = np.piecewise(x, [(x < x0), (x0 <= x) * (x < x1), (x >= x1)], [0, lambda x: (x - x0) *(1/(x1-x0)), 1])
    return a * smooth(y, window_len=s) + b

位置:

def smooth(x, window_len=6, window='flat'):
    if x.ndim != 1:
        raise ValueError("smooth only accepts 1 dimension arrays.")

    if x.size < window_len:
        raise ValueError("Input vector needs to be bigger than window size.")

    if window_len < 3:
        return x

    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError("Window is one of: 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")

    s = np.r_[x[window_len - 1:0:-1], x, x[-2:-window_len - 1:-1]]

    if window == 'flat':  # moving average
        w = np.ones(window_len, 'd')
    else:
        w = eval('np.' + window + '(window_len)')
    y = np.convolve(w / w.sum(), s, mode='valid')
    return y[(int(window_len / 2 - 1)):-(int(window_len / 2))]

0 个答案:

没有答案