其他分享
首页 > 其他分享> > smo

smo

作者:互联网

还在更新中


import numpy as np
import random

# build auxiliary function

class optstruct:
def __init__(self, datax, datay, C, toler):
self.x = datax
self.label = datay
self.c = C
self.tol = toler
self.m = datax.shape[0] # how many samples are there
self.alphas = np.mat(np.zeros((self.m, 1))) # initialize alphas
self.b = 0 # initialize b
self.ercache = np.mat(np.zeros((self.m, 2)))

def calcek(os,k):
fxk=float(np.multiply(os.alphas,os.label).T*(os.x*os.x[k,:].T))+os.b # fx=wx+b,w=alphasi*yi*xi sum from n
Ek=fxk-float(os.label[k]) # Ek=yi-labeli
return Ek



def pickJ(i,os,Ei):
maxk=-1; maxdeltaE=0;Ej=0
os.ercache[i]=[1,Ei]
valerchache=np.nonzero(os.ercache[:,0].A)[0] # work for .A matrix convert array # index value
if (len(valerchache))>1: # if having value , do it
for k in valerchache: # traversal index
if k==i:
continue
Ek=calcek(os,k)
deltas=abs(Ei-Ek)
if deltas>maxdeltaE: # heuristic algorithm
maxk=k
maxdeltaE=deltas
Ej=Ek
return maxk,Ej
else:
j=i
while(j==i):
j=int(random.uniform(0,os.m))
Ej=calcek(os,j)
return j,Ej
def updateEk(os,k):
Ek=calcek(os,k)
os.ercache[k]=[1,Ek]

标签:calcek,Ek,self,smo,np,os,ercache
来源: https://www.cnblogs.com/tangjunjun/p/12164363.html