plt.show()

    X = map_feature(X[:, 0:1], X[:, 1:])
    m, n = X.shape

    initial_theta = np.ones((n, 1))
    lambda_coef = 0.1
    theta = minimize(cost_function, initial_theta, method='BFGS', jac=grad_function, options={'disp': False},
                    args=(X, y, lambda_coef)).x

    u = np.linspace(-1, 1.5, 50)
    v = np.linspace(-1, 1.5, 50)

    X1, X2 = np.meshgrid(u, v)
    X1, X2 = X1.reshape(-1, 1), X2.reshape(-1, 1)

    temp_X = map_feature(X1, X2)
    z = np.dot(temp_X, theta).reshape(len(u), len(v))

    plt.plot(x1[f_y == 0], x2[f_y == 0], 'yo')
    plt.plot(x1[f_y == 1], x2[f_y == 1], 'bx')
    CS = plt.contour(u, v, z)
    plt.clabel(CS, inline=1, fontsize=10)
    plt.show()

    for lambda_coef in (0.03, 0.3, 0.1, 1, 3, 10):
        initial_theta = np.ones((n, 1))
        theta = minimize(cost_function, initial_theta, method='BFGS', jac=grad_function, options={'disp': False},
                        args=(X, y, lambda_coef)).x
        print 'lambda = {}, Train Accuracy: {}'.format(lambda_coef, lr_accuracy(X, y, theta))
Example #2
0
    f_y = y.ravel()
    plt.plot(x1[f_y == 0], x2[f_y == 0], 'yo')
    plt.plot(x1[f_y == 1], x2[f_y == 1], 'bx')

    plt.show()

    X = add_zero_feature(X)
    m, n = X.shape
    initial_theta = np.ones((n, 1))

    theta = minimize(cost_function,
                     initial_theta,
                     method='BFGS',
                     jac=grad_function,
                     options={
                         'disp': False
                     },
                     args=(X, y)).x
    print theta
    print cost_function(theta, X, y)

    x1_boundery = np.array([np.min(x1) - 2, np.max(x1) + 2])
    x2_boundery = (-1 / theta[2]) * (theta[1] * x1_boundery + theta[0])

    plt.plot(x1[f_y == 0], x2[f_y == 0], 'yo')
    plt.plot(x1[f_y == 1], x2[f_y == 1], 'bx')
    plt.plot(x1_boundery, x2_boundery)
    plt.show()

    print 'Train Accuracy: {}'.format(lr_accuracy(X, y, theta))
Example #3
0
    X, y = load_data('ex2data1.txt')

    x1, x2 = X.T
    f_y = y.ravel()
    plt.plot(x1[f_y==0], x2[f_y==0], 'yo')
    plt.plot(x1[f_y==1], x2[f_y==1], 'bx')

    plt.show()

    X = add_zero_feature(X)
    m, n = X.shape
    initial_theta = np.ones((n, 1))

    theta = minimize(cost_function, initial_theta, method='BFGS', jac=grad_function, options={'disp': False},
                    args=(X, y)).x
    print theta
    print cost_function(theta, X, y)

    x1_boundery = np.array([np.min(x1)-2, np.max(x1)+2])
    x2_boundery = (-1/theta[2])*(theta[1]*x1_boundery + theta[0])

    plt.plot(x1[f_y==0], x2[f_y==0], 'yo')
    plt.plot(x1[f_y==1], x2[f_y==1], 'bx')
    plt.plot(x1_boundery, x2_boundery)
    plt.show()


    print 'Train Accuracy: {}'.format(lr_accuracy(X, y, theta))