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
最新资源
- 毕设和企业适用springboot企业内部数据分析平台类及机器学习平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及全生命周期管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及汽车信息管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及视频会议系统源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及停车管理系统源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及数据存储平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及信息安全管理系统源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及文化创意平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及虚拟银行平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及用户反馈平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及医疗诊断系统源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及用户体验优化平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及运动赛事管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及职业技能培训平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及智慧社区管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业安全管理系统类及资产管理平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页