3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
機器學習還是要落在代碼中,我們來看看邏輯回歸部分的代碼,怎麼落實。這裡說明一下,文中代碼均為Python。1 邏輯回歸1.1 預測函數這裡n是特徵的數量。使用代碼實現如下:# 預測函數 def hypothesis(X, theta): z = X.dot(theta) g = 1/(1+np.e**-z) return g1.2 代價函數和梯度邏輯回歸的代價函數:向量化表達:求導得到的梯度:# 代價函數和梯度 def costFunction(theta, X, y): h = hypothesis(X,theta) J = 1.0/m*(-y.T.dot(log(h)) - (1-y).T.dot(log(1-h))) grad = 1.0/m*X.T.dot(h-y) return J, grad為了能夠使用現有的,別人已經實現的方法,對於代價函數和梯度稍微做一些調整:# 代價函數 def costFunction(theta, X, y): # 樣本數量 m = y.size # 參數的拷貝 tmp_theta = theta.reshape(X.shape[1],1).copy # 預測函數 z = np.array(dot(X,tmp_theta)) h = 1.0/(1.0+np.e**-z) # 代價函數 J = 1.0/m*(-y.T.dot(log(h))-(1-y).T.dot(log(1-h))) if np.isnan(J): return np.inf return J # 梯度 def gradient(theta, X, y): # 樣本數量 m = y.size # 參數的拷貝 tmp_theta = theta.reshape(X.shape[1],1).copy # 預測函數 z = np.array(dot(X,tmp_theta)) h = 1.0/(1.0+np.e**-z) # 梯度計算 grad = 1.0/m*X.T.dot(h-y) grad = grad.flatten return grad2 正則化增加了正則化項的代價函數:向量化表達:求導得到的梯度:# 代價函數 def costFunctionReg(theta, X, y, mylambda = 1): # 樣本數量 m = y.size # 參數的拷貝 tmp_theta = theta.reshape(X.shape[1],1).copy # 預測函數 z = np.array(dot(X,tmp_theta)) h = 1.0/(1.0+np.e**-z) # 第一個參數不計算,和 L·θ 效果類似 tmp_theta[0,0] = 0 # 代價函數計算 J = 1.0/m*(-y.T.dot(log(h))-(1-y).T.dot(log(1-h)))+1.0*mylambda/2/m*sum(tmp_theta**2) if np.isnan(J): return np.inf return J # 梯度 def gradientReg(theta, X, y,mylambda = 1): # 樣本數量 m = y.size # 參數的拷貝 tmp_theta = theta.reshape(X.shape[1],1).copy # 預測函數 z = np.array(dot(X,tmp_theta)) h = 1.0/(1.0+np.e**-z) # 第一個參數不計算,和 L·θ 效果類似 tmp_theta[0,0] = 0 # 梯度計算 grad = 1.0/m*X.T.dot(h-y) + 1.0*mylambda/m*tmp_theta # 結果變成一行 grad = grad.flatten return grad3 調用現有方法CG:opts = {'disp': False, 'gtol': 1e-05, 'eps': 1.4901161193847656e-08, 'return_all': False, 'maxiter': None, 'norm': inf} result = optimize.minimize(costFunctionReg, initial_theta, args=(X, y, mylambda), method='CG', jac=gradientReg, tol=None, callback=None, options=opts) theta = result.x print("\n代價函數計算結果:\n%s"%result.fun) print("\n參數theta計算結果:\n%s"%result.x)Newton-CG:opts = {'disp': False, 'xtol': 1e-05, 'eps': 1.4901161193847656e-08, 'return_all': False, 'maxiter': None} result = optimize.minimize(costFunctionReg, initial_theta, args=(X, y, mylambda), method='Newton-CG', jac=gradientReg, hess=None, hessp=None, tol=None, callback=None, options=opts) theta = result.x print("\n代價函數計算結果:\n%s"%result.fun) print("\n參數theta計算結果:\n%s"%result.x)BFGS:opts = {'disp': False, 'gtol': 1e-05, 'eps': 1.4901161193847656e-08, 'return_all': False, 'maxiter': None, 'norm': inf} result = optimize.minimize(costFunctionReg, initial_theta, args=(X, y, mylambda), method='BFGS', jac=None, tol=None, callback=None, options=opts) theta = result.x print("\n代價函數計算結果:\n%s"%result.fun) print("\n參數theta計算結果:\n%s"%result.x)L-BFGS-B:opts = {'disp': None, 'iprint': -1, 'gtol': 1e-05, 'eps': 1e-08, 'maxiter': 15000, 'ftol': 2.220446049250313e-09, 'maxcor': 10, 'maxfun': 15000} result = optimize.minimize(costFunctionReg, initial_theta, args=(X, y, mylambda), method='L-BFGS-B', jac=gradientReg, bounds=None, tol=None, callback=None, options=opts) theta = result.x print("\n代價函數計算結果:\n%s"%result.fun) print("\n參數theta計算結果:\n%s"%result.x)其中costFunctionReg為代價函數,gradientReg為梯度,(X, y, mylambda)為代價函數所使用的參數,也就是我們的訓練數據集。initial_theta為初始化參數,也是我們最終要獲得的結果,實現的代價函數中,該參數應該放在第一位,如costFunctionReg(theta, X, y, mylambda = 1),否則會出現報錯。更多方法參考:https://docs.scipy.org/doc/scipy/reference/optimize.html4 應用假如我們有基於晶元的兩組測試數據,以及測試的結果。y = 1,表示晶元質量過關;y = 0 表示晶元不合格。現在,我們要檢查一批晶元,看起是否能通過質量檢驗(QA)。我們希望從過去的數據中得到指引,快速找到不符合要求的晶元。這時候,我們可以建立邏輯回歸模型。最終,我們得到了這樣的結果:λ = 1可以看到,紅線大致把符合要求的晶元都框中了。這裡使用的正則化參數為λ = 1。如果正則化參數為λ過小,或者過大會怎麼樣呢?參數過小,結果是過度擬合:λ = 0參數過大,結果是未擬合:λ = 100文章提前發布在公眾號:止一之路

本文由yidianzixun提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