1. 概率:

     1.1 定义   概率(P)robability: 对一件事情发生的可能性的衡量

     1.2 范围   0 <= P <= 1

     1.3 计算方法:

          1.3.1 根据个人置信

          1.3.2 根据历史数据

          1.3.3 根据模拟数据

     1.4 条件概率:

                         8694e4193ba45b55403595096b7d23c5

2. Logistic Regression (逻辑回归)

     2.1 例子

Image

Image [1]

h(x) > 0.2

   2.2 基本模型
         测试数据为X(x0,x1,x2···xn)
         要学习的参数为: Θ(θ0,θ1,θ2,···θn)

001QAImHgy6I1ohlalO18&690

向量表示:

001QAImHgy6I1oi9u8Kae&690

处理二值数据,引入Sigmoid函数时曲线平滑化

001QAImHgy6I1ojfTjYaa&690

001QAImHgy6I1ok9Brb61&690

 

  预测函数:

001QAImHgy6I1olbW3yfc&690

用概率表示:

          正例(y=1):

001QAImHgy6I1osqQ7lc7&690

      反例(y=0):

001QAImHgy6I1omK5aoc8&690

 2.3  Cost函数

              线性回归:

001QAImHgy6I1otAWE890&690

001QAImHgy6I1oudixl13&690

001QAImHgy6I1owps7Ud2&690

找到合适的 θ0,θ1使上式最小

  Logistic regression:

 Cost函数:001QAImHgy6I1oEKVWg50&690

 

目标:找到合适的 θ0,θ1使上式最小

2.4 解法:梯度下降(gradient decent)

imgresimgres [1]

001QAImHgy6I1oGTmnA36&690

              更新法则:

001QAImHgy6I1oJm3Qz27&690

  学习率

                      同时对所有的θ进行更新

                      重复更新直到收敛

非线性回归应用 使用python实现

import numpy as np
import random

# m denotes the number of examples here, not the number of features
def gradientDescent(x, y, theta, alpha, m, numIterations):
    xTrans = x.transpose()
    for i in range(0, numIterations):
        hypothesis = np.dot(x, theta)
        loss = hypothesis - y
        # avg cost per example (the 2 in 2*m doesn't really matter here.
        # But to be consistent with the gradient, I include it)
        cost = np.sum(loss ** 2) / (2 * m)
        print("Iteration %d | Cost: %f" % (i, cost))
        # avg gradient per example
        gradient = np.dot(xTrans, loss) / m
        # update
        theta = theta - alpha * gradient
    return theta

#模拟获取数据
def genData(numPoints, bias, variance):
    x = np.zeros(shape=(numPoints, 2))
    y = np.zeros(shape=numPoints)
    # basically a straight line
    for i in range(0, numPoints):
        # bias feature
        x[i][0] = 1
        x[i][1] = i
        # our target variable
        y[i] = (i + bias) + random.uniform(0, 1) * variance
    return x, y

# gen 100 points with a bias of 25 and 10 variance as a bit of noise
x, y = genData(100, 25, 10)
m, n = np.shape(x)
numIterations= 100000
alpha = 0.0005
theta = np.ones(n)
theta = gradientDescent(x, y, theta, alpha, m, numIterations)
print(theta)

该代码运行的Iteration 不断变大 cost 不断变小 求出theta
20170619153238

logistic regression示例代码下载