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
云端暮雪
- 粉丝: 176
- 资源: 3
最新资源
- 章节1:Python入门视频
- 无需样板的 Python 类.zip
- ESP32 : 32-bit MCU & 2.4 GHz Wi-Fi & BT/BLE SoCs
- 博物馆文博资源库-JAVA-基于springBoot博物馆文博资源库系统设计与实现
- 旅游网站-JAVA-springboot+vue的桂林旅游网站系统设计与实现
- 小说网站-JAVA-基于springBoot“西贝”小说网站的设计与实现
- 游戏分享网站-JAVA-基于springBoot“腾达”游戏分享网站的设计与实现
- 学习交流-JAVA-基于springBoot“非学勿扰”学习交流平台设计与实现
- EDAfloorplanning
- 所有课程均提供 Python 复习部分.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页