最近閑著沒事,想把coursera上斯坦福ML課程里面的練習(xí),用Python來實(shí)現(xiàn)一下,一是加深ML的基礎(chǔ),二是熟悉一下numpy,matplotlib,scipy這些庫。
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),南木林網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:南木林等地區(qū)。南木林做網(wǎng)站價格咨詢:18982081108在EX2中,優(yōu)化theta使用了matlab里面的fminunc函數(shù),不知道Python里面如何實(shí)現(xiàn)。搜索之后,發(fā)現(xiàn)stackflow上有人提到用scipy庫里面的minimize函數(shù)來替代。我嘗試直接調(diào)用我的costfunction和grad,程序報(bào)錯,提示(3,)和(100,1)dim維度不等,gradient vector不對之類的,試了N多次后,終于發(fā)現(xiàn)問題何在。。
首先來看看使用np.info(minimize)查看函數(shù)的介紹,傳入的參數(shù)有:
fun : callable The objective function to be minimized. ``fun(x, *args) -> float`` where x is an 1-D array with shape (n,) and `args` is a tuple of the fixed parameters needed to completely specify the function. x0 : ndarray, shape (n,) Initial guess. Array of real elements of size (n,), where 'n' is the number of independent variables. args : tuple, optional Extra arguments passed to the objective function and its derivatives (`fun`, `jac` and `hess` functions). method : str or callable, optional Type of solver. Should be one of - 'Nelder-Mead' :ref:`(see here)` - 'Powell' :ref:`(see here) ` - 'CG' :ref:`(see here) ` - 'BFGS' :ref:`(see here) ` - 'Newton-CG' :ref:`(see here) ` - 'L-BFGS-B' :ref:`(see here) ` - 'TNC' :ref:`(see here) ` - 'COBYLA' :ref:`(see here) ` - 'SLSQP' :ref:`(see here) ` - 'trust-constr':ref:`(see here) ` - 'dogleg' :ref:`(see here) ` - 'trust-ncg' :ref:`(see here) ` - 'trust-exact' :ref:`(see here) ` - 'trust-krylov' :ref:`(see here) ` - custom - a callable object (added in version 0.14.0), see below for description. If not given, chosen to be one of ``BFGS``, ``L-BFGS-B``, ``SLSQP``, depending if the problem has constraints or bounds. jac : {callable, '2-point', '3-point', 'cs', bool}, optional Method for computing the gradient vector. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov, trust-exact and trust-constr. If it is a callable, it should be a function that returns the gradient vector: ``jac(x, *args) -> array_like, shape (n,)`` where x is an array with shape (n,) and `args` is a tuple with the fixed parameters. Alternatively, the keywords {'2-point', '3-point', 'cs'} select a finite difference scheme for numerical estimation of the gradient. Options '3-point' and 'cs' are available only to 'trust-constr'. If `jac` is a Boolean and is True, `fun` is assumed to return the gradient along with the objective function. If False, the gradient will be estimated using '2-point' finite difference estimation.
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。