Path: blob/master/Week 4/Programming Assignment - 3/machine-learning-ex3/ex3/predictOneVsAll.m
864 views
function p = predictOneVsAll(all_theta, X)1%PREDICT Predict the label for a trained one-vs-all classifier. The labels2%are in the range 1..K, where K = size(all_theta, 1).3% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions4% for each example in the matrix X. Note that X contains the examples in5% rows. all_theta is a matrix where the i-th row is a trained logistic6% regression theta vector for the i-th class. You should set p to a vector7% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 28% for 4 examples)910m = size(X, 1);11num_labels = size(all_theta, 1);1213% You need to return the following variables correctly14p = zeros(size(X, 1), 1);1516% Add ones to the X data matrix17X = [ones(m, 1) X];1819% ====================== YOUR CODE HERE ======================20% Instructions: Complete the following code to make predictions using21% your learned logistic regression parameters (one-vs-all).22% You should set p to a vector of predictions (from 1 to23% num_labels).24%25% Hint: This code can be done all vectorized using the max function.26% In particular, the max function can also return the index of the27% max element, for more information see 'help max'. If your examples28% are in rows, then, you can use max(A, [], 2) to obtain the max29% for each row.30%31[predict indices] = max(sigmoid(all_theta * X'));32p = indices';33% =========================================================================343536end373839