Esempio n. 1
0
def compute_cost(X, y, theta):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
    """

    # initialize cost
    J = 0.0
    # get number of training examples
    m = y.shape[0]

    # Compute cost for logistic regression.
    for i in range(0, m):
        hypothesis = calculate_hypothesis(X, theta, i)[0]
        output = y[i]
        cost = 0.0

        if i > 0:
            for j in range(1, len(X[i])):
                cost += (-output * np.log(hypothesis * X[i][j])) - (
                    (1 - output) * np.log(1 - hypothesis * X[i][j]))
        if not np.math.isnan(cost):
            J += cost
    J = J / m
    return J
Esempio n. 2
0
def compute_cost(X, y, theta):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
    """

    # initialize cost
    J = 0.0
    # get number of training examples
    m = y.shape[0]

    # Compute cost for logistic regression.
    for i in range(m):
        hypothesis = calculate_hypothesis(X, theta, i)
        output = y[i]
        # cost = 0.0
        #########################################
        # You must calculate the cost
        cost = (-np.log(hypothesis) * output) - (
            (1 - output) * np.log(1 - hypothesis))
        ########################################/
        J += cost
    J = J / m

    return J
Esempio n. 3
0
def compute_cost_regularised(X, y, theta, l):
    """
    :param X        : 2D array of our dataset
    :param y        : 1D array of the groundtruth labels of the dataset
    :param theta    : 1D array of the trainable parameters
    :param l        : scalar, regularization parameter
    """

    # initialize costs
    total_squared_error = 0.0
    total_regularised_error = 0.0

    # get number of training examples
    m = y.shape[0]

    for i in range(m):
        hypothesis = calculate_hypothesis(X, theta, i)
        output = y[i]
        squared_error = (hypothesis - output)**2
        total_squared_error += squared_error

    for i in range(1, len(theta)):
        total_regularised_error += theta[i]**2

    J = (total_squared_error + l * total_regularised_error) / (2 * m)

    return J
Esempio n. 4
0
def hypothesis_to_vector(X, theta):

    hypothesis_vec = np.array([], dtype=np.float32)
    for i in range(X.shape[0]):
        hypothesis_vec = np.append(hypothesis_vec,
                                   calculate_hypothesis(X, theta, i))

    return hypothesis_vec
Esempio n. 5
0
 def ret_total_squared_error(X, y, theta):
     total_squared_error = 0.0
     m = y.shape[0]
     for i in range(m):
         hypothesis = calculate_hypothesis(X, theta, i)
         output = y[i]
         squared_error = (hypothesis - output)**2
         total_squared_error += squared_error
     return total_squared_error
Esempio n. 6
0
def gradient_descent(X, y, theta, alpha, iterations):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
    """

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        for i in range(m):
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)

            ########################################/
            output = y[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            for k in range(len(theta)):
                sigma[k] = sigma[k] + (hypothesis - output) * X[i, k]
            ########################################/

        # update theta_temp
        #########################################
        # Write your code here
        # Update theta_temp, using the values of sigma
        for param in range(len(theta)):
            theta_temp[param] = theta_temp[param] - (alpha / m) * sigma[param]
        ########################################/

        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)
    print('Gradient descent finished.')

    return theta, cost_vector
Esempio n. 7
0
def gradient_descent_training(X_train, y_train, X_test, y_test, theta, alpha,
                              iterations):
    """
        :param X_train      : 2D array of our training set
        :param y_train      : 1D array of the groundtruth labels of the training set
        :param X_test       : 2D array of our test set
        :param y_test       : 1D array of the groundtruth labels of the test set
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
    """

    m = X_train.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector_train = np.array(
        [], dtype=np.float32
    )  # empty array to store the train cost for every iteration
    cost_vector_test = np.array(
        [], dtype=np.float32
    )  # empty array to store the test cost for every iteration

    # Gradient Descent
    for it in range(iterations):
        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()
        # print(len(theta_temp))
        sigma = np.zeros((len(theta)))
        # print(sigma)
        for index in range(len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X_train, theta, i)
                output = y_train[i]
                sigma[index] = sigma[index] + (hypothesis -
                                               output) * X_train[i, index]

            theta_temp[index] = theta_temp[index] - (alpha / m) * sigma[index]
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost_train = compute_cost(X_train, y_train, theta)
        # print(iteration_cost_train)
        cost_vector_train = np.append(cost_vector_train, iteration_cost_train)
        # cost_vector_test = compute_cost(X_test, y_test, theta)
        # append current iteration's cost to cost_vector
        iteration_cost_test = compute_cost(X_test, y_test, theta)
        # print(iteration_cost_test)
        cost_vector_test = np.append(cost_vector_test, iteration_cost_test)
    print('Gradient descent finished.')

    return theta, cost_vector_train, cost_vector_test
def gradient_descent(X, y, theta, alpha, iterations):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
    """

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))

        for size in range(0, len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X, theta, i)
                output = y[i]
                if size > 1:
                    sigma[size] += (hypothesis - output) * X[i, size]
                else:
                    sigma[size] += (hypothesis - output)

            theta_temp[size] += -(alpha / m) * sigma[size]

        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)
    print('Gradient descent finished.')

    return theta, cost_vector
Esempio n. 9
0
def compute_cost(X, y, theta):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
    """
    
    # initialize cost
    J = 0.0
    # get number of training examples
    m = y.shape[0]

    for i in range(m):
        hypothesis = calculate_hypothesis(X, theta, i)
        output = y[i]
        squared_error = (hypothesis - output)**2
        J = J + squared_error
    J = J/(2*m)
    
    return J
def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent
    for it in range(iterations):
        # get temporary variables for theta's parameters
        theta_0 = theta[0]
        theta_1 = theta[1]

        # update temporary variable for theta_0
        sigma = 0.0
        for i in range(m):
            #  hypothesis = X[i, 0] * theta[0] + X[i, 1] * theta[1]
            #########################################
            # Write your code here
            # Replace the above line that calculates the hypothesis, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)

            ########################################/
            output = y[i]
            sigma = sigma + (hypothesis - output)
        theta_0 = theta_0 - (alpha / m) * sigma

        # update temporary variable for theta_1
        sigma = 0.0
        for i in range(m):

            # hypothesis = X[i,0] * theta[0] + X[i,1] * theta[1]
            #########################################
            # Write your code here
            # Replace the above line that calculates the hypothesis, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            sigma = sigma + (hypothesis - output) * X[i, 1]
        theta_1 = theta_1 - (alpha / m) * sigma

        # update theta, using the temporary variables
        theta = np.array([theta_0, theta_1])

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
do_plot = True # initially true

# call the gradient descent function to obtain the trained parameters theta_final
theta_final = gradient_descent(X_normalized, y, theta, alpha, iterations, do_plot)

#########################################
# Write your code here
# Create two new samples: (1650, 3) and (3000, 4)
print('The final theta values found:')
print(theta_final, '\n')

new_samples = [[1650,3], [3000,4]] # new ndarray

# normalize and add bias:
# Make sure to apply the same preprocessing that was applied to the training data
new_samples = (new_samples - mean_vec) / std_vec
column_of_ones = np.ones((new_samples.shape[0], 1))
new_samples = np.append(column_of_ones, new_samples, axis=1)

print('New (Normalized) Samples:')
print(new_samples, '\n')

# Calculate the hypothesis for each sample, using the trained parameters theta_final
hyp = []
for i in range(len(new_samples)):
    hyp.append(calculate_hypothesis(new_samples, theta_final, i))

# Print the predicted prices for the two samples
print('Area of 1650, 3 bedroom house price: ', hyp[0])
print('Area of 3000, 4 bedroom house price: ', hyp[1])
########################################
Esempio n. 12
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """
    
    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot==True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[0] # the number of training samples is the number of rows of array X
    cost_vector = np.array([], dtype=np.float32) # empty array to store the cost for every iteration
    
    # Gradient Descent loop
    for it in range(iterations):
        
        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()
        
        sigma = np.zeros((len(theta)))
        for i in range(m):
            #hypothesis = X[i, 0] * theta[0] + X[i, 1] * theta[1]
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            sigma +=  np.dot((hypothesis - output),X[i])
            ########################################/
        
        # update theta_temp
        #########################################
        # Write your code here
        # Update theta_temp, using the values of sigma
        theta_temp -= alpha/m * sigma
        ########################################/
        # copy theta_temp to theta
        theta = theta_temp.copy()
        
        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)
        
        # plot predictions for current iteration
        if do_plot==True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################
    
    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')
    
    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(min_cost, argmin_cost+1))
    
    return theta
