Stanford Machine Learning – Coursera – Flashcards

Unlock all answers in this set

Unlock answers
question
Linear Regression with One Variable Cost Function We can measure the accuracy of our hypothesis function by using a cost function. This takes an average difference of all the results of the hypothesis with inputs from x's and the actual output y's. J(θ₀,θ₁)=½m∑=(hθ(xi)−yi)²
answer
function J = computeCost(X, y, theta) % J = COMPUTECOST(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 urn the following variables correctly J = 0; prediction = X*theta; sqrErrors = (prediction - y).^2; J = 1/(2*m) * sum(sqrErrors); end
question
Multivar Linear Regression Cost Function We can measure the accuracy of our hypothesis function by using a cost function. This takes an average difference of all the results of the hypothesis with inputs from x's and the actual output y's. J(θ₀,θ₁,θ₂,θ₃, ... )=½m∑=(hθ(xi)−yi)²
answer
function J = computeCostMulti(X, y, theta) % Compute cost for linear regression with multiple variables J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the parameter for linear regression to fit the data points in X and y m = length(y); % number of training examples J = 0; J = sum((X*theta - y).^2)/(2*m); end
question
Feature Normalization x' =
answer
function [X_norm, mu, sigma] = featureNormalize(X) %FEATURENORMALIZE Normalizes the features in X FEATURENORMALIZE(X) returns a normalized version of X where the mean value of each feature is 0 and the standard deviation is 1. This is often a good preprocessing step to do when working with learning algorithms. X_norm = X; mu = zeros(1, size(X, 2)); sigma = zeros(1, size(X, 2));
question
What is Machine Learning?
answer
Two definitions of Machine Learning are offered. Arthur Samuel described it as: "the field of study that gives computers the ability to learn without being explicitly programmed." This is an older, informal definition. Tom Mitchell provides a more modern definition: "A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E." In general, any machine learning problem can be assigned to one of two broad classifications: Supervised learning and Unsupervised learning.
question
Supervised Learning
answer
In supervised learning, we are given a data set and already know what our correct output should look like, having the idea that there is a relationship between the input and the output. Supervised learning problems are categorized into "regression" and "classification" problems. In a regression problem, we are trying to predict results within a continuous output, meaning that we are trying to map input variables to some continuous function. In a classification problem, we are instead trying to predict results in a discrete output. In other words, we are trying to map input variables into discrete categories.
question
Supervised learning problems are categorized into "regression" and "classification" problems⊆
answer
Example 1: Given data about the size of houses on the real estate market, try to predict their price. Price as a function of size is a continuous output, so this is a regression problem. We could turn this example into a classification problem by instead making our output about whether the house "sells for more or less than the asking price." Here we are classifying the houses based on price into two discrete categories.
question
Supervised learning problems are categorized into "regression" and "classification" problems
answer
Example 2: (a) Regression - Given a picture of a person, we have to predict their age on the basis of the given picture (b) Classification - Given a patient with a tumor, we have to predict whether the tumor is malignant or benign.
question
Unsupervised Learning
answer
Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don't necessarily know the effect of the variables. We can derive this structure by clustering the data based on relationships among the variables in the data. With unsupervised learning there is no feedback based on the prediction results. Example: Clustering: Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on. Non-clustering: The "Cocktail Party Algorithm", allows you to find structure in a chaotic environment. (i.e. identifying individual voices and music from a mesh of sounds at a cocktail party).
question
Cost Function - Intuition I
answer
If we try to think of it in visual terms, our training data set is scattered on the x-y plane. We are trying to make a straight line (defined by hθ(x)) which passes through these scattered data points. Our objective is to get the best possible line. The best possible line will be such so that the average squared vertical distances of the scattered points from the line will be the least. Ideally, the line should pass through all the points of our training data set. In such a case, the value of J(θ₀,θ₁) will be 0. The following example shows the ideal situation where we have a cost function of 0. When θ1=1, we get a slope of 1 which goes through every single data point in our model. Conversely, when θ1=0.5, we see the vertical distance from our fit to the data points increase. This increases our cost function to 0.58. Plotting several other points yields to the following graph: Thus as a goal, we should try to minimize the cost function. In this case, θ₁=1 is our global minimum.
question
Normal Equation θ=(X' X)⁻¹ X' y
answer
function [theta] = normalEqn(X, y) %NORMALEQN Computes the closed-form solution to linear regression NORMALEQN(X,y) computes the closed-form solution to linear regression using the normal equations. theta = zeros(size(X, 2), 1); theta = pinv(X'*X)*X'*y;
question
Gradient Descent One Variable
answer
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) GRADIENTDESCENT Performs gradient descent to learn theta m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters x = X(:,2); h = theta(1) + (theta(2)*x); theta_zero = theta(1) - alpha * (1/m) * sum(h-y); theta_one = theta(2) - alpha * (1/m) * sum((h - y) .* x); theta = [theta_zero; theta_one]; J_history(iter) = computeCost(X, y, theta); end disp(min(J_history)); end
question
Gradient Descent Multi Variable
answer
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters) GRADIENTDESCENTMULTI Performs gradient descent to learn theta m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters gradJ = 1/(2*m) * 2 * (X'*X*theta - X'*y); theta = theta - alpha * gradJ; J_history(iter) = computeCostMulti(X, y, theta); end end
Get an explanation on any task
Get unstuck with the help of our AI assistant in seconds
New