Created
April 16, 2020 21:16
-
-
Save simonespa/4a8eb9ea6a07ae2864df4da4b220ed53 to your computer and use it in GitHub Desktop.
Linear Regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function J = costFunction(X, y, theta) | |
| % Compute cost for linear regression | |
| % J = costFunction(X, y, theta) computes the cost of using theta as the | |
| % parameter for linear regression to fit the data points in X and y | |
| % Initialize some useful values | |
| m = length(y); % number of training examples | |
| % Compute the cost | |
| J = sum((X * theta - y).^2) * 1/(2*m); | |
| % ========================================================================= | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function [theta, J_history] = gradientDescent(X, y, theta, alpha, iterations) | |
| %GRADIENTDESCENT Performs gradient descent to learn theta | |
| % theta = GRADIENTDESCENT(X, y, theta, alpha, iterations) updates theta by | |
| % taking iterations gradient steps with learning rate alpha | |
| % Initialize some useful values | |
| m = length(y); % number of training examples | |
| J_history = zeros(iterations, 1); | |
| for iter = 1:iterations | |
| % ====================== YOUR CODE HERE ====================== | |
| % Instructions: Perform a single gradient step on the parameter vector | |
| % theta. | |
| % | |
| % Hint: While debugging, it can be useful to print out the values | |
| % of the cost function (computeCost) and gradient here. | |
| % | |
| error = (X * theta - y); % h(x) - y [97x1] | |
| delta1 = sum(error .* X(:,1)); | |
| delta2 = sum(error .* X(:,2)); | |
| theta(1) = theta(1) - alpha / m * delta1; | |
| theta(2) = theta(2) - alpha / m * delta2; | |
| % ============================================================ | |
| % Save the cost J in every iteration | |
| J_history(iter) = computeCost(X, y, theta); | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment