Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- classdef mynb
- methods(Static)
- % estimates the shape of the distribution of each feature within each class
- function m = fit(train_examples, train_labels)
- % unique() function used on train_labels to find each different
- % possible class label
- m.unique_classes = unique(train_labels);
- % length() function works out number of classes
- m.n_classes = length(m.unique_classes);
- m.means = {};
- m.stds = {};
- % loops over all the values in unique_classes
- for i = 1:m.n_classes
- this_class = m.unique_classes(i);
- % retrieves all rows in train_examples where corresponding element
- % of train_labels is equal to value stored inside this_class variable
- examples_from_this_class = train_examples{train_labels==this_class,:};
- % loop writes each new array into its own element of a cell
- % array within the model structure
- m.means{end+1} = mean(examples_from_this_class);
- m.stds{end+1} = std(examples_from_this_class);
- end
- % a prior is how likely each class label is to occur,
- % based on how many times it is present in the training data
- m.priors = [];
- % loops over all the values in unique_classes again to calculate prior
- for i = 1:m.n_classes
- this_class = m.unique_classes(i);
- % retrieves all rows in train_examples where corresponding element
- % of train_labels is equal to value stored inside this_class variable
- examples_from_this_class = train_examples{train_labels==this_class,:};
- % calculates number of examples from current class as fraction of
- % total number of training examples to approximate the likelihood that
- % arandomly chosen iris will belong to this class
- m.priors(end+1) = size(examples_from_this_class,1) / size(train_labels,1);
- end
- end
- function predictions = predict(m, test_examples)
- predictions = categorical;
- % loops through all test examples row by row using size() function
- for i=1:size(test_examples,1)
- fprintf('classifying example %i/%i\n', i, size(test_examples,1));
- this_test_example = test_examples{i,:};
- % predict_one() function called for each test example
- this_prediction = mynb.predict_one(m, this_test_example);
- % prediction stored in categorical array
- predictions(end+1) = this_prediction;
- end
- end
- % calculates likelihood for current test example given the class
- % by calling calculate_likelihood() function
- function prediction = predict_one(m, this_test_example)
- for i=1:m.n_classes
- % computes value proportional to probability of current class label,
- % given the test example
- this_likelihood = mynb.calculate_likelihood(m, this_test_example, i);
- % uses get_prior() function to retrieve current class'
- % prior
- this_prior = mynb.get_prior(m, i);
- % multiply new likelihood with current class' prior
- % posterior proportional to the true posterior probabilities
- posterior_(i) = this_likelihood * this_prior;
- end
- % index of array element containing maximum value tells us which
- % element of unique_classes to look in for name of predicted class label
- [winning_value_, winning_index] = max(posterior_);
- % takes most likely and applies its class label
- prediction = m.unique_classes(winning_index);
- end
- % works by considering every feature's value in current test example, in complete isolation
- function likelihood = calculate_likelihood(m, this_test_example, class)
- likelihood = 1;
- % loop calculates densities for every feature value using
- % calculate_pd() function
- for i=1:length(this_test_example)
- likelihood = likelihood * mynb.calculate_pd(this_test_example(i), m.means{class}(i), m.stds{class}(i));
- end
- end
- % fetches prior for current class
- function prior = get_prior(m, class)
- prior = m.priors(class);
- end
- % function for calculating a probability density at a given value
- % given a Normal distribution with a particular mean and standard deviation
- % (normpdf two-part rebuild)
- function pd = calculate_pd(x, mu, sigma)
- first_bit = 1 / sqrt(2*pi*sigma^2);
- second_bit = - ( ((x-mu)^2) / (2*sigma^2) );
- pd = first_bit * exp(second_bit);
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement