curve_fit无法正常使用4个参数

时间:2016-09-27 04:33:49

标签: python numpy matplotlib scipy curve-fitting

运行以下代码,

x = np.array([50.849937, 53.849937, 56.849937, 59.849937, 62.849937, 65.849937, 68.849937, 71.849937, 74.849937, 77.849937, 80.849937, 83.849937, 86.849937, 89.849937, 92.849937])
y = np.array([410.67800, 402.63800, 402.63800, 386.55800, 330.27600, 217.71400, 72.98990, 16.70860, 8.66833, 40.82920, 241.83400, 386.55800, 394.59800, 394.59800, 402.63800])
def f(om, a, i , c):
       return a - i*np.exp(- c* (om-74.)**2)
par, cov = curve_fit(f, x, y)
stdev = np.sqrt(np.diag(cov) )

生成此图,

enter image description here

使用以下参数和标准差:

par =   [ 4.09652163e+02, 4.33961227e+02, 1.58719772e-02]
stdev = [ 1.46309578e+01, 2.44878171e+01, 2.40474753e-03]

但是,在尝试将此数据拟合到以下函数时:

def f(om, a, i , c, omo):
       return a - i*np.exp(- c* (om-omo)**2)

它不起作用,它产生

的标准偏差
stdev = [inf, inf, inf, inf, inf]

有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

看起来它没有收敛(请参阅thisthis)。尝试添加初始条件

par = [ 4.11892318e+02, 4.36953868e+02, 1.55741131e-02, 7.32560690e+01])
stdev = [ 1.17579445e+01, 1.94401006e+01, 1.86709423e-03, 2.62952690e-01]

导致

ALTER PROCEDURE [dbo].[usp_RmsExecuteValidationRule]        
    @nRuleId INT     
AS 
BEGIN        
    -- Local variables        
    DECLARE @sqlstat AS NVARCHAR(MAX)        
    DECLARE @params  AS NVARCHAR(MAX)        
    DECLARE @RULE_QUERY NVARCHAR(MAX)     
    DECLARE @FIPS  varchar(5) 

    SET @sqlstat = N'SELECT @RULE_QUERY=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = @nRuleId'        
    --SET @sqlstat =  N'SELECT ' + @RULE_QUERY + '=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = ' + @nRuleId 

    SET @params = N'@nRuleId INT,   @RULE_QUERY NVARCHAR(MAX) OUTPUT'        

    EXEC sp_executesql @sqlstat, @params, @nRuleId = @nRuleId, @RULE_QUERY= @RULE_QUERY OUTPUT 

    -- Output   
    SELECT @RULE_QUERY            
END

答案 1 :(得分:2)

您可以从数据计算初始条件:

select 
    u.ID, name, "Desc", sum(pp.amount) as paid
from 
    [dbo].[Users] u, [dbo].[UserTypes] ut, [dbo].[PlayerPayments] pp
where 
    u.UserTypeID = ut.ID 
    and u.ID = pp.UserID
group by 
    u.ID, Name, "Desc";

select 
    u.ID,name, "Desc", sum(ga.GamePrice) as owed, count(ga.ID) as gamesplayed
from 
    [dbo].[Users] u,[dbo].[UserTypes] ut, [dbo].[Games] ga, [dbo].[GamePlayers] gp
where 
    u.UserTypeID = ut.ID 
    and u.ID = gp.UserID 
    and gp.GameID = ga.ID
group by 
    u.ID, Name, "Desc";