码迷,mamicode.com
首页 > 其他好文 > 详细

smo

时间:2020-01-08 01:03:18      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:shape   rand   sam   initial   sel   sample   amp   matrix   dex   

还在更新中


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]

smo

标签:shape   rand   sam   initial   sel   sample   amp   matrix   dex   

原文地址:https://www.cnblogs.com/tangjunjun/p/12164363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!