def gradient_descent_training(X_train, y_train, X_test, y_test, theta, alpha,
                              iterations):
    """
        :param X_train      : 2D array of our training set
        :param y_train      : 1D array of the groundtruth labels of the training set
        :param X_test       : 2D array of our test set
        :param y_test       : 1D array of the groundtruth labels of the test set
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
    """

    m = X_train.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector_train = np.array(
        [], dtype=np.float32
    )  # empty array to store the train cost for every iteration
    cost_vector_test = np.array(
        [], dtype=np.float32
    )  # empty array to store the test cost for every iteration

    # Gradient Descent
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        sigma1 = np.zeros((len(theta)))
        for i in range(m):
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X_train, theta, i)
            ########################################/
            output = y_train[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            for k in range(len(theta)):
                sigma[k] = sigma[k] + (hypothesis - output) * X_train[i, k]

            ########################################/

        # update theta_temp

        #########################################
        # Write your code here
        # Update theta_temp, using the values of sigma
        temp = 1 - alpha * (1 / m)
        for j in range(len(theta)):
            theta_temp[j] = (theta_temp[j] * temp) - (alpha / m) * sigma[j]
            theta_temp[0] = theta_temp[0] - (alpha / m) * sigma[0]

        ########################################/

        ###############
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost vector
        #########################################
        # Write your code here
        # Store costs for both train and test set in their corresponding vectors
        iteration_cost = compute_cost(X_test, y_test, theta)
        cost_vector_test = np.append(cost_vector_test, iteration_cost)
        iteration_cost = compute_cost(X_train, y_train, theta)
        cost_vector_train = np.append(cost_vector_train, iteration_cost)

        ########################################/

    print('Gradient descent finished.')

    return theta, cost_vector_train, cost_vector_test
Esempio n. 14
0
def gradient_descent(X, y, theta, alpha, l, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and two subplots.
    # The first subplot (ax1) will be used to plot the predictions on the given 7 values of the dataset
    # The second subplot (ax2) will be used to plot the predictions on a densely sampled space, to get a more smooth curve
    fig, (ax1, ax2) = plt.subplots(1, 2)
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        for index in range(len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X, theta, i)
                output = y[i]
                sigma[index] = sigma[index] + (hypothesis - output) * X[i,
                                                                        index]

            theta_temp[index] = theta_temp[index] - (alpha / m) * sigma[index]
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        # iteration_cost = compute_cost(X, y, theta)
        # iteration_cost = compute_cost_regularised(X, y, theta, l)
        iteration_cost = compute_cost_regularised_new(X, y, theta, alpha, l)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions on the dataset's points using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # sample 1000 points, from -1.0 to +1.0
    X_sampled = np.linspace(-1.0, 1.0, 1000)
    # plot predictions on the sampled points using the final parameters
    plot_sampled_points(X, y, X_sampled, theta, ax2)

    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Esempio n. 15
0
def gradient_descent_training(X_train, y_train, X_test, y_test, theta, alpha,
                              iterations):
    """
        :param X_train      : 2D array of our training set
        :param y_train      : 1D array of the groundtruth labels of the training set
        :param X_test       : 2D array of our test set
        :param y_test       : 1D array of the groundtruth labels of the test set
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
    """

    m = X_train.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector_train = np.array(
        [], dtype=np.float32
    )  # empty array to store the train cost for every iteration
    cost_vector_test = np.array(
        [], dtype=np.float32
    )  # empty array to store the test cost for every iteration

    # Gradient Descent
    for it in range(iterations):
        theta_temp = theta.copy()
        sigma = np.zeros((len(theta)))
        for size in range(0, len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X_train, theta, i)
                output = y_train[i]
                if size > 1:
                    sigma[size] += (hypothesis - output) * X_train[i, size]
                else:
                    sigma[size] += (hypothesis - output)
            theta_temp[size] += -(alpha / m) * sigma[size]
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X_train, y_train, theta)
        cost_vector_train = np.append(cost_vector_train, iteration_cost)

    m = X_test.shape[
        0]  # the number of test samples is the number of rows of array X
    for it in range(iterations):
        theta_temp = theta.copy()
        sigma = np.zeros((len(theta)))
        for size in range(0, len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X_test, theta, i)
            output = y_test[i]
            if size > 1:
                sigma[size] += (hypothesis - output) * X_test[i, size]
            else:
                sigma[size] += (hypothesis - output)
        theta_temp[size] += -(alpha / m) * sigma[size]
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X_test, y_test, theta)
        cost_vector_test = np.append(cost_vector_test, iteration_cost)

    print('Gradient descent finished.')

    return theta, cost_vector_train, cost_vector_test
