Esempio n. 1
0
def showFeatureSpace(X, W, rho):
    plt.figure()
    filename = "feature(" + str(W) + "," + str(rho) + ")2g.png"

    print("==================")

    classSize = len(W)
    canContinue = True

    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c='green', s=50, marker="o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c='yellow', s=50, marker="s")

    while canContinue:
        canContinue = False
        cntIncorrectClass = 0

        for i in range(len(X)):

            g = []
            for c in range(classSize):
                g.append(mathfunction.g(X[i], W[c]))

            maxClass = np.nanargmax(g) + 1
            rightClass = X[i][2]

            print g
            if maxClass != rightClass:
                print str(rightClass) + "→" + str(maxClass)
                W[rightClass - 1][0] += rho * X[i][0]
                W[rightClass - 1][1] += rho * X[i][1]

                W[maxClass - 1][0] -= rho * X[i][0]
                W[maxClass - 1][1] -= rho * X[i][1]

                canContinue = True
                cntIncorrectClass += 1

        print("IncorrectClass:" + str(cntIncorrectClass))

    # 学習後識別関数の表示
    showGfunction("black", 1)
    showGfunction("red", 2)

    intersection_x = (W[0][0] - W[1][0]) / (W[1][1] - W[0][1])
    intersection_y = (W[1][1] * W[0][0] - W[0][1] * W[1][0]) / (W[1][1] -
                                                                W[0][1])

    plt.plot([intersection_x, intersection_x], [0, intersection_y],
             color="blue",
             linewidth=2,
             linestyle='--')

    matplotConfig.forFeature(filename, 0)
    plt.savefig(filename)  #CUIでshowは使えない
def showFeatureSpace(X, W , rho):
    plt.figure()
    filename = "feature(" + str(W) + "," + str(rho) + ")2g.png"
    
    print("==================")
    
    classSize = len(W)
    canContinue = True
    
    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c = 'green', s = 50, marker = "o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c = 'yellow', s= 50, marker = "s")
    
    while canContinue:
        canContinue = False
        cntIncorrectClass = 0
        
        for i in range(len(X)):
            
            g = []
            for c in range(classSize):
                g.append(mathfunction.g(X[i], W[c]))
            
            maxClass = np.nanargmax(g) + 1
            rightClass = X[i][2]
            
            print g
            if maxClass != rightClass:
                print str(rightClass) + "→" + str(maxClass)
                W[rightClass-1][0] += rho * X[i][0]
                W[rightClass-1][1] += rho * X[i][1]
                
                W[maxClass-1][0] -= rho * X[i][0]
                W[maxClass-1][1] -= rho * X[i][1]
                
                canContinue = True
                cntIncorrectClass += 1
                
        print("IncorrectClass:" + str(cntIncorrectClass))

    # 学習後識別関数の表示
    showGfunction("black",1)
    showGfunction("red",2)
    
    intersection_x = (W[0][0] - W[1][0]) / (W[1][1] - W[0][1])
    intersection_y = (W[1][1]*W[0][0] - W[0][1]*W[1][0]) / (W[1][1] - W[0][1])
    
    plt.plot([intersection_x, intersection_x],[0, intersection_y], color = "blue", linewidth = 2, linestyle = '--')
    
    matplotConfig.forFeature(filename, 0)
    plt.savefig(filename) #CUIでshowは使えない
