Пример #1
0
def problem_1c(n=500):
    algorithmList = [
        'Perceptron', 'Perceptron with margin', 'Winnow', 'Winnow with margin',
        'AdaGrad'
    ]
    bestParaList = {500:  [(1, 0), (0.03, 1), (1.1, 0), (1.1, 2.0), (0.25, 1)], \
                    1000: [(1, 0), (0.03, 1), (1.1, 0), (1.1, 2.0), (0.25, 1)]}
    t = Trainer()
    d = t.data_generator(l=10,
                         m=100,
                         n=n,
                         number_of_instances=50000,
                         noise=False)
    x, y = d['x'], d['y']
    initDict = initDictGenerator(n=t.n)
    color = 'rgbyk'
    for idx in range(len(algorithmList)):
        algorithm = algorithmList[idx]
        algorithmInit = initDict[algorithm]
        if n in bestParaList:
            lr, mg = bestParaList[n][idx]
        else:
            lr, mg = bestParaList[500][idx]
        algorithmInit['learning rate'] = [lr]
        algorithmInit['margin'] = [mg]
        t.learning(algorithm,
                   x,
                   y,
                   initDict={algorithm: algorithmInit},
                   times=1)
        t._unpack_resDict(lr, mg)
        plt.plot(t.mistake_list, color[idx])
    plt.legend(algorithmList, loc='best')
    plt.title('Plots for n = %s' % n)
    plt.show()
Пример #2
0
def problem_2_plot():
    algorithmList = [
        'Perceptron', 'Perceptron with margin', 'Winnow', 'Winnow with margin',
        'AdaGrad'
    ]
    bestParaList = {
        40: [(1, 0), (0.25, 1), (1.1, 0), (1.1, 2.0), (1.5, 1)],
        80: [(1, 0), (0.03, 1), (1.1, 0), (1.1, 2.0), (0.25, 1)],
        120: [(1, 0), (0.03, 1), (1.1, 0), (1.1, 2.0), (0.25, 1)],
        160: [(1, 0), (0.03, 1), (1.1, 0), (1.1, 2.0), (0.25, 1)],
        200: [(1, 0), (0.25, 1), (1.1, 0), (1.1, 2.0), (1.5, 1)]
    }

    record = {}
    for algorithm in algorithmList:
        record[algorithm] = []
    for n in range(40, 240, 40):
        print()
        t = Trainer()
        d = t.data_generator(l=10,
                             m=20,
                             n=n,
                             number_of_instances=50000,
                             noise=False)
        x, y = d['x'], d['y']
        initDict = initDictGenerator(n=t.n)
        color = 'rgbyk'

        for idx in range(len(algorithmList)):
            algorithm = algorithmList[idx]

            algorithmInit = initDict[algorithm]
            if n in bestParaList:
                lr, mg = bestParaList[n][idx]
            else:
                lr, mg = bestParaList[500][idx]
            algorithmInit['learning rate'] = [lr]
            algorithmInit['margin'] = [mg]
            t.learningWithStop(algorithm,
                               x,
                               y,
                               initDict={algorithm: algorithmInit},
                               times=1)
            print(len(t.resDict[lr, mg][3]))
            t._unpack_resDict(lr, mg)
            record[algorithm].append(t.mistake_list[-1])

    i = 0
    for algorithm in algorithmList:
        plt.plot(range(40, 240, 40), record[algorithm], color[i])
        i += 1
    plt.legend(algorithmList, loc='best')
    plt.xlabel('Number of total features')
    plt.xticks(range(40, 240, 40))
    plt.ylabel('Total Mistakes before stop')
    #plt.title('Plots for n = %s'%n)
    plt.savefig('p2plot.png', dpi=144)
    plt.show()