function [predictive, posterior] = predict_nbc(test, priors, likelihood)
%PREDICT_NBC uses a naive bayes classifier to predict the class labels of
%the test data set.
%% [predictive, posterior] = predict_nbc(test, priors, likelihood)
%% Input:
% test - a struct representing the test data set
% test.class - the class of each data
% test.features - the feature of each data
% priors - a struct representing the priors of each class
% priors.class - the class labels
% priors.value - the priors of its corresponding classes
% likelihood - a struct representing the likelihood
% likelihood.matrixColnames - the feature values
% likelihood.matrixRownames - the class labels
% likelihood.matrix - the likelihood values
%% Output:
% predictive - the predictive results of the test data set
% predictive.class - the predictive class for each data
% posterior - a struct representing the posteriors of each class
% posterior.class - the class labels
% posterior.value - the posteriora of the corresponding classes
%% Running these code to get some examples:
%nbc_mushroom
%% Edited by X. Sun
% My homepage: http://pamixsun.github.io/
%%
% Check the input arguments
if nargin < 3
error(message('MATLAB:UNIQUE:NotEnoughInputs'));
end
posterior.class = priors.class;
% Calculate posteriors for each test data record
predictive.class = zeros(length(size(test.features, 1)), 1);
posterior.value = zeros(size(test.features, 1), length(priors.class));
for i = 1 : size(test.features, 1)
record = test.features(i, :);
% Calculate posteriors for each possible class of that record
for j = 1 : length(priors.class)
class = priors.class(j);
% Initialize posterior as the prior value of that class
posteriorValue = priors.value(priors.class == class);
for k = 1 : length(record)
item = record(k);
likelihoodValue = ...
likelihood.matrix{k}(j, likelihood.matrixColnames{k}(:) == item);
posteriorValue = posteriorValue * likelihoodValue;
end
% Calculate the posteriors
posterior.value(i, j) = posteriorValue;
end
% Get the predictive class
predictive.class(i) = ...
posterior.class(posterior.value(i, :) == max(posterior.value(i, :)));
end
predictive.class = char(predictive.class);
predictive.class = predictive.class(:);
end
云端暮雪
- 粉丝: 177
- 资源: 3
最新资源
- 垂直输送机-机械3D图Solidworks设计图.zip
- 第9章 聚类分析.zip
- 【目标检测数据集】飞机数据集7930张VOC+YOLO格式.zip
- 恐怖场景模型 :Horror - modular interior and props v2.0
- Java安装.zipJava安装.zip
- Bongo Cat 桌面宠物
- 3_9月6日 上午11点27分.m4a..mp3
- ubuntu20.04安装教程-ubuntu20.04图文安装教程.pdf
- 欧姆龙CP1H与3台三菱E700变频器通讯程序 功能:程序,欧姆龙CP1H的CIF11通讯板,实现对3台三菱e700变频器 设定
- pycharm怎么配置python环境.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页