def showWeightSpace(X, W, row):
    plt.figure()
    filename = "weight(" + str(W[1]) + "," + str(
        W[0]) + "," + str(row) + ").png"

    W_copy = list(W)
    canContinue = True

    plt.scatter(W[1], W[0], c='blue', marker="o")

    while canContinue:
        canContinue = False
        for i in range(len(X)):

            g_value = mathfunction.g(X[i], W)
            print(str(X[i]) + ":" + str(W) + ":" + str(g_value))
            if g_value < 0 and X[i][2] == 1:
                print("1→2")
                W[0] = W[0] + row * X[i][0]
                W[1] = W[1] + row * X[i][1]
                canContinue = True
                plt.scatter(W[1], W[0], c='red', marker="o")
                plt.plot([W_copy[1], W[1]], [W_copy[0], W[0]], color="red")
                W_copy = list(W)
            elif g_value > 0 and X[i][2] == 2:
                print("2→1")
                W[0] = W[0] - row * X[i][0]
                W[1] = W[1] - row * X[i][1]
                canContinue = True
                plt.scatter(W[1], W[0], c='red', marker="o")
                plt.plot([W_copy[1], W[1]], [W_copy[0], W[0]], color="red")
                W_copy = list(W)

    # 解領域の表示
    w1 = x = np.linspace(-3, 13)
    for i in range(len(X)):
        if X[i][2] == 1:
            color = '#ffff00'
        elif X[i][2] == 2:
            color = '#00ff00'
        w0 = -X[i][1] * w1
        plt.plot(w1, w0, color, linewidth=1.0, linestyle="-")

    matplotConfig.forWeight(filename)
    plt.savefig(filename)  #CUIでshowは使えない
def widrowhoff():
    
    classSize = len(B)
    epoch = 100
    
    for ep in range(epoch):
        print("---"+ str(ep+1) + "th---")
        
        total_epsilon = 0
        
        for p in range(len(X)):
            
            g_v = []
            e_v = []
            p_total_epsilon = 0
            for i in range(classSize):
                g = mathfunction.g(X[p], W[i])
                
                    
                e = g - B[X[p][2]-1][i]
                W[i][0] -= rho * X[p][0] * e
                W[i][1] -= rho * X[p][1] * e
                g_v.append(g)
                e_v.append(e)
            
            p_total_epsilon = math.fabs(e_v[i])
                    
            g_str = ""
            B_str = ""
            e_str = ""
            for i in range(classSize):
                g_str += str(g_v[i]) + ","
                B_str += str(B[X[p][2]-1][i]) + ","
                e_str += str(e_v[i]) + ","
            print("Xp:" + str(p) 
            + " g:[" + g_str 
            + "] B:[" + B_str 
            + "] e:[" + e_str + "]"
            + "=" + str(p_total_epsilon))
            
            
            for i in range(classSize):
                total_epsilon += p_total_epsilon

        print("total_e:" + str(total_epsilon))
def showWeightSpace(X, W, row):
    plt.figure()
    filename = "weight(" + str(W[1]) + "," + str(W[0]) + "," + str(row) + ").png"
    
    W_copy = list(W)
    canContinue = True
    
    plt.scatter(W[1], W[0], c = 'blue', marker = "o")
    
    while canContinue:
        canContinue = False
        for i in range(len(X)):
            
            g_value = mathfunction.g(X[i], W)
            print(str(X[i]) + ":" + str(W) + ":" + str(g_value))
            if g_value < 0 and X[i][2] == 1:
                print("1→2")
                W[0] = W[0] + row * X[i][0]
                W[1] = W[1] + row * X[i][1]
                canContinue = True
                plt.scatter(W[1], W[0], c = 'red', marker = "o")
                plt.plot([W_copy[1], W[1]], [W_copy[0], W[0]], color="red")
                W_copy = list(W)
            elif g_value > 0 and X[i][2] == 2:
                print("2→1")
                W[0] = W[0] - row * X[i][0]
                W[1] = W[1] - row * X[i][1]
                canContinue = True
                plt.scatter(W[1], W[0], c = 'red', marker = "o")
                plt.plot([W_copy[1], W[1]], [W_copy[0], W[0]], color="red")
                W_copy = list(W)
    
    # 解領域の表示
    w1 = x = np.linspace(-3, 13)
    for i in range(len(X)):
        if X[i][2] == 1:
            color = '#ffff00'
        elif X[i][2] == 2:
            color = '#00ff00'
        w0 = -X[i][1] * w1
        plt.plot(w1, w0, color, linewidth=1.0, linestyle="-")
    
    matplotConfig.forWeight(filename)
    plt.savefig(filename) #CUIでshowは使えない
