簡體   English   中英

即使在復制和粘貼時,函數定義和函數調用也會在python 3中產生語法錯誤

[英]Function definition and function call produce syntax error in python 3, even when copied and pasted

我正在嘗試使用梯度下降解決在python中拼接到三階多項式的冪律的最小二乘擬合問題。 我已經在Matlab中針對參數計算了梯度。 我手動計算的邊界條件。 我在卡方最小化算法中遇到語法錯誤,該錯誤必須考慮邊界條件。 我在機器學習課程中這樣做,我在其中完成了一個自我指導和自我建議的長期項目,但是由於這種語法錯誤,我感到困惑,我不確定該如何克服。 我不會因此而獲得學分。 這簡直就是我的簡歷。

def polypowerderiv(x,a1,b1,c1,a2,b2,c2,d2,boundaryx,ydat):


    #need to minimize square of ydat-polypower
    #from Mathematica, to be careful
    gradd2=2*(d2+c2*x+b2*x**2+a2*x**3-ydat)
    gradc2=gradd2*x
    gradb2=gradc2*x
    grada2=gradb2*x

    #again from Mathematica, to be careful
    gradc1=2(c+a1*x**b1-ydat)
    grada1=gradc1*x**b1
    gradb1=grada1*a1*log(x)

    return [np.sum(grada1),np.sum(gradb1),\
        np.sum(gradc1),np.sum(grada2),np.sum(gradb2),\
        np.sum(gradc2),np.sum(gradd2)]


def manualleastabsolutedifference(xdat, ydat, params,seed, maxiter, learningrate):
    chisq=0 #chisq is the L2 error of the fit relative to the ydata
    dof=len(xdat)-len(params)
    xparams=seed
    for step in np.arange(maxiter):
        a1,b1,c1,a2,b2,c2,d2=params
        chisq=polypowerlaw(xdat,params)
        for i in np.arange(len(xdat)):
            grad=np.zeros(len(seed))
            for i in np.arange(seed):


        polypowerlawboundarysolver=\
        polypowerboundaryconstraint(xdat,a1,b1,c1,a2,b2,c2)

        boundaryx=minimize(polypowerlawboundarysolver,x0=1000) 
        #hard coded to be half of len(xdat)
        chisq+=abs(ydat-\ 
            polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx)
        grad=\
            polypowerderiv(xdat,a1,b1,c1,\
            a2,b2,c2,d2,boundaryx,ydat)

    params+=learningrate*grad
return params

我得到的錯誤是:

文件“”,第14行
grad = polypowerderiv(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx,ydat)
^ SyntaxError:語法無效

另外,我在格式化方面遇到了一些小麻煩。 請幫忙。 經過多年的上下投票后,這是我有史以來第幾篇有關Stack Overflow的文章。 感謝您的廣泛幫助,社區。

根據Alan-Fey,您忘記了右括號:

chisq+=abs(ydat-\ 
            polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx)

應該

chisq+=abs(ydat-\ 
            polypower(xdat,a1,b1,c1,a2,b2,c2,d2,boundaryx))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM