You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用curve_fit时自定义双参数函数仅返回一个优化参数的问题求助

使用curve_fit时自定义双参数函数仅返回一个优化参数的问题求助

我现在碰到了一个棘手的问题:明明我定义的函数包含td和tr两个参数,但用curve_fit做拟合时,只输出了一个优化后的参数值,而且这个数值完全不符合预期——毕竟我用的是已知参数的测试数据集来验证的。

另外还有个小插曲:我必须在函数里加上return conv这一行,不然就会弹出操作数错误,提示同时存在float和NoneType两种类型。

以下是我目前的完整代码:

import pandas as pd
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import math
from scipy.optimize import curve_fit

data = pd.read_excel('data.xlsx')
time = data["time"]
TA = data["amp"]
y0 = 0
t0 = 0.13 #put in t0 here
IRS = 0.1 #put IRS here
mx = 1 #put max/min amp of raw here
l = 1
pi = 3.14
a = 1 #amplitude for gaussian

def func(td, tr):
    t = np.array(time) #building signal test code
    signal = np.array([(l*(np.exp(-(t-t0)/td)-np.exp(-(t-t0)/tr)))]) #creates signal
    np.nan_to_num(signal, copy=False, nan=0.00, posinf=0, neginf=0)
    signal[signal < 0] = 0 #replaces all negative numbers with 0
    signal = signal.flatten() #makes 1D array
    #uncomment following line if bleach and not IA
    #signal = signal*-1
    n = np.size(time) #convolution while moving Gaussian center to each time value
    matrix = []
    for i in time:
        tc = i
        for j in time:
            gauss = np.array(a*np.exp(-((j-tc)/IRS)**2))
            product = np.convolve(signal, gauss, mode='full')
            matrix.append(product)
    matrix = np.array(matrix)
    conv = np.sum(matrix, axis=0)
    conv = np.delete(conv, np.s_[n:])
    
    max = np.nanmax(conv)
    conv = (conv/max)*mx
    return conv

popt, pcov = curve_fit(func, time, TA)
print(popt)

备注:内容来源于stack exchange,提问作者Rena Kramer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:42:59