朴素贝叶斯(Naive Bayes)是一种基于贝叶斯定理的分类算法,它假设特征之间相互独立,从而简化了计算复杂性。该算法常用于文本分类、垃圾邮件过滤、情感分析等应用场景。
MATLAB实现鸢尾花数据集分类代码如下:
clear
load fisheriris
X = meas(:,1:2);
Y = species;
labels = unique(Y);
figure;
gscatter(X(:,1), X(:,2), species,'rgb','osd');
xlabel('Sepal length');
ylabel('Sepal width');
mdl = fitcnb(X,Y);
[xx1, xx2] = meshgrid(4:.01:8,2:.01:4.5);
XGrid = [xx1(:) xx2(:)];
[predictedspecies,Posterior,~] = predict(mdl,XGrid);
sz = size(xx1);
s = max(Posterior,[],2);
figure
hold on
surf(xx1,xx2,reshape(Posterior(:,1),sz),'EdgeColor','none')
surf(xx1,xx2,reshape(Posterior(:,2),sz),'EdgeColor','none')
surf(xx1,xx2,reshape(Posterior(:,3),sz),'EdgeColor','none')
xlab