#学习自机器学习实战
from numpy import *
def loadDataSet(filename):
dataMat=[];labelMat=[]#initilize
fr=open(filename)#open the file
for line in fr.readlines():#process the readline
lineArr=line.strip().split(' ')#the disperation of the data
#print(lineArr[0])
dataMat.append([float(lineArr[0]),float(lineArr[1])])#the value of the data
labelMat.append(float(lineArr[2]))
return dataMat,labelMat#return value
def selectJrand(i,m):#fine the j which is different from i
j=i
while(j==i):
j=int(random.uniform(0,m))#random produce
return j
def clipAlpha(aj,H,L):
if aj>H:#make the newaj in the edge
aj=H
if L>aj:
aj=L
return aj
def smoSimple(dataMatIn,classLabels,C,toler,maxIter):
dataMatrix=mat(dataMatIn);labelMat=mat(classLabels).transpose()#copy
b=0;m,n=shape(dataMatrix)#m the number of data n the column of data
alphas=mat(zeros((m,1)))#initilize the alphas alphas 's volumn is equal to dataMat .m
iter=0#initilize the iteration record
while(iter<maxIter):#weather is achieve the maxiteration
alphaPairsChanged=0#havn't be changed
for i in range(m):#circle
fxi=float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T))+b#columns dot +bias is the function value
ei=fxi-float(labelMat[i])
if((labelMat[i]*ei<-toler)and(alphas[i]<C))or((labelMat[i]*ei>toler) and(alphas[i]>0)):#if the value have much differencefrom the real it needs ptimize
j=selectJrand(i,m)
fxj=float(multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T))+b
ej=fxj-float(labelMat[j])#the difference of the prcdict and the real
alphaIold=alphas[i].copy()#save the old value
alphaJold=alphas[j].copy()
if(labelMat[i]!=labelMat[j]):#if not a same class
L=max(0,alphas[j]-alphas[i])#the edge
H=min(C,C+alphas[j]-alphas[i])#a-b 's slope is 1
else:
L=max(0,alphas[j]+alphas[i]-C)
H=min(C,alphas[j]+alphas[i])
if L==H: print("L==H");continue#don't have num
eta=2.0*dataMatrix[i,:]*dataMatrix[j,:].T-dataMatrix[i,:]*dataMatrix[i,:].T-dataMatrix[j,:]*dataMatrix[j,:].T
if eta>=0: print("eta>=0");continue#if we decide the ai aj and the other a is constance and the let it in the w find the least
alphas[j]-=labelMat[j]*(ei-ej)/eta#new aj
alphas[j]=clipAlpha(alphas[j],H,L)
if(abs(alphas[j]-alphaJold)<toler): print("j not moving enough");continue;
alphas[i]+=labelMat[j]*labelMat[i]*(alphaJold-alphas[j])#new ai and then new b
b1=b-ei-labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T-labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T
b2=b-ej-labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T-labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T
if (0<alphas[i]) and (C>alphas[i]) :b=b1
elif(0<alphas[j]) and (C>alphas[j]) :b=b2
else :b=(b1+b2)/2.0
alphaPairsChanged+=1
print("iter:%d,i %d pairs changed %d"%(iter,i,alphaPairsChanged))
if alphaPairsChanged ==0: iter+=1
else:iter=0
print("iteration number:%d"%iter)
return b,alphas
if __name__=='__main__':
x,y=loadDataSet("testSet.txt")
# print("data" ,x,"\nlabel" ,y)
b,alpha=smoSimple(x,y,0.6,0.001,10)
print("b" ,b,"\nalphas" ,alpha)