def widrowhoff():

    classSize = len(B)
    epoch = 100

    for ep in range(epoch):
        print("---" + str(ep + 1) + "th---")

        total_epsilon = 0

        for p in range(len(X)):

            g_v = []
            e_v = []
            p_total_epsilon = 0
            for i in range(classSize):
                g = mathfunction.g(X[p], W[i])

                e = g - B[X[p][2] - 1][i]
                W[i][0] -= rho * X[p][0] * e
                W[i][1] -= rho * X[p][1] * e
                g_v.append(g)
                e_v.append(e)

            p_total_epsilon = math.fabs(e_v[i])

            g_str = ""
            B_str = ""
            e_str = ""
            for i in range(classSize):
                g_str += str(g_v[i]) + ","
                B_str += str(B[X[p][2] - 1][i]) + ","
                e_str += str(e_v[i]) + ","
            print("Xp:" + str(p) + " g:[" + g_str + "] B:[" + B_str + "] e:[" +
                  e_str + "]" + "=" + str(p_total_epsilon))

            for i in range(classSize):
                total_epsilon += p_total_epsilon

        print("total_e:" + str(total_epsilon))
def showFeatureSpace(X, W, row):
    plt.figure()
    filename = "feature(" + str(W[1]) + "," + str(
        W[0]) + "," + str(row) + ").png"

    canContinue = True

    showGfunction("blue")

    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c='green', s=50, marker="o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c='yellow', s=50, marker="s")

    while canContinue:
        canContinue = False
        for i in range(len(X)):

            g_value = mathfunction.g(X[i], W)
            print(str(X[i]) + ":" + str(W) + ":" + str(g_value))

            if g_value < 0 and X[i][2] == 1:
                print("1→2")
                W[0] = W[0] + row * X[i][0]
                W[1] = W[1] + row * X[i][1]
                showGfunction("red")
                canContinue = True
            elif g_value > 0 and X[i][2] == 2:
                print("2→1")
                W[0] = W[0] - row * X[i][0]
                W[1] = W[1] - row * X[i][1]
                showGfunction("red")
                canContinue = True

    # 学習後識別関数の表示
    showGfunction("black")

    matplotConfig.forFeature(filename, -2)
    plt.savefig(filename)  #CUIでshowは使えない
def showFeatureSpace(X, W , row):
    plt.figure()
    filename = "feature(" + str(W[1]) + "," + str(W[0]) + "," + str(row) + ").png"
    
    canContinue = True
    
    showGfunction("blue")
    
    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c = 'green', s = 50, marker = "o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c = 'yellow', s= 50, marker = "s")
    
    while canContinue:
        canContinue = False
        for i in range(len(X)):
            
            g_value = mathfunction.g(X[i], W)
            print(str(X[i]) + ":" + str(W) + ":" + str(g_value))
            
            if g_value < 0 and X[i][2] == 1:
                print("1→2")
                W[0] = W[0] + row * X[i][0]
                W[1] = W[1] + row * X[i][1]
                showGfunction("red")
                canContinue = True
            elif g_value > 0 and X[i][2] == 2:
                print("2→1")
                W[0] = W[0] - row * X[i][0]
                W[1] = W[1] - row * X[i][1]
                showGfunction("red")
                canContinue = True
    
    # 学習後識別関数の表示
    showGfunction("black")
    
    matplotConfig.forFeature(filename, -2)
    plt.savefig(filename) #CUIでshowは使えない
