Advertisement
Guest User

np np

a guest
Dec 9th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. classdef mynb
  2. methods(Static)
  3. % estimates the shape of the distribution of each feature within each class
  4. function m = fit(train_examples, train_labels)
  5. % unique() function used on train_labels to find each different
  6. % possible class label
  7. m.unique_classes = unique(train_labels);
  8. % length() function works out number of classes
  9. m.n_classes = length(m.unique_classes);
  10.  
  11. m.means = {};
  12. m.stds = {};
  13. % loops over all the values in unique_classes
  14. for i = 1:m.n_classes
  15.  
  16. this_class = m.unique_classes(i);
  17. % retrieves all rows in train_examples where corresponding element
  18. % of train_labels is equal to value stored inside this_class variable
  19. examples_from_this_class = train_examples{train_labels==this_class,:};
  20. % loop writes each new array into its own element of a cell
  21. % array within the model structure
  22. m.means{end+1} = mean(examples_from_this_class);
  23. m.stds{end+1} = std(examples_from_this_class);
  24.  
  25. end
  26. % a prior is how likely each class label is to occur,
  27. % based on how many times it is present in the training data
  28. m.priors = [];
  29. % loops over all the values in unique_classes again to calculate prior
  30. for i = 1:m.n_classes
  31.  
  32. this_class = m.unique_classes(i);
  33. % retrieves all rows in train_examples where corresponding element
  34. % of train_labels is equal to value stored inside this_class variable
  35. examples_from_this_class = train_examples{train_labels==this_class,:};
  36. % calculates number of examples from current class as fraction of
  37. % total number of training examples to approximate the likelihood that
  38. % arandomly chosen iris will belong to this class
  39. m.priors(end+1) = size(examples_from_this_class,1) / size(train_labels,1);
  40.  
  41. end
  42.  
  43. end
  44.  
  45. function predictions = predict(m, test_examples)
  46.  
  47. predictions = categorical;
  48. % loops through all test examples row by row using size() function
  49. for i=1:size(test_examples,1)
  50.  
  51. fprintf('classifying example %i/%i\n', i, size(test_examples,1));
  52. this_test_example = test_examples{i,:};
  53. % predict_one() function called for each test example
  54. this_prediction = mynb.predict_one(m, this_test_example);
  55. % prediction stored in categorical array
  56. predictions(end+1) = this_prediction;
  57.  
  58. end
  59. end
  60. % calculates likelihood for current test example given the class
  61. % by calling calculate_likelihood() function
  62. function prediction = predict_one(m, this_test_example)
  63.  
  64. for i=1:m.n_classes
  65. % computes value proportional to probability of current class label,
  66. % given the test example
  67. this_likelihood = mynb.calculate_likelihood(m, this_test_example, i);
  68. % uses get_prior() function to retrieve current class'
  69. % prior
  70. this_prior = mynb.get_prior(m, i);
  71. % multiply new likelihood with current class' prior
  72. % posterior proportional to the true posterior probabilities
  73. posterior_(i) = this_likelihood * this_prior;
  74.  
  75. end
  76. % index of array element containing maximum value tells us which
  77. % element of unique_classes to look in for name of predicted class label
  78. [winning_value_, winning_index] = max(posterior_);
  79. % takes most likely and applies its class label
  80. prediction = m.unique_classes(winning_index);
  81.  
  82. end
  83. % works by considering every feature's value in current test example, in complete isolation
  84. function likelihood = calculate_likelihood(m, this_test_example, class)
  85.  
  86. likelihood = 1;
  87. % loop calculates densities for every feature value using
  88. % calculate_pd() function
  89. for i=1:length(this_test_example)
  90. likelihood = likelihood * mynb.calculate_pd(this_test_example(i), m.means{class}(i), m.stds{class}(i));
  91. end
  92. end
  93. % fetches prior for current class
  94. function prior = get_prior(m, class)
  95.  
  96. prior = m.priors(class);
  97.  
  98. end
  99. % function for calculating a probability density at a given value
  100. % given a Normal distribution with a particular mean and standard deviation
  101. % (normpdf two-part rebuild)
  102. function pd = calculate_pd(x, mu, sigma)
  103.  
  104. first_bit = 1 / sqrt(2*pi*sigma^2);
  105. second_bit = - ( ((x-mu)^2) / (2*sigma^2) );
  106. pd = first_bit * exp(second_bit);
  107.  
  108. end
  109.  
  110. end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement