[Machine Learning]--Logistic Regression

本文介绍了使用逻辑回归进行分类的方法,并对比了批量梯度上升、随机梯度上升及改进版随机梯度上升三种优化算法。详细解释了每种算法的工作原理,并提供了Python实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Classification with Logistic Regression and the Sigmoid Function: a Tractable Step Function

We don’t use Heaviside step function.Just as the following equation:

H(x)=0,12,1,x < 0x=0x > 0

σ(z)=11+ez

Logistic regression gradient ascent optimization function

Fist, i will show the code

def gradAscent(dataMatIn, classLabels):
    dataMatrix = mat(dataMatIn)
    labelMat = mat(classLabels).transpose()
    m, n = shape(dataMatrix)
    alpha = 0.001
    maxCycles = 500
    weights = ones((n, 1))
    for k in range(maxCycles):
        h = sigmoid(dataMatrix*weights)
        error = (labelMat - h)
        weights = weights + alpha * dataMatrix.transpose()*error
    return weights

However, it can be quite time-consuming. Quite expensive in terms of computing resource!
dataMatrix [m*n]
weights: [n*1]
h: [m*1]
error: [m*1]
so, dataMatrix*weights means m*n*n times calculation!
h
So, we can use the Stochastic Gradient Ascent to do the optimization.
Let’s see the code!

def stocGradAscent0(dataMatrix, classLabels):
    m, n = shape(dataMatrix)
    alpha = 0.01
    weights = ones(n)                                # weights  [1, n]
    for i in range(m):
        h = sigmoid(sum(dataMatrix[i]*weights))      # not matrix calculation, but element * element,
                                                     # dataMatrix[i]: [1, n]    weights:[1, n]
        error = classLabels[i] - h
        weights = weights + alpha * error * dataMatrix[i]
    return weights

Very similar to gradient ascent except that the variable h and the error ate now single values rather than vectors!

However the Convergence Rate is not very good, so modify this optimization algorithm.

def stocGradAscent1(dataMatrix, classLabels, numIter=150):
    m,n = shape(dataMatrix)
    weights = ones(n)
    for j in range(numIter):
        dataIndex = range(m)
        for i in range(m):
        # alpha changes on each iteration, which will improve the oscillations that occur.
            alpha = 4/(1.0 + i + j) + 0.1
            # randomly select some instance to use in updating the weights. Be sure to delete what you have used.
            randIndex = int(random.uniform(0, len(dataIndex)))
            h = sigmoid(sum(dataMatrix[randIndex]*weights))
            error = classLabels[randIndex] - h
            weights = weights + alpha * error * dataMatrix[randIndex]
    return weights

Classifying with logistic regression

The weights we get is used to classify.
See the classify function!

def classifyVector(inX, weights):
    prob = sigmoid(sum(inX*weights))
    if prob > 0.5: return 1.0
    else: return 0.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值