File size: 2,506 Bytes
7a1d574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def linear_classifier(c1,c2, iteration, threshold):
    w = [1, 0, 0]
    total = len(c1) + len(c2)
    correct = 0
    step = 0.6
    iteration = 0
    while(correct < total):
        iteration += 1
        if(iteration >= threshold):
            print("The dataset might be not linear separable")
            break
        for x in c1:
            wx = 0
            i = 0
            j = 0
            for xi in x:
                if(i < len(w)):
                    wx += xi * w[i]
                    i += 1
            if (wx <= 0):
                for wj in w:
                    if(j < len(w)):
                        w[j] += step * x[j]
                        j+=1
                correct = 1
            else:
                correct += 1
            if(correct >= total):
                pass
        for x in c2:
            wx = 0
            i = 0
            j = 0
            for xi in x:
                if(i < len(w)):
                    wx += xi * w[i]
                    i += 1
            if (wx > 0):
                for wj in w:
                    if(j < len(w)):
                        w[j] -= step * x[j]
                        j+=1
                correct = 1
            else:
                correct += 1
            if(correct >= total):
                pass
    print("Iterations: {0}".format(iteration))
    print("=======Final Weights========================")
    i = 0
    for wi in w:
        print("w{0}: {1}".format(i,str(wi)))
        i+=1
    print("=======Final Dot Products=============")
    for x in c1:
        wx = 0
        i = 0
        for xi in x:
            if(i < len(w)):
                wx += xi * w[i]
                i += 1
        print(wx)
    print("======================================")
    for x in c2:
        wx = 0
        i = 0
        for xi in x:
            if(i < len(w)):
                wx += xi * w[i]
                i += 1
        print(wx)
    print("=======End=================================")

print("=======Dataset 1==============================================")
c1 = [[1, 1, 3, 5], [1, 2, 3, 10], [1, 3, 5, 9]]
c2 = [[1, -2, -1,-7], [1, -3, -3,-5], [1, -4, 4,-10]]
linear_classifier(c1,c2,0.6,1000)
print("=======Dataset 2(not linear separable)==============================================")
c1 = [[1, 1, 3, 5], [1, -3, -3,-5], [1, 3, 5, 9]]
c2 = [[1, -2, -1,-7], [1, 2, 3, 10], [1, -4, 4,-10]]
linear_classifier(c1,c2,0.3,1000000)