Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 4/Programming Assignment - 3/machine-learning-ex3/ex3/predictOneVsAll.m
864 views
1
function p = predictOneVsAll(all_theta, X)
2
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
3
%are in the range 1..K, where K = size(all_theta, 1).
4
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
5
% for each example in the matrix X. Note that X contains the examples in
6
% rows. all_theta is a matrix where the i-th row is a trained logistic
7
% regression theta vector for the i-th class. You should set p to a vector
8
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
9
% for 4 examples)
10
11
m = size(X, 1);
12
num_labels = size(all_theta, 1);
13
14
% You need to return the following variables correctly
15
p = zeros(size(X, 1), 1);
16
17
% Add ones to the X data matrix
18
X = [ones(m, 1) X];
19
20
% ====================== YOUR CODE HERE ======================
21
% Instructions: Complete the following code to make predictions using
22
% your learned logistic regression parameters (one-vs-all).
23
% You should set p to a vector of predictions (from 1 to
24
% num_labels).
25
%
26
% Hint: This code can be done all vectorized using the max function.
27
% In particular, the max function can also return the index of the
28
% max element, for more information see 'help max'. If your examples
29
% are in rows, then, you can use max(A, [], 2) to obtain the max
30
% for each row.
31
%
32
[predict indices] = max(sigmoid(all_theta * X'));
33
p = indices';
34
% =========================================================================
35
36
37
end
38
39