Esempio n. 1
0
def plot_convergence_RealData(size, train, tr_label, test='', te_label=''):
    if size != 'l':
        train_data = scio.loadmat(train)
        train_label = scio.loadmat(tr_label)
        a = train_data['A']
        b = train_label['b']
        a = a[:int(0.7 * np.shape(a)[0])]
        a1 = a[int(0.7 * np.shape(a)[0]):]
        b = b[:int(0.7 * np.shape(b)[0])]
        b1 = b[int(0.7 * np.shape(b)[0]):]
        m, n = np.shape(a)
        m1, n1 = np.shape(a1)
        a = hstack((a, np.ones((m, 1))))
        a1 = hstack((a1, np.ones((m1, 1))))
        m, n = np.shape(a)
        x = np.zeros((n, 1))
    else:
        train_data = scio.loadmat(train)
        train_label = scio.loadmat(tr_label)
        test_data = scio.loadmat(test)
        test_label = scio.loadmat(te_label)

        a = train_data['A']
        b = train_label['b']

        a1 = test_data['A']
        b1 = test_label['b']

        m, n = np.shape(a)
        m1, n1 = np.shape(a1)
        a = hstack((a, np.ones((m, 1))))
        a1 = hstack((a1, np.ones((m1, 1))))
        m, n = np.shape(a)
        x = np.zeros((n, 1))

    x_LBFGS, norm_LBFGS, _, acc_LBFGS, duration_LBFGS, diteration_LBFGS = LR.L_BFGS(
        a, b, x, args.m, args.l, True, a1, b1)
    f_LBFGS = open('LR_' + size + '.txt', mode='a+')
    f_LBFGS.write(
        np.str(acc_LBFGS) + ' ' + str(duration_LBFGS) + ' ' +
        str(diteration_LBFGS))

    x_BFGS, norm_BFGS, _, acc_BFGS, duration_BFGS, diteration_BFGS = SVM.L_BFGS(
        a, b, x, args.m, args.l, args.d, True, a1, b1)
    f_BFGS = open('svm_' + size + '.txt', mode='a+')
    f_BFGS.write(
        np.str(acc_BFGS) + ' ' + str(duration_BFGS) + ' ' +
        str(diteration_BFGS))

    Draw.gradient_plot(len(norm_BFGS), norm_BFGS, 'SVM')
    Draw.gradient_plot(len(norm_LBFGS), norm_LBFGS, 'LR')
    plt.savefig('SVM_VS_LR_LBFGS_' + size)
    plt.show()
    plt.close()