예제 #1
0
def food_engine(food_list, theta_init, iterations):
    previous_cost = constants.MAX_COST
    if iterations <= 0:
        iterations = constants.ITERATIONS
    if food_list:
        final_foods = food_list
    elif constants.FIRST_RUN:
        final_foods = console.show_food_groups()
        open(constants.INPUT_FILE, 'w').write(','.join(final_foods))
    else:
        final_foods = open(constants.INPUT_FILE, 'r') \
            .read().split('\n')[0].split(',')
    daily_nutrients_limit_y = [
        constants.DAILY_NUTRIENTS_LIMIT[x]
        for x in constants.REQUIRED_NUTRIENT_LIST
    ]
    x, y, normalize_vector = utils.build_x_and_y(
        final_foods, constants.SAMPLE_SIZE, daily_nutrients_limit_y,
        constants.REQUIRED_NUTRIENT_LIST)
    # input_sample_size = x.shape[0]
    foods_count = x.shape[1]
    nutrients_count = x.shape[2]
    if theta_init.tolist():
        theta = theta_init
    elif constants.FIRST_RUN or food_list:
        theta = np.array([[x] * nutrients_count
                          for x in np.random.rand(foods_count)])
    else:
        theta = np.array(eval(open(constants.PREVIOUS_THETA_FILE, 'r').read()))
        previous_cost = eval(open(constants.PREVIOUS_COST_FILE, 'r').read())

    for loop in range(iterations):
        regression.gradient_descent(x, y, theta)
        print(
            str(loop) + " Normalized cost",
            regression.compute_cost(x, theta, y), "||||", "Original Cost",
            regression.compute_cost(x / normalize_vector, theta,
                                    y / normalize_vector))
        if previous_cost >= regression.compute_cost(x, theta, y):
            previous_cost = regression.compute_cost(x, theta, y)
            if loop % constants.SAVE_FREQUENCY == 0 and \
                    regression.compute_cost(x, theta, y) <= previous_cost:
                open(constants.PREVIOUS_THETA_FILE, 'w') \
                    .write(str(theta.tolist()))
                open(constants.PREVIOUS_COST_FILE, 'w') \
                    .write(str(previous_cost))
                open(constants.INPUT_FILE, 'w') \
                    .write(','.join(final_foods))
    console.display_output_normalized(x, y, theta, final_foods,
                                      constants.REQUIRED_NUTRIENT_LIST,
                                      normalize_vector)
    console.display_output(x / normalize_vector, y / normalize_vector, theta,
                           final_foods, constants.REQUIRED_NUTRIENT_LIST)
    output = utils.add_or_remove_foods(x, y, theta, final_foods)
    if output:
        food_engine(output[0], output[1], output[2])
예제 #2
0
파일: test.py 프로젝트: mbarnard09/CS540
    def test_gradient_descent(self):
        dataset = regression.get_dataset(self.BODYFAT_FILE)

        grad_desc = regression.gradient_descent(dataset,
                                                cols=[2, 3],
                                                betas=[0, 0, 0])
        numpy.testing.assert_almost_equal(
            grad_desc, np.array([-37.87698413, -1756.37222222,
                                 -7055.35138889]))
def d_dimensional_comparison(d, beta_star, num_points, sigma, l=1):
    beta_star = np.array(beta_star)

    X_list = [np.random.uniform(-20, 80, num_points) for _ in range(d)]
    X = np.column_stack(X_list)
    X = np.column_stack((np.ones(num_points), X))
    Y = np.random.normal(X.dot(beta_star), sigma)

    beta_hat_ridge = fit_ridge_regression(X, Y, l=l)
    beta_hat_grad = gradient_descent(X, Y, l=l, epsilon=1e-8, step_size=1e-2,
                                     max_steps=10000)

    print('ridge beta', beta_hat_ridge)
    print('grad beta', beta_hat_grad)
    assert loss(X, Y, beta_hat_grad) < 1.25 * loss(X, Y, beta_hat_ridge)
    print('Passed')
def d_dimensional_comparison(d, beta_star, num_points, sigma, l=1):
    beta_star = np.array(beta_star)

    X_list = [np.random.uniform(-20, 80, num_points) for _ in range(d)]
    X = np.column_stack(X_list)
    X = np.column_stack((np.ones(num_points), X))
    Y = np.random.normal(X.dot(beta_star), sigma)

    beta_hat_ridge = fit_ridge_regression(X, Y, l=l)
    beta_hat_grad = gradient_descent(X,
                                     Y,
                                     l=l,
                                     epsilon=1e-8,
                                     step_size=1e-2,
                                     max_steps=10000)

    print('ridge beta', beta_hat_ridge)
    print('grad beta', beta_hat_grad)
    assert loss(X, Y, beta_hat_grad) < 1.25 * loss(X, Y, beta_hat_ridge)
    print('Passed')
