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) ''' #运算结果很精确 Congratulations, training complete! Took 0.666000s! optimalWeights= [[-11.70491496] [ -3.65040344] [ 2.31317572]] step 3: testing... accuracy= 0.964285714286 step 4: show the result... The classify accuracy is: 96.429% '''
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)
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% 改进的随机梯度下降法迭代200次 达到精度94% 改进的随机梯度下降法迭代20次 达到精度95% '''