def main():
    print 'Loading data...'
    X, y = load_data('ex2data2.txt')

    print 'First 10 examples from dataset:'
    print '\t\tX\t\t  y'
    for i in range(10):
        print '  {0}\t{1}'.format(X[i], y[i])

    print 'Plotting data...'
    plot_data(X, y)

    print 'Normalizing features...'
    X, mu, sigma = feature_normalize(X)

    print 'Adding polynomial features...'
    X = map_feature(X[:, 0], X[:, 1])

    initial_theta = np.zeros((X.shape[1], 1))

    lambda_ = 1

    print 'Computing cost...'
    cost, grad = cost_function_reg(initial_theta, X, y, lambda_)

    print 'Optimizing using gradient descent...'
    alpha = 0.05
    num_iters = 1000

    theta = np.zeros((X.shape[1], 1))
    theta, J_history = gradient_descent_multi_reg(
        X, y, theta, alpha, num_iters, lambda_)

    plt.plot(range(1, len(J_history) + 1), J_history, '-b')
    plt.xlabel('Number of iterations')
    plt.ylabel('Cost J')
    plt.show()

    print 'Theta computed from gradient descent:'
    print theta

    chip_data = np.array([[-0.5, 0.7]])
    chip_data = (chip_data - mu) / sigma
    chip_data = np.insert(chip_data, 0, 1, axis=1)
    chip_data = map_feature(chip_data[:, 0], chip_data[:, 1])
    prob = sigmoid(chip_data.dot(theta))
    print 'For a microchip with test of -0.5 and 0.7, we predict an acceptance of {0}%'.format(prob[0, 0] * 100)

    print 'Computing accuracy:\n'
    p = predict(theta, X)
    accuracy = np.mean(y == p)
    print 'Train Accuracy: {0}%'.format(accuracy * 100)
def cost_function_reg(theta, X, y, lambda_):
    h = sigmoid(X.dot(theta))
    log_h = np.log(h)
    one_minus_h = 1 - h
    log_one_minus_h = np.log(one_minus_h)
    vectorized_cost = -y * log_h - (1 - y) * log_one_minus_h
    m = len(y)
    J = np.sum(vectorized_cost) / m
    theta[0] = 0
    regularization = lambda_ / (2 * m) * sum(theta ** 2)
    J = J + regularization
    error = h - y
    grad = X.T.dot(error)
    grad = grad / m
    grad = grad + (lambda_ * theta) / m

    return J, grad
Beispiel #3
0
    lda_costs[lda] = costs

    # newths = np.zeros(Xfm.shape[1])
    # prev_cost = float("inf")
    # costs = []
    # for j in range(niter):
    #     new_cost = logreg.cost(newths,Xfm,y,lda)
    #     if prev_cost -new_cost <= epsilon:
    #         break
    #     costs.append(new_cost)
    #     prev_cost = new_cost

    #     newths = logreg.bgd(newths,Xfm,y,alpha,lda)
    # lda_costs[lda] = costs

    ymg = map(lambda x: 1 if logreg.sigmoid(x) >= 0.5 else 0, np.dot(Xmg,np.matrix(newths).T)) # logreg
    # ymg = map(lambda x: 1 if logreg.sigmoid(x) >= 0.5 else 0, Xmg*np.matrix(newths).T) # logreg
    ymg = np.array(ymg).reshape(xx.shape)

    plt.subplot(pltnum[i])
    plt.title("lambda = " +str(lda))
    plt.xlabel("x_1")
    plt.ylabel("x_2")
    plt.pcolormesh(xx, yy, ymg, cmap=plt.cm.Paired)
    plt.scatter(x1,x2,c=y,cmap=mpl.cm.get_cmap("Greens"))
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())

# find largest x and y range
def plot_costs():
    plt.clf()
res = minimize(cost, initial_theta, (X, y), method='BFGS', jac=True)
theta = res.x
J = res.fun

# Print theta to screen
print('Cost at theta found by fminunc: %f' % J)
print('Expected cost (approx): 0.203')
print('theta: ')
print(theta)
print('Expected theta (approx):')
print(' -25.161\n 0.206\n 0.201')

# Plot Boundary
plot_decision_boundary(theta, X, y)

input('\nProgram paused. Press enter to continue.')

# ============== Part 4: Predict and Accuracies ==============

prob = sigmoid(np.array([1, 45, 85]).dot(theta))
print(
    'For a student with scores 45 and 85, we predict an admission probability of %f'
    % prob)
print('Expected value: 0.775 +/- 0.002\n')

# Compute accuracy on our training set
p = predict(theta, X)

print('Train Accuracy: %f' % (np.mean(p == y) * 100))
print('Expected accuracy (approx): 89.0')
Beispiel #5
0
def eval_probs(X, Theta) :
	(m, n) = X.shape
	probs = np.array([[logreg.sigmoid(np.dot(X[i, :], Theta.transpose()))] \
			for i in range(m)])
	return probs
Beispiel #6
0
    lda_costs[lda] = costs

    # newths = np.zeros(Xfm.shape[1])
    # prev_cost = float("inf")
    # costs = []
    # for j in range(niter):
    #     new_cost = logreg.cost(newths,Xfm,y,lda)
    #     if prev_cost -new_cost <= epsilon:
    #         break
    #     costs.append(new_cost)
    #     prev_cost = new_cost

    #     newths = logreg.bgd(newths,Xfm,y,alpha,lda)
    # lda_costs[lda] = costs

    ymg = map(lambda x: 1 if logreg.sigmoid(x) >= 0.5 else 0,
              np.dot(Xmg,
                     np.matrix(newths).T))  # logreg
    # ymg = map(lambda x: 1 if logreg.sigmoid(x) >= 0.5 else 0, Xmg*np.matrix(newths).T) # logreg
    ymg = np.array(ymg).reshape(xx.shape)

    plt.subplot(pltnum[i])
    plt.title("lambda = " + str(lda))
    plt.xlabel("x_1")
    plt.ylabel("x_2")
    plt.pcolormesh(xx, yy, ymg, cmap=plt.cm.Paired)
    plt.scatter(x1, x2, c=y, cmap=mpl.cm.get_cmap("Greens"))
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())