西瓜书习题3.3
1.梯度上升法解答
# -*- coding: utf-8 -*- '''西瓜书第三章课后题3.3''' #梯度上升法 from numpy import * import operator from os import listdir def loaddata(): dataMat=[];labelMat=[] #fr=open(r'E:\机器学习\西瓜书\data33.txt') fr=open(r'E:\机器学习\机器学习实战\machinelearninginaction\Ch05\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(dataMat,classLabels): dataMatrix=mat(dataMat) labelMat=mat(classLabels).transpose() m,n=shape(dataMatrix) alpha=0.001 maxCycles=500 weights=ones((n,1)) for k in range(maxCycles): h=sigmoid(dataMatrix*weights) error=(labelMat-h) weights=weights+alpha*dataMatrix.transpose()*error return weights #画出决策边界 def plotBestFit(weights): import matplotlib.pyplot as plt dataMat,labelMat=loaddata() 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(0.0,1.0,0.1) y=(-weights[0]-weights[1]*x)/weights[2] ax.plot(x,y) plt.xlabel('x1');plt.ylabel('x2') plt.show() dataArr,labelMat=loaddata() print(gradAscent(dataArr,labelMat)) weights=gradAscent(dataArr,labelMat) print(plotBestFit(weights.getA()))