Author: Not specified Language: python Description: (v5) Timestamp: 2018-05-21 03:11:32 +0000
 View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply
1. while(passes < max_passes):
2.
3.     num_changed_alphas = 0
4.     for i in range(m):
5.
6.         E_i = decision_function(alphas,data,b,data[i,0:2]) - data[i,2]
7.         #print "COmputed E_i"
8.         if(((data[i,2] * E_i) < (-1) * tol and alphas[i] < C) or ((data[i,2]*E_i)>tol and alphas[i]>0 )):
9.             j = np.random.randint(0,m)
10.             while(j==i):
11.                 j = np.random.randint(0,m)
12.
13.             E_j = decision_function(alphas,data,b,data[j,0:2]) - data[j,2]
14.
15.             #print "COmputed E_j"
16.             alpha_i_old = alphas[i]
17.             alpha_j_old = alphas[j]
18.
19.             L,H = compute_range(alphas,i,j,data,C)
20.
21.             if L == H:
22.                 continue
23.             k_ij = gaussian_kernel(data[i,0:2],data[j,0:2])
24.             k_ii = gaussian_kernel(data[i,0:2],data[i,0:2])
25.             k_jj = gaussian_kernel(data[j,0:2],data[j,0:2])
26.             eta = 2 * k_ij - k_ii - k_jj
27.             #print "COmputed Eta"
28.             if eta>=0:
29.                 continue
30.
31.             alphas[j] = alphas[j] - (data[j,2] * (E_i - E_j)/eta)
32.
33.             if alphas[j] > H:
34.                 alphas[j] = H
35.             elif alphas[j] < L:
36.                 alphas[j] = L
37.
38.             if abs(alphas[j] - alpha_j_old) < 0.00001:
39.                 continue
40.
41.             alphas[i] = alphas[i] + data[i,2] * data[j,2] * (alpha_j_old - alphas[j])
42.
43.             b_1 = b - E_i - data[i,2]*(alphas[i]-alpha_i_old)*k_ii - data[j,2]*(alphas[j]-alpha_j_old)*k_ij
44.
45.             b_2 = b - E_j - data[i,2]*(alphas[i]-alpha_i_old)*k_ij - data[j,2]*(alphas[j]-alpha_j_old)*k_ij
46.             #print "COmputed b's"
47.             if alphas[i]<C and alphas[i] > 0:
48.                 b = b_1
49.             elif alphas[j]<C and alphas[j] > 0:
50.                 b = b_2
51.             else:
52.                 b = (b_1+b_2)/2.0
53.             num_changed_alphas += 1
54.
55.     if num_changed_alphas == 0:
56.         print "Currently at Pass {}".format(passes)
57.         passes += 1
58.         print "Pass Updated"
59.         print "++++++++++++++++++++++++++++++++"
60.     else:
61.         passes = 0
 View raw paste Parent paste by: Not specified - Child paste by: Not specified Reply