''' Created on Oct 6, 2010 @author: Peter ''' from numpy import * import matplotlib import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from Ch05 import logRegres dataMat,labelMat=logRegres.loadDataSet() dataArr = array(dataMat) weights = logRegres.stocGradAscent0(dataArr,labelMat) n = shape(dataArr)[0] #number of points to create xcord1 = []; ycord1 = [] xcord2 = []; ycord2 = [] markers =[] colors =[] 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(xcord,ycord, c=colors, s=markers) type1 = ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
from Ch05 import logRegres from numpy import * if __name__ == '__main__': a, b = logRegres.loadDataSet() w = logRegres.stocGradAscent1(array(a), b, 20) print w logRegres.plotBestFit(w)
''' Created on Oct 6, 2010 @author: Peter ''' from numpy import * import matplotlib import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from Ch05 import logRegres dataMat, labelMat = logRegres.loadDataSet() dataArr = array(dataMat) weights = logRegres.stocGradAscent0(dataArr, labelMat) n = shape(dataArr)[0] #number of points to create xcord1 = [] ycord1 = [] xcord2 = [] ycord2 = [] markers = [] colors = [] 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])
for i in range(m): alpha = 4 / (1.0 + j + i) + 0.01 # 随机选取一条数据记录 randIndex = int(random.uniform(0, len(dataIndex))) h = logRegres.sigmoid(sum(dataMatrix[randIndex] * weights)) error = classLabels[randIndex] - h # print error weights = weights + alpha * error * dataMatrix[randIndex] # 记录weights,注意这个地方的 j * m + i ,j为迭代的次数,m为数据总量100 weightsHistory[j * m + i, :] = weights del (dataIndex[randIndex]) print weights return weightsHistory dataMat, labelMat = logRegres.loadDataSet('../testSet.txt') dataArr = array(dataMat) myHist = stocGradAscent1(dataArr, labelMat) n = shape(dataArr)[0] # number of points to create xcord1 = [] ycord1 = [] xcord2 = [] ycord2 = [] markers = [] colors = [] fig = plt.figure() ax = fig.add_subplot(311) type1 = ax.plot(myHist[:, 0])