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

        

  • 相关阅读:
    搭建自己的SIPserver:开源sipserveropensips的搭建及终端TwInkle的使用
    字符串类习题、面试题具体解释(第二篇)
    【Android笔记】MediaPlayer基本使用方式
    poj 1258 Agri-Net
    ActionScript3游戏中的图像编程(连载二十四)
    SQL中declare申明变量
    CreateFileMapping使用方法
    【剑指offer】调整数组顺序
    2015美团网笔试面试总结(嵌入式/硬件类)(美团网校园招聘)
    linux串口驱动分析
  • 原文地址:https://www.cnblogs.com/hao11/p/11668854.html
Copyright © 2011-2022 走看看