邏輯回歸模型:
want: 0 <= hθ(x)<=1
hθ(x) = g(θTx) //θ 注意有個θ0 一般置1
g(z) = 1 / (1+e-z) //sigmoid 函式
由sigmoid函式特性(2分類問題):
def sigmoid(inX): return 1.0 /(1 + np.exp(-inX))
當θTx>= 0 時,hθ(x)>= 0.5 , 預測 y =1 ;
當θTx< 0 時,hθ(x)< 0.5 , 預測 y = 0 ;
ps: 可以改變閾值, 此處為0.5,取決于需要的置信度高低
決策邊界:
θT.X == 0 對應上面閾值為 0.5 不一定是直線,看特征向量怎么選 平方 x1*x2
csot function:
J(θ) = 1/m Σ(-y log( hθ(X) ) - (1 - y)log( 1 - hθ(X) ))+ (λ /2m)∑j=1n θ2j //m為樣本數 可進行向量化 紅色部分為正則項
目標:
min(J(θ))
使用梯度下降
Repeat:
θj := θj - α(Σ(hθ(xi) - yi) xij + (λ /m)θj)
注意: θ要“同時“賦值, 因為求下降的部分用到了 hθ(xi) 而hθ(xi)里面用到θ
即是在山上一個位置同時對各個方向求偏導 如果不同時 那么 位置就變了
1 def gradDescent(dataMatIn, classLabels): 2 dataMatrix = np.mat(dataMatIn) 3 labelMat = np.mat(classLabels).transpose() 4 m , n = np.shape(dataMatrix) 5 alpha = 0.001 6 maxCycles = 500 7 weights = np.ones((n,1)) 8 for k in range(maxCycles): 9 h = sigmoid(dataMatrix * weights)10 error = (h - labelMat)11 weights = weights - alpha * dataMatrix.transpose() *error12 return weights
優化:
overfit : reduce feature, bigger dataset,increase penalize parameter λ
underfit: increase feature, reduce penalize parameter λ
How to choose learning rate α ?
By iteration and draw the picture of cost function and times of iteration
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/72008.html
標籤:其他
上一篇:python 不執行if陳述句
下一篇:大佬來