import regression
from numpy import *
import numpy as np
theta = [[np.random.rand()], [np.random.rand()]]
xArr, yArr = regression.loadDataSet('ex0.txt')
past_thetas, past_costs = regression.gradient_descent(xArr, yArr, 500, 0.01)
ws = past_thetas[-1]

xMat = mat(xArr)
yMat = mat(yArr)
yHat = xMat * ws
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xMat[:, 1].flatten().A[0], yMat.T[:, 0].flatten().A[0])
xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy * ws
ax.plot(xCopy[:, 1], yHat)
#plt.plot(past_costs)
plt.show()
yHat = xMat * ws
print(corrcoef(yHat.T, yMat))
print(ws)
plt.ylabel("Food Truck Profit in 10,000s", fontsize=14)
plt.axis([4, 24, -5, 25])

# add a column of ones to x and obtain feature matrix X
X = np.ones(shape=(len(x), 2))
X[:, 1] = x

# initialize model parameters
num_features = X.shape[1]
theta = np.zeros(num_features)

# compute the initial cost
print('The initial cost is:', reg.cost(X, y, theta))

# train model and plot fit
theta, _ = reg.gradient_descent(X, y, 0.02, 600)
predictions = X @ theta
plt.plot(X[:, 1], predictions, linewidth=2)

# train models using different learning rates and plot convergence
plt.figure()
num_iters = 1200
learning_rates = [0.01, 0.015, 0.02]
for lr in learning_rates:
    _, cost_history = reg.gradient_descent(X, y, lr, num_iters)
    plt.plot(cost_history, linewidth=2)

plt.title("Convergence plots for different learning rates")
plt.xlabel("number of iterations", fontsize=14)
plt.ylabel("cost", fontsize=14)
plt.legend(list(map(str, learning_rates)))
def d_dimensional_comparison(d, beta_star, num_points, sigma, l=1):
    beta_star = np.array(beta_star)

    X_list = [np.random.uniform(-20, 80, num_points) for _ in range(d)]
    X = np.column_stack(X_list)
    X = np.column_stack((np.ones(num_points), X))
    Y = np.random.normal(X.dot(beta_star), sigma)

    beta_hat_ridge = fit_ridge_regression(X, Y, l=l)
    beta_hat_grad = gradient_descent(X,
                                     Y,
                                     l=l,
                                     epsilon=1e-8,
                                     step_size=1e-2,
                                     max_steps=10000)

    print('ridge beta', beta_hat_ridge)
    print('grad beta', beta_hat_grad)
    assert loss(X, Y, beta_hat_grad) < 1.25 * loss(X, Y, beta_hat_ridge)
    print('Passed')


if __name__ == '__main__':
    beta5d = [15, 2.2, 3.5, 4.4, 1.1, 3.9]
    beta_est = gradient_descent(np.array([[1, 2], [1, 3], [1, 4], [1, 5]]),
                                np.array([2, 3, 4, 5.01]),
                                max_steps=2)
    assert beta_est.shape == (2, )
    d_dimensional_comparison(5, beta5d, 200, 2, l=1)
import regression as r
datasetg=r.input_data("linear_data")
z=r.gradient_descent(r.gradient_of_function,[55,2.9])
print z
r.plot_the_scatter(datasetg)
r.plot_the_fit_function([z[0],z[1]])




def loss(X, Y, beta):
    return sum((X.dot(beta) - Y) ** 2) / X.shape[0]


def d_dimensional_comparison(d, beta_star, num_points, sigma, l=1):
    beta_star = np.array(beta_star)

    X_list = [np.random.uniform(-20, 80, num_points) for _ in range(d)]
    X = np.column_stack(X_list)
    X = np.column_stack((np.ones(num_points), X))
    Y = np.random.normal(X.dot(beta_star), sigma)

    beta_hat_ridge = fit_ridge_regression(X, Y, l=l)
    beta_hat_grad = gradient_descent(X, Y, l=l, epsilon=1e-8, step_size=1e-2,
                                     max_steps=10000)

    print('ridge beta', beta_hat_ridge)
    print('grad beta', beta_hat_grad)
    assert loss(X, Y, beta_hat_grad) < 1.25 * loss(X, Y, beta_hat_ridge)
    print('Passed')

if __name__ == '__main__':
    beta5d = [15, 2.2, 3.5, 4.4, 1.1, 3.9]
    beta_est = gradient_descent(np.array([[1, 2], [1, 3], [1, 4], [1, 5]]),
                                np.array([2, 3, 4, 5.01]),
                                max_steps=2)
    assert beta_est.shape == (2,)
    d_dimensional_comparison(5, beta5d, 200, 2, l=1)