Machine Learning (logistics regression)

In this article we will first touch on optimization algorithms.

The main idea of ​​logistic regression for classification is to establish a regression formula for the classification boundary line according to the existing data, so as to classify. The boundary of this classification is the regression function we seek.

The term regression comes from best fit, which means that to find the best fit parameters, an optimization algorithm is used. The regression function is to determine the best regression parameters, and then assign different weights to different features

Pros: Not computationally expensive, easy to understand and implement

Disadvantages: easy to underfit, classification accuracy is not high

Applicable nominal and numerical data

Algorithm Fundamentals

The mapping function used is the Sigmoid function. The Sigmoid function is better than the 0-1 function in that it is smooth locally, but it is approximately jumping as a whole, while the 0-1 function itself is jumping. At this moment The jumping process is difficult to handle, not smooth enough, and the error is large

In order to implement the logistic regression classifier, we can multiply each feature by a regression coefficient, then add the values ​​of all the results, and bring this sum into the Sigmoid function. It is classified into 0 category when it is small, so this classification method is also a probability estimation

How to Determine the Best Regression Coefficient

1. Gradient ascent method, this method is used to find the maximum value of the function, and the often-mentioned gradient descent method is used to find the minimum value of the function

2. The so-called gradient is actually the derivative in the mathematical sense, and it is also the direction in which the data changes the most. Generally, the gradient is represented by the inverted triangle symbol.

3. The formula is w= w+ a.tidu(f(w)), where a is the step size, the formula will iterate until a certain value, or reach the allowable range of error

from numpy import *


def loadDataSet():
    dataMat = []; labelMat = []
    fr = open('testSet.txt')
    for line in fr.readlines():
        lineArr = line.strip().split()
        dataMat.append([1.0, float(lineArr[0]), float(lineArr[1])])
        labelMat.append(int(lineArr[2]))
    return dataMat,labelMat



def sigmoid(inX):
    return 1.0/(1+exp(-inX))




def gradAscent(dataMatIn, classLabels):
    dataMatrix = mat(dataMatIn)             #convert to NumPy matrix
    labelMat = mat(classLabels).transpose() #convert to NumPy matrix
    m,n = shape(dataMatrix)
    alpha = 0.001
    maxCycles = 500
    weights = ones((n,1))
    for k in range(maxCycles):              #heavy on matrix operations
        h = sigmoid(dataMatrix*weights)     #matrix mult
        error = (labelMat - h)              #vector subtraction
        weights = weights + alpha * dataMatrix.transpose()* error #matrix mult
    return weights




    
def plotBestFit (weights):
     import matplotlib.pyplot as plt
    dataMat,labelMat=loadDataSet()
    dataArr = array(dataMat)
    n = shape(dataArr)[0] 
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    for i in range(n):
        if int(labelMat[i])== 1:
            xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2])
        else:
            xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='green')
    x = arange(-3.0, 3.0, 0.1)
    y = (-weights[0]-weights[1]*x)/weights[2]
    ax.plot(x, y)
    plt.xlabel( ' X1 ' ); plt.ylabel( ' X2 ' );
    plt.show()

def stocGradAscent0(dataMatrix, classLabels):
    m,n = shape(dataMatrix)
    alpha = 0.01
    weights = ones(n)   #initialize to all ones
    for i in range(m):
        h = sigmoid(sum(dataMatrix[i]*weights))
        error = classLabels[i] - h
        weights = weights + alpha * error * dataMatrix[i]
    return weights

def stocGradAscent1(dataMatrix, classLabels, numIter=150):
    m,n = shape(dataMatrix)
    weights = ones(n)   #initialize to all ones
    for j in range(numIter):
        dataIndex = range(m)
        for i in range(m):
            alpha = 4/(1.0+j+i)+0.0001    #apha decreases with iteration, does not 
            randIndex = int(random.uniform(0,len(dataIndex)))#go to 0 because of the constant
            h = sigmoid(sum(dataMatrix[randIndex]*weights))
            error = classLabels[randIndex] - h
            weights = weights + alpha * error * dataMatrix[randIndex]
            del(dataIndex[randIndex])
    return weights

def classifyVector(inX, weights):
    prob = sigmoid(sum(inX*weights))
    if prob > 0.5: return 1.0
    else: return 0.0

def colicTest():
    frTrain = open('horseColicTraining.txt'); frTest = open('horseColicTest.txt')
    trainingSet = []; trainingLabels = []
    for line in frTrain.readlines():
        currLine = line.strip().split('\t')
        lineArr =[]
        for i in range(21):
            lineArr.append(float(currLine[i]))
        trainingSet.append(lineArr)
        trainingLabels.append(float(currLine[21]))
    trainWeights = stocGradAscent1(array(trainingSet), trainingLabels, 1000)
    errorCount = 0; numTestVec = 0.0
    for line in frTest.readlines():
        numTestVec += 1.0
        currLine = line.strip().split('\t')
        lineArr =[]
        for i in range(21):
            lineArr.append(float(currLine[i]))
        if int(classifyVector(array(lineArr), trainWeights))!= int(currLine[21]):
            errorCount += 1
    errorRate = (float(errorCount)/numTestVec)
    print "the error rate of this test is: %f" % errorRate
    return errorRate

def multiTest():
    numTests = 10; errorSum=0.0
    for k in range(numTests):
        errorSum += colicTest()
    print "after %d iterations the average error rate is: %f" % (numTests, errorSum/float(numTests))
        

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325226840&siteId=291194637