5

How do you classify a row of seperate cells in MATLAB?

At the moment I can classify single coloums like so:

training = [1;0;-1;-2;4;0;1]; % this is the sample data.
target_class = ['posi';'zero';'negi';'negi';'posi';'zero';'posi'];
% target_class are the different target classes for the training data; here 'positive' and 'negetive' are the two classes for the given training data

% Training and Testing the classifier (between positive and negative)
test = 10*randn(25, 1); % this is for testing. I am generating random numbers.
class  = classify(test,training, target_class, 'diaglinear')  % This command classifies the test data depening on the given training data using a Naive Bayes classifier

Unlike the above, I want to classify:

        A   B   C
Row A | 1 | 1 | 1 = a house

Row B | 1 | 2 | 1 = a garden

Here's a code example from the MATLAB site:

nb = NaiveBayes.fit(training, class)
nb = NaiveBayes.fit(..., 'param1', val1, 'param2', val2, ...)

I don't understand what param1, val1, etc. are. Can anyone help?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
G Gr
  • 6,030
  • 20
  • 91
  • 184

1 Answers1

3

Here is an example adapted from the docs:

%# load data, and shuffle instances order
load fisheriris
ord = randperm(size(meas,1));
meas = meas(ord,:);
species = species(ord);

%# lets split into training/testing
training = meas(1:100,:);         %# 100 rows, each 4 features
testing = meas(101:150,:);        %# 50 rows
train_class = species(1:100);     %# three possible classes
test_class = species(101:150);

%# train model
nb = NaiveBayes.fit(training, train_class);

%# prediction
y = nb.predict(testing);

%# confusion matrix
confusionmat(test_class,y)

the output in this case was 2 misclassified instances:

ans =
    15     0     1
     0    20     0
     1     0    13

Now you can customize all sorts of options for the classifier (param/value you mention), just refer the documentation for a description of each..

For example it allows you to choose from a gaussian or non-parametric kernel distribution to model the features. Also you could specify the prior probabilities of the classes, should it be estimated from the training instances or do you assume equal probabilities.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Hi amro what is test_class in this instance? – G Gr Jul 19 '12 at 04:45
  • 1
    @JungleBoogie: it is the true class labels of the test set. We use it to get an unbiased measure of performance (we train a model on one set, and test it on a completely different set) – Amro Jul 19 '12 at 11:38
  • Ah I understand now. I get a few errors however trying to use your method but I managed to use my own method (not sure on the difference) you can see the implementation [here](http://stackoverflow.com/questions/11566964/clustering-and-bayes-classifiers-matlab). – G Gr Jul 19 '12 at 18:54
  • @JungleBoogie: as far as the above is concerned, the solution is good with no errors (and answers the question as asked). Your other question is using discriminant analysis with a diagonal covariance matrix. Although considered equivalent to naive Bayes, the two algorithms are completely different... – Amro Jul 20 '12 at 15:20