朴素贝叶斯行分类

时间:2012-07-01 02:01:56

标签: statistics machine-learning classification matlab

如何在MATLAB中对一行单独的单元格进行分类?

目前我可以像这样对单个coloums进行分类:

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

与上述不同,我想分类:

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

Row B | 1 | 2 | 1 = a garden

以下是MATLAB网站的代码示例:

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

我不明白param1val1等是什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

以下是改编自文档的示例:

%# 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)

这种情况下的输出是2个错误分类的实例:

ans =
    15     0     1
     0    20     0
     1     0    13

现在您可以为分类器自定义各种选项(您提到的参数/值),只需参考documentation获取每个选项的说明..

例如,它允许您从高斯或非参数内核分布中进行选择以对特征进行建模。您还可以指定类的先验概率,是否应该从训练实例中估计,或者您是否假设等概率。