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.

emailFeatures.m 2.2 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function x = emailFeatures(word_indices)
  2. %EMAILFEATURES takes in a word_indices vector and produces a feature vector
  3. %from the word indices
  4. % x = EMAILFEATURES(word_indices) takes in a word_indices vector and
  5. % produces a feature vector from the word indices.
  6. % Total number of words in the dictionary
  7. n = 1899;
  8. % You need to return the following variables correctly.
  9. x = zeros(n, 1);
  10. % ====================== YOUR CODE HERE ======================
  11. % Instructions: Fill in this function to return a feature vector for the
  12. % given email (word_indices). To help make it easier to
  13. % process the emails, we have have already pre-processed each
  14. % email and converted each word in the email into an index in
  15. % a fixed dictionary (of 1899 words). The variable
  16. % word_indices contains the list of indices of the words
  17. % which occur in one email.
  18. %
  19. % Concretely, if an email has the text:
  20. %
  21. % The quick brown fox jumped over the lazy dog.
  22. %
  23. % Then, the word_indices vector for this text might look
  24. % like:
  25. %
  26. % 60 100 33 44 10 53 60 58 5
  27. %
  28. % where, we have mapped each word onto a number, for example:
  29. %
  30. % the -- 60
  31. % quick -- 100
  32. % ...
  33. %
  34. % (note: the above numbers are just an example and are not the
  35. % actual mappings).
  36. %
  37. % Your task is take one such word_indices vector and construct
  38. % a binary feature vector that indicates whether a particular
  39. % word occurs in the email. That is, x(i) = 1 when word i
  40. % is present in the email. Concretely, if the word 'the' (say,
  41. % index 60) appears in the email, then x(60) = 1. The feature
  42. % vector should look like:
  43. %
  44. % x = [ 0 0 0 0 1 0 0 0 ... 0 0 0 0 1 ... 0 0 0 1 0 ..];
  45. %
  46. %
  47. for i = 1 : length(word_indices)
  48. x(word_indices(i)) = 1;
  49. end
  50. % =========================================================================
  51. end

机器学习

Contributors (1)