zoukankan      html  css  js  c++  java
  • Logistic Regression

    逻辑回归模型:

      want: 0 <= hθ(x)<=1

      hθ(x) = g(θTx)     //θ 注意有个θ一般置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() *error
    12     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

        

  • 相关阅读:
    项目总结—校园办公管理系统(SSM框架搭建)
    sssp-webservce_restful
    angular 中怎么获取路径上的参数 参考:https://docs.angularjs.org/api/ng/service/$location
    spring mvc 解决json 不能转换的问题
    在 html中怎么获取中的参数
    弹框,图标
    sssp maven pom
    spring中 的MD5 加密
    angularjs 整合 bootstrap
    深入理解Java包装类与自动拆装箱
  • 原文地址:https://www.cnblogs.com/hao11/p/11668854.html
Copyright © 2011-2022 走看看