Esempio n. 16
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot, l):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
        :param l            : scalar, used to represent lambda, used for regularization
    """

    # Create just a figure and two subplots.
    # The first subplot (ax1) will be used to plot the predictions on the given 7 values of the dataset
    # The second subplot (ax2) will be used to plot the predictions on a densely sampled space, to get a more smooth curve
    fig, (ax1, ax2) = plt.subplots(1, 2)
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()

        sigma = np.zeros((len(theta)))
        for i in range(m):
            #########################################
            # Write your code here
            # Calculate the hypothesis for the i-th sample of X, with a call to the "calculate_hypothesis" function
            hypothesis = calculate_hypothesis(X, theta, i)
            ########################################/
            output = y[i]
            #########################################
            # Write your code here
            # Adapt the code, to compute the values of sigma for all the elements of theta
            for j in range(0, len(theta)):
                sigma[j] = sigma[j] + (hypothesis - output) * X[i, j]

                ########################################/

                # update theta_temp
                #########################################
                # Write your code here
                # Update theta_temp, using the values of sigma
                # Make sure to use lambda, if necessary
                if i == 0:
                    theta_temp[0] = theta_temp[0] - ((alpha / m) * sigma[0])
                else:
                    theta_temp[j] = (theta_temp[j] * (1 - (
                        (alpha * l) / m))) - (alpha / m) * sigma[j]

        ########################################/

        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        # iteration_cost = compute_cost(X, y, theta)

        iteration_cost = compute_cost_regularised(X, y, theta, l)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions on the dataset's points using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # sample 1000 points, from -1.0 to +1.0
    X_sampled = np.linspace(-1.0, 1.0, 1000)
    # plot predictions on the sampled points using the final parameters
    plot_sampled_points(X, y, X_sampled, theta, ax2)

    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Esempio n. 17
0
def gradient_descent(X, y, theta, alpha, iterations, do_plot):
    """
        :param X            : 2D array of our dataset
        :param y            : 1D array of the groundtruth labels of the dataset
        :param theta        : 1D array of the trainable parameters
        :param alpha        : scalar, learning rate
        :param iterations   : scalar, number of gradient descent iterations
        :param do_plot      : boolean, used to plot groundtruth & prediction values during the gradient descent iterations
    """

    # Create just a figure and only one subplot
    fig, ax1 = plt.subplots()
    if do_plot == True:
        plot_hypothesis(X, y, theta, ax1)

    m = X.shape[
        0]  # the number of training samples is the number of rows of array X
    cost_vector = np.array(
        [],
        dtype=np.float32)  # empty array to store the cost for every iteration

    # Gradient Descent loop
    for it in range(iterations):

        # initialize temporary theta, as a copy of the existing theta array
        theta_temp = theta.copy()
        # print(len(theta_temp))
        sigma = np.zeros((len(theta)))
        # print(sigma)
        for index in range(len(theta_temp)):
            for i in range(m):
                hypothesis = calculate_hypothesis(X, theta, i)
                output = y[i]
                sigma[index] = sigma[index] + (hypothesis - output) * X[i,
                                                                        index]

            theta_temp[index] = theta_temp[index] - (alpha / m) * sigma[index]
        # copy theta_temp to theta
        theta = theta_temp.copy()

        # append current iteration's cost to cost_vector
        iteration_cost = compute_cost(X, y, theta)
        cost_vector = np.append(cost_vector, iteration_cost)

        # plot predictions for current iteration
        if do_plot == True:
            plot_hypothesis(X, y, theta, ax1)
    ####################################################

    # plot predictions using the final parameters
    plot_hypothesis(X, y, theta, ax1)
    # save the predictions as a figure
    plot_filename = os.path.join(os.getcwd(), 'figures', 'predictions.png')
    plt.savefig(plot_filename)
    print('Gradient descent finished.')

    # Plot the cost for all iterations
    plot_cost(cost_vector)
    min_cost = np.min(cost_vector)
    argmin_cost = np.argmin(cost_vector)
    print('Minimum cost: {:.5f}, on iteration #{}'.format(
        min_cost, argmin_cost + 1))

    return theta
Esempio n. 18
0
# initialise trainable parameters theta, set learning rate alpha and number of iterations
theta = np.zeros((3))
alpha = 1
iterations = 100

# plot predictions for every iteration?
do_plot = True

# call the gradient descent function to obtain the trained parameters theta_final
theta_final = gradient_descent(X_normalized, y, theta, alpha, iterations,
                               do_plot)
print('Final Theta: {}'.format(theta_final))
#########################################
# Write your code here
# Create two new samples: (1650, 3) and (3000, 4)
# Calculate the hypothesis for each sample, using the trained parameters theta_final
# Make sure to apply the same preprocessing that was applied to the training data
# Print the predicted prices for the two samples
new_samples = np.array([[1650, 3], [3000, 4]], dtype=np.float64)
normalized_samples = (new_samples - mean_vec) / std_vec
column_of_ones = np.ones((normalized_samples.shape[0], 1))
normalized_samples = np.append(column_of_ones, normalized_samples, axis=1)

for i in range(normalized_samples.shape[0]):
    print('Predicted price: {} for Sqft {} and {} bedrooms'.format(
        (calculate_hypothesis(normalized_samples, theta_final, i)),
        new_samples[i, 0], new_samples[i, 1]))

########################################/
alpha = 0.1
iterations = 100

# plot predictions for every iteration?
do_plot = True

# call the gradient descent function to obtain the trained parameters theta_final
theta_final = gradient_descent(X_normalized, y, theta, alpha, iterations,
                               do_plot)
#########################################
# Write your code here

# Create two new samples: (1650, 3) and (3000, 4)
# X_sample = load_samples()
X_sample = [[1650, 3], [3000, 4]]
#normalize the new data
X_sample_normalized = (X_sample - mean_vec) / std_vec
new_column_of_ones = np.ones((X_sample_normalized.shape[0], 1))
X_sample_normalized = np.append(new_column_of_ones,
                                X_sample_normalized,
                                axis=1)
# Calculate the hypothesis for each sample, using the trained parameters theta_final
for i in range(2):
    sample_hypothesis = calculate_hypothesis(X_sample_normalized, theta_final,
                                             i)
    print("# {}: {}".format(i, sample_hypothesis))
# Make sure to apply the same preprocessing that was applied to the training data
# Print the predicted prices for the two samples

########################################/