def widrowhoff():
    
    classSize = len(B)
    epoch = 10
    filename = "linearlyseparable.png"
    
    plt.figure()
    plt.text(0, -20, "epoch=" + str(epoch), ha = 'center', va = 'bottom', size = 'xx-large')
    
    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c = 'black', s = 50, marker = "o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c = 'black', s= 50, marker = "x")
    
    showG1minusG2function("blue", "start")
    
    for i in range(epoch):
        print("---"+ str(i+1) + "th---")
        
        total_epsilon = 0
        
        for p in range(len(X)):
            
            g = []
            for c in range(classSize):
                g.append(mathfunction.g(X[p], W[c]))
            
            if X[p][2] == 1:
                
                epsilon = []
                for w_c in range(classSize):
                    e = g[w_c] - B[0][w_c]
                    W[w_c][0] -= rho * e * X[p][0]
                    W[w_c][1] -= rho * e * X[p][1] 
                    epsilon.append(e)
                        
                print("Xp:" + str(p) + " g:[" + str(g[0]) + "," + str(g[1]) 
                + "] B:[" + str(B[0][0]) + "," + str(B[0][1]) + "] e:["
                + str(epsilon[0]) + "," + str(epsilon[1]) + "]")
                
                # w_c : 各クラスの識別関数gに用いる重み
                for w_c in range(classSize):
                    total_epsilon +=math.fabs(epsilon[w_c])

            elif X[p][2] == 2:
                
                epsilon = []
                for w_c in range(classSize):
                    e = g[w_c] - B[1][w_c]
                    W[w_c][0] -= rho * e * X[p][0]
                    W[w_c][1] -= rho * e * X[p][1] 
                    epsilon.append(e)
                
                print("Xp:" + str(p) + " g:[" + str(g[0]) + "," + str(g[1]) 
                + "] B:[" + str(B[1][0]) + "," + str(B[1][1]) + "] e:["
                + str(epsilon[0]) + "," + str(epsilon[1]) + "]")
                
                for w_c in range(classSize):
                    total_epsilon +=math.fabs(epsilon[w_c])

                
        print("total_e:" + str(total_epsilon))
        showG1minusG2function("red", "")
        
    # 学習後識別関数の表示
    showG1minusG2function("green", "finish")
    
    matplotConfig.forFeature(filename)
    plt.savefig(filename) #CUIでshowは使えない
Esempio n. 10
0
def widrowhoff():

    classSize = len(B)
    epoch = 10
    filename = "linearlyseparable.png"

    plt.figure()
    plt.text(0,
             -20,
             "epoch=" + str(epoch),
             ha='center',
             va='bottom',
             size='xx-large')

    for i in range(len(X)):
        if X[i][2] == 1:
            plt.scatter(X[i][1], 0, c='black', s=50, marker="o")
        elif X[i][2] == 2:
            plt.scatter(X[i][1], 0, c='black', s=50, marker="x")

    showG1minusG2function("blue", "start")

    for i in range(epoch):
        print("---" + str(i + 1) + "th---")

        total_epsilon = 0

        for p in range(len(X)):

            g = []
            for c in range(classSize):
                g.append(mathfunction.g(X[p], W[c]))

            if X[p][2] == 1:

                epsilon = []
                for w_c in range(classSize):
                    e = g[w_c] - B[0][w_c]
                    W[w_c][0] -= rho * e * X[p][0]
                    W[w_c][1] -= rho * e * X[p][1]
                    epsilon.append(e)

                print("Xp:" + str(p) + " g:[" + str(g[0]) + "," + str(g[1]) +
                      "] B:[" + str(B[0][0]) + "," + str(B[0][1]) + "] e:[" +
                      str(epsilon[0]) + "," + str(epsilon[1]) + "]")

                # w_c : 各クラスの識別関数gに用いる重み
                for w_c in range(classSize):
                    total_epsilon += math.fabs(epsilon[w_c])

            elif X[p][2] == 2:

                epsilon = []
                for w_c in range(classSize):
                    e = g[w_c] - B[1][w_c]
                    W[w_c][0] -= rho * e * X[p][0]
                    W[w_c][1] -= rho * e * X[p][1]
                    epsilon.append(e)

                print("Xp:" + str(p) + " g:[" + str(g[0]) + "," + str(g[1]) +
                      "] B:[" + str(B[1][0]) + "," + str(B[1][1]) + "] e:[" +
                      str(epsilon[0]) + "," + str(epsilon[1]) + "]")

                for w_c in range(classSize):
                    total_epsilon += math.fabs(epsilon[w_c])

        print("total_e:" + str(total_epsilon))
        showG1minusG2function("red", "")

    # 学習後識別関数の表示
    showG1minusG2function("green", "finish")

    matplotConfig.forFeature(filename)
    plt.savefig(filename)  #CUIでshowは使えない