1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| import numpy as np import random from matplotlib import pyplot as plt from matplotlib.patches import Circle
global g_y_vec global g_x_mat global g_m global g_alpha global g_C global g_w global g_b global g_y_now global g_err
g_x_mat = np.array([[0.602, 0.732], [-0.599, 0.945], [-0.337, -0.677], [0.459, -0.486], [1.056, 0.137], [0.838, 0.623], [1.022, -0.685], [0.504, 0.821], [-0.977, -0.724], [1.116, 0.56], [0.969, 0.151], [-0.693, 0.077], [1.042, -0.146], [0.705, -0.215], [1.024, -0.322], [1.025, -0.172], [0.306, -1.12], [-0.131, 0.008], [-1.157, -1.081], [0.452, -0.865], [-1.117, -0.533], [-1.083, -0.355], [-0.982, 0.572], [-1.053, 1.003], [-0.553, -0.434], [-0.115, 0.283], [0.785, 0.233], [-0.926, -0.299], [-1.039, 0.581], [0.869, -1.033], [0.754, -1.091], [-1.096, -0.311], [0.537, 0.508], [-0.38, -0.565], [1.165, 0.219], [-0.123, 0.431], [1.048, -0.896], [-0.409, 0.299], [0.537, -0.126], [0.985, -0.577], [-1.135, 1.025], [-0.779, 0.81], [0.547, 0.697], [0.424, -1.015], [0.421, -0.904], [0.151, -0.149], [0.77, -1.011], [-0.401, 1.113], [0.817, 0.573], [0.87, -0.266], [-0.731, 0.418], [-0.651, 0.063], [0.731, 0.04], [0.649, 0.677], [-0.084, -0.568], [0.391, -0.171], [-1.07, 0.738], [-0.307, 0.702], [0.854, 1.125], [0.093, -0.148], [-0.82, 0.969], [0.11, -1.011], [0.672, -0.261], [0.6, -0.262], [0.28, 0.001], [-0.005, -0.544], [-0.666, 0.046], [-0.457, -0.129], [-1.02, 1.071], [1.191, 0.121], [0.665, -0.884], [0.412, 0.665], [-0.992, -1.165], [-0.726, -1.178], [-0.886, 1.08], [0.263, 0.481], [-0.051, 0.668], [0.933, -0.008], [-0.896, -0.637], [-0.605, 0.287], [0.03, -0.232], [0.749, 0.012], [1.175, 0.632], [0.968, 1.106], [-1.19, 0.82], [0.641, 0.129], [-0.375, -1.079], [-0.267, -0.442], [0.361, -0.741], [-0.475, 0.473], [0.133, 1.18], [1.146, 1.185], [-0.293, 0.172], [0.78, -0.805], [0.186, -0.089], [-0.068, 0.829], [-0.621, -0.778], [0.407, -0.523], [0.415, -0.01], [-0.229, 0.002], [-0.997, -0.891], [1.011, -1.186], [0.19, -0.437], [0.958, 0.669], [-0.888, -0.217], [0.444, 0.05], [-0.54, -1.041], [-0.314, 0.296], [0.879, -0.898], [0.127, -0.008], [0.995, -1.11], [-0.878, -0.843], [-0.109, 0.189], [0.859, 0.564], [-0.023, 0.945], [-0.878, 0.899], [-0.062, -1.051], [0.394, 0.519], [-1.139, 0.282], [-0.494, -0.075], [-0.922, 1.11], [0.753, -1.018], [0.816, -1.106], [0.03, 0.569], [-1.11, -0.289], [0.777, 0.025], [0.892, 0.784], [0.91, 0.176], [0.692, 0.099], [0.97, 0.58], [0.034, 1.151], [-0.606, -0.775], [0.873, -0.579], [0.833, -1.042], [-0.251, 0.102], [0.436, -0.585], [0.86, -1.06], [-1.118, 1.094], [0.598, -0.129], [0.694, 0.281], [1.048, -1.036], [-0.348, 0.639], [1.046, -1.124], [-0.333, -0.463], [-0.447, -0.009], [0.344, -0.852], [-1.174, 0.196], [0.701, 0.695], [-0.916, -0.128], [-0.597, -0.934]]) g_y_vec = np.array([1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, 1, -1])
def calcLH(id1,id2): if g_y_vec[id1] == g_y_vec[id2]: L = max(0,g_alpha[id1]+g_alpha[id2]-g_C) H = min(g_C,g_alpha[id1]+g_alpha[id2]) else: L = max(0,g_alpha[id2]-g_alpha[id1]) H = min(g_C,g_C+g_alpha[id2]-g_alpha[id1]) return (L,H)
def svmOutput(id1): global g_b sum = 0; for i in range(g_m): sum += g_alpha[i]*g_y_vec[i]*kernel(i,id1) return sum+g_b
def kernel(id1,id2): x_1 = g_x_mat[id1] x_2 = g_x_mat[id2] val = np.subtract(x_1,x_2) val = np.dot(val,val) return np.exp(-val)
def compareFun(id1,id2,L,H): global g_b y_1 = g_y_vec[id1] y_2 = g_y_vec[id2] e_1 = g_err[id1] e_2 = g_err[id2] alpha_1 = g_alpha[id1] alpha_2 = g_alpha[id2] k11 = kernel(id1,id1) k12 = kernel(id1,id2) k22 = kernel(id2,id2) s = y_1*y_2 f_1 = y_1*(e_1+g_b)-alpha_1*k11-s*alpha_2*k12 f_2 = y_2*(e_2+g_b)-s*alpha_1*k12-alpha_2*k22 L_1 = alpha_1+s*(alpha_2-L) H_1 = alpha_1+s*(alpha_2-H) phi_l = L_1*f_1+L*f_2+0.5*L_1*L_1*k11+0.5*L*L*k22+s*L*L_1*k12 phi_h = H_1*f_1+H*f_2+0.5*H_1*H_1*k11+0.5*H*H*k22+s*H*H_1*k12 if L==H: return 0 if phi_l < phi_h: return 1 else: return -1
def takeStep(id1,id2,err): if id1==id2: return 0 alpha_1 = g_alpha[id1] alpha_2 = g_alpha[id2] y_1 = g_y_vec[id1] y_2 = g_y_vec[id2] e1 = g_err[id1] e2 = g_err[id2] s = y_1*y_2 L,H=calcLH(id1,id2) if L==H : return 0 k11 = kernel(id1,id1) k12 = kernel(id1,id2) k22 = kernel(id2,id2) eta = k11+k22-2*k12
alpha_2_new = alpha_2 if eta>0 : alpha_2_new = alpha_2+y_2*(e1-e2)/eta alpha_2_new = max(alpha_2_new,L) alpha_2_new = min(alpha_2_new,H) else: ret = compareFun(id1, id2, L, H) if ret == 0: alpha_2_new = alpha_2 elif ret == 1: alpha_2_new = L elif ret == 1: alpha_2_new = H print("----------------eta<=0----------------")
if alpha_2_new < err: alpha_2_new = 0 elif alpha_2_new > g_C - err: alpha_2_new = g_C if abs(alpha_2_new-alpha_2) < err: print("alpha_2 is no need to update") return 0
global g_b alpha_1_new = alpha_1 + s * (alpha_2 - alpha_2_new) b1_new = -e1-y_1*k11*(alpha_1_new-alpha_1)-y_2*k12*(alpha_2_new-alpha_2) + g_b b2_new = -e2-y_1*k12*(alpha_1_new-alpha_1)-y_2*k22*(alpha_2_new-alpha_2) + g_b if alpha_1_new>0 and not alpha_1_new<g_C: g_b = b1_new elif alpha_2_new>0 and not alpha_2_new<g_C: g_b = b2_new else: g_b = 0.5*(b1_new+b2_new)
g_alpha[id1] = alpha_1_new g_alpha[id2] = alpha_2_new print("g_alpha[id1]=",g_alpha[id1],"g_alpha[id2]=",g_alpha[id2],"s=",s,", alpha_1=",alpha_1,", alpha_2=",alpha_2)
updateYAndErr()
return 1
def updateYAndErr(): for i in range(g_m): g_y_now[i] = svmOutput(i) g_err[i] = svmOutput(i)-g_y_vec[i]
def chooseBestAlphaIndex(id2): maxIncr = 0 maxIndex = -1; for i in range(g_m): incr = abs(g_err[i]-g_alpha[i]) if incr >= maxIncr: maxIndex = i maxIncr = incr return maxIndex
def sizeOfNonZerorAndNonC(): size=0 for i in range(g_m): if g_alpha[i]!=0 and g_alpha[i]!=g_C: size= size+1 return size
def chooseRandomIndex(id2): ret = id2; while ret==id2: ret = random.randint(0,g_m-1) return ret
def examineExample(id2): y_2 = g_y_vec[id2] tol = 1e-2 alpha_2 = g_alpha[id2] e_2 = svmOutput(id2)-g_y_vec[id2] r_2 = e_2 * y_2
if r_2 < -tol and alpha_2 < g_C or r_2 > tol and alpha_2 > 0 : if sizeOfNonZerorAndNonC()>0: id1=chooseBestAlphaIndex(id2) if takeStep(id1,id2,1e-3): return 1 r=chooseRandomIndex(id2) for i in range(g_m): id1 = (r+i)%g_m if id1!=id2 and g_alpha[id1]!=0 and g_alpha[id1]!=g_C: if takeStep(id1,id2,1e-3): return 1 r = chooseRandomIndex(id2) for i in range(g_m): id1 = (r + i) % g_m if id1 != id2: if takeStep(id1,id2,1e-3): return 1 return 0
def kernel_test(id1,x): x_1 = g_x_mat[id1] val = np.subtract(x_1,x) val = np.dot(val,val) return np.exp(-val)
def svmOutput_test(alpha,b,x): global g_m sum = 0 for i in range(g_m): if alpha[i]!=0: sum += alpha[i]*g_y_vec[i]*kernel_test(i,x) return sum+\ b
def showPic(alpha,b): figure, ax = plt.subplots() ax.set_xlim(left=-2, right=2) ax.set_ylim(bottom=-2, top=2)
x=[] for j in range(100): x_0 = random.randint(-1200,1200)/1000 x_1 = random.randint(-1200,1200)/1000 x.append([x_0,x_1])
for i in range(100): if svmOutput_test(alpha,b,x[i])>0: plt.plot(x[i][0], x[i][1], 'b--', marker='+', color='r') else: plt.plot(x[i][0], x[i][1], 'b--', marker='o', color='b')
cir1 = Circle(xy=(0.0, 0.0), radius=1) ax.add_patch(cir1)
plt.xlabel("x1") plt.ylabel("x2") plt.plot() plt.show()
if __name__=="__main__": SHOW_PIC = True
g_m = len(g_x_mat) if SHOW_PIC == False: g_alpha = np.zeros(g_m) g_y_now = np.zeros(g_m) g_err = np.zeros(g_m) global g_b g_b = 0 numChanged = 0 examineAll = 1 g_C = 10 err = 0 updateYAndErr() while numChanged>0 or examineAll: numChanged = 0 if examineAll: for i in range(g_m): numChanged += examineExample(i) else: for i in range(g_m): if g_alpha[i]!=0 and g_alpha[i]!=g_C: numChanged += examineExample(i) examineAll = abs(examineAll-1) print(g_alpha, g_b) else: alpha= [ 0.00000000e+00, 7.59410972e-01, -2.22044605e-16, 0.00000000e+00, 1.00000000e+01, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 0.00000000e+00, 5.67018243e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 0.00000000e+00, 2.45788486e+00, 1.33286169e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.11022302e-16, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 7.22541284e+00, 0.00000000e+00, -1.38777878e-17, 0.00000000e+00, -2.77555756e-17, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 8.66273771e-04, 0.00000000e+00, 8.33442222e+00, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 1.00000000e+01, -2.77555756e-17, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.39461998e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -2.22044605e-16, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -2.77555756e-17, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -5.55111512e-17, 0.00000000e+00, 0.00000000e+00, 6.59194921e-17, 0.00000000e+00, 1.00000000e+01, 1.00000000e+01, -1.38777878e-17, 5.21868376e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -2.22044605e-16, 6.56986459e+00, 0.00000000e+00, 5.19744490e+00, 9.69510083e+00, 1.00000000e+01, 1.00000000e+01, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 8.69782724e+00, 0.00000000e+00, 1.00000000e+01, 3.24960991e+00, 8.23041098e+00] b = -2.68518972087 showPic(alpha,b)
|