朴素贝叶斯行分类

5

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

目前我可以这样分类单个列:

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网站的代码示例:

Code example:

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

我不理解 param1, val1等代表什么。有人能帮忙吗?
1个回答

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

现在,您可以自定义分类器的各种选项(您提到的参数/值),只需参考文档以了解每个选项的描述。
例如,它允许您选择高斯或非参数核分布来模拟特征。此外,您还可以指定类别的先验概率,是从训练实例中估计还是假设等概率。

嗨Amro,在这个实例中,test_class是什么? - G Gr
1
@JungleBoogie:它是测试集的真实类标签。我们使用它来获得一个无偏的性能度量(我们在一个数据集上训练模型,然后在完全不同的数据集上进行测试)。 - Amro
啊,我现在明白了。尝试使用你的方法时出现了一些错误,但是我设法使用了自己的方法(不确定有什么区别),你可以在这里看到实现:https://dev59.com/2mbWa4cB1Zd3GeqPVVDT。 - G Gr
@JungleBoogie: 就以上内容而言,该解决方案无误(并且回答了所提出的问题)。你的另一个问题是使用带有对角协方差矩阵的判别分析。虽然被认为等同于朴素贝叶斯,但这两种算法完全不同... - Amro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接