You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

predictOneVsAll.m 1.5 kB

8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. m = size(X, 1);
  11. num_labels = size(all_theta, 1);
  12. % You need to return the following variables correctly
  13. p = zeros(size(X, 1), 1);
  14. % Add ones to the X data matrix
  15. X = [ones(m, 1) X];
  16. % ====================== YOUR CODE HERE ======================
  17. % Instructions: Complete the following code to make predictions using
  18. % your learned logistic regression parameters (one-vs-all).
  19. % You should set p to a vector of predictions (from 1 to
  20. % num_labels).
  21. %
  22. % Hint: This code can be done all vectorized using the max function.
  23. % In particular, the max function can also return the index of the
  24. % max element, for more information see 'help max'. If your examples
  25. % are in rows, then, you can use max(A, [], 2) to obtain the max
  26. % for each row.
  27. %
  28. p = X * all_theta';
  29. [temp_p,p] = max(p, [], 2);
  30. %p(find(p == 10)) = 0;
  31. % =========================================================================
  32. end

机器学习

Contributors (1)