1. 程式人生 > >Python(線性不可分SVM)

Python(線性不可分SVM)

for t in range(maxgen):     i1 = v1(a,y_train,gram)     i2 = v2(i1,n)          old_a1,old_a2 = a[i1],a[i2]     old_y1,old_y2 = y_train[i1],y_train[i2]     if old_y1 == old_y2:         L = max(0,old_a2+old_a1-C)         H = min(C,old_a2+old_a1)     else:         L = max(0,old_a2-old_a1)         H = min(C,old_a2-old_a1+C)     E1 = np.dot((a*y_train).T,gram[:,i1])+b - y_train[i1]     E2 = np.dot((a*y_train).T,gram[:,i2])+b - y_train[i2]     g = gram[i1,i1]+gram[i2,i2]-2*gram[i1,i2]     tmp_a2 = old_a2 + y_train[i2]*(E1-E2)/g          if tmp_a2>H:         a[i2] = H     if tmp_a2 >=L and tmp_a2<=H:         a[i2] = tmp_a2     if tmp_a2<L:         a[i2] = L              deta_a2 = a[i2]-old_a2     a[i1] = old_a1-old_y1*old_y2*deta_a2     deta_a1 = a[i1]-old_a1     dw = (y_train[i1]*deta_a1,y_train[i2]*deta_a2)     b1 = -E1-y_train[i1]*gram[i1,i1]*deta_a1-y_train[i2]*gram[i1,i2]*deta_a2     b2 = -E2-y_train[i1]*gram[i1,i2]*deta_a1-y_train[i2]*gram[i2,i2]*deta_a2     db = (b1+b2)/2     p = p+dw[0]*gram[i1].T+dw[1]*gram[i2].T+db