Exemple #1
0
#     generateFile()
    
    ## step 1: load data
    print "step 1: load data..."
    train_x, train_y = loadData()
    
    test_x = train_x; test_y = train_y
    
    ## step 2: training...
    print "step 2: training..."
    opts = {'alpha': 0.01, 'maxIter': 200, 'optimizeType': 'smoothStocGradDescent'}
    optimalWeights = trainLogRegres(train_x, train_y, opts)
    print 'optimalWeights=',optimalWeights
    ## step 3: testing
    print "step 3: testing..."
    accuracy = testLogRegres(optimalWeights, test_x, test_y)
    
#     numSamples, numFeatures = shape(test_x)
#     matchCount = 0
#     for i in xrange(numSamples):
#         predict = sigmoid(test_x[i, :] * optimalWeights)[0, 0] > 0.5
#         if predict == bool(test_y[i, 0]):
#             matchCount += 1
#     accuracy = float(matchCount) / numSamples
    
    print 'accuracy=',accuracy
    
    ## step 4: show the result
    print "step 4: show the result..."    
    print 'The classify accuracy is: %.3f%%' % (accuracy * 100)
    showLogRegres(optimalWeights, train_x, train_y) 
def loadData():
    train_x = []
    train_y = []
    fileIn = open('./testSet.txt')
    for line in fileIn.readlines():
        lineArr = line.strip().split()
        train_x.append([1.0, float(lineArr[0]), float(lineArr[1])])
        train_y.append(float(lineArr[2]))
    return np.mat(train_x), np.mat(train_y).transpose()


## step 1: load data
print "step 1: load data..."
train_x, train_y = loadData()
test_x = train_x
test_y = train_y
## step 2: training...
print "step 2: training..."
opts = {'alpha': 0.01, 'maxIter': 500, 'optimizeType': 'gradDescent'}
optimalWeights = logRegression.trainLogRegres(train_x, train_y, opts)

## step 3: testing
print "step 3: testing..."
accuracy = logRegression.testLogRegres(optimalWeights, test_x, test_y)

## step 4: show the result
print "step 4: show the result..."
print 'The classify accuracy is: %.3f%%' % (accuracy * 100)
logRegression.showLogRegres(optimalWeights, train_x, train_y)
    return mat(train_x), mat(train_y).transpose()

if __name__ == '__main__':
    ## step 1: load data
    print "step 1: load data..."
    train_x, train_y = loadData()
    test_x = train_x; test_y = train_y
    
    ## step 2: training...
    print "step 2: training..."
    opts = {'alpha': 0.01, 'maxIter': 20, 'optimizeType': 'smoothStocGradDescent'}
    optimalWeights = trainLogRegres(train_x, train_y, opts)
    print 'optimalWeights=',optimalWeights
    ## step 3: testing
    print "step 3: testing..."
    accuracy = testLogRegres(optimalWeights, test_x, test_y)
    print 'accuracy=',accuracy
    
    ## step 4: show the result
    print "step 4: show the result..."    
    print 'The classify accuracy is: %.3f%%' % (accuracy * 100)
    showLogRegres(optimalWeights, train_x, train_y) 
    
    
'''
梯度下降法,迭代500次
达到精度95%

随机梯度下降法 迭代200次
达到精度97%