Chapter VI logistic regression model with maximum entropy

The book and an understanding of some important definitions

  • First to elicit logist distribution model described by the logic Steve
  • And to derive the parameters of the model by maximum likelihood estimation method
  • Model parameters likelihood function is obtained by deriving an equation by recursively
  • It can be seen is determined by the formula logist way preceding perceptron upgraded version, too simple perceptron. And its gradient descent, it also removed the sign, otherwise can not be differentiated.
  • After writing to the formula by the equation, as follows

 

Import numpy AS NP
 from read_data Import get_2_kind_data 


DEF logistic_Regression (tra_x, tra_y, of lambdas = 0.01, = 200 is ITER ):
     '' ' 
    logistic regression training process 
    : param trainDataList: training set 
    : param trainLabelList: Label Set 
    : param iter: Iteration times 
    : return: Acquisition w 
    '' ' 
    # accordance with the rules of formula 6.5 book "6.1.2 two logistic regression model", and b together with w, 
    # case x need to add a dimension value It is. 1 
    tra_x = np.insert (tra_x, len (tra_x [0]),. 1,. 1 ) 
    W = np.random.rand (len (tra_x [0]))
     for I in Range (ITER):   # each iteration washed once traversed all samples for stochastic gradient descent
        for J in the Range (len (tra_x)):
             # stochastic gradient ascent part 
            # gives the likelihood function in the "6.1.3 Parameter Estimation" chapter, we need to maximize the likelihood function 
            # but due to the likelihood function there summation term, and can not directly obtain the optimal w w derivation, so that the likelihood function for the summing 
            # individually request each of the guide is partially w, to obtain the sample for the gradient, and the gradient rises ( because 
            # claim maximum likelihood function, so it is the gradient ascent, if the minimum value is gradient descent gradient is increased 
            # plus, minus sign is decreased) 
            # summation of each request separately w guide results: XI * yi - (exp (w * XI) * XI) / (1 + exp (w * XI)) 
            # If there are guided ask questions for which you can view my blog www.pkudodo.com 
            # computing w * xi, because the formula to calculate the value twice, calculated in advance in order to save time here 
            # in fact, can also be directly calculated exp (wx), in order to facilitate the reader can see that it is so written, including yi and xi are ahead lists 
            WX = np.dot (W, tra_x [J] .T) 
            Yi = tra_y [J]
            XI = tra_x [J] 
            W + = * of lambdas (XI * Yi - (np.exp (WX) XI *) / (. 1 + np.exp (WX)))
     return W 

DEF Predict (W, X):
     '' ' 
    prediction tags 
    : param w: training process learned W 
    : param X: the sample to be predicted 
    : return: predictors 
    ' '' 
    # dOT is the dot product of two vectors operation, calculate X * W 
    WX = np.dot (W, X)
     # calculates the probability of tag 
    # the formula refer to "6.1.2 two logistic regression model" in the formula 6.5 
    Pl = np.exp (WX) / (1 + np.exp (WX) )
     # If the probability is greater than 0.5, returns an 
    IF Pl> = 0.5 :
         return 1
     #Otherwise return 0 
    return predict (W, testDataList [I]):0 

DEF Test (testDataList, testLabelList, W):
     '' ' 
    Verify 
    : param testDataList: Test Suite 
    : param testLabelList: Test Set Tags 
    : param w: training process learned W 
    : return: the correct rate 
    ' '' 
    # and training process consistent for all samples first adding a dimension value of 1, please see the reason training function 
    testDataList = np.insert (testDataList, len (testDataList [0]), 1,1 )
     # error count value 
    ERRORCNT = 0
     # for the test each focus test sample to verify 
    for I in Range (len (testDataList)):
         # if inconsistency flag and the prediction error value plus. 1 
        IF ! testLabelList [I] = 
            ERRORCNT +. 1 =
     # returns accuracy
    return 1 - errorCnt / len(testDataList)


if __name__ == '__main__':
    tra_x, test_x, tra_y, test_y = get_2_kind_data('data/Mnist/mnist_train.csv')
    tra_x, test_x = tra_x / 255, test_x / 255
    w=logistic_Regression(tra_x, tra_y)
    acc=test(test_x,test_y,w)
    print(acc)

 

Guess you like

Origin www.cnblogs.com/fghfghfgh666/p/11000714.html