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
![avatar](https://profile-avatar.csdnimg.cn/36e9d0017d8740a7b012d269f38e5402_longyindiyi.jpg!1)
云端暮雪
- 粉丝: 176
- 资源: 3
最新资源
- Ubuntu 22.04.5 LTS 安装企业微信,(2025-02-17安装可行)
- 深入解读R语言实现的传染病传播模型源码:自由增长模型、SI、SIS与SIR模型原理及北京新冠数据预测应用,基于R语言的新冠传染病传播模型解读及实践:自由增长模型、SI模型、SIS模型、SIR模型之探讨
- 基于Matlab的LQR算法在车辆轨迹跟踪中的应用研究,基于Matlab的LQR算法在车辆轨迹跟踪中的应用研究,基于Matlab使用LQR实现车辆轨迹跟踪… ,基于Matlab; LQR; 车辆轨迹跟
- 糖尿病患者住院信息.zip
- Spring容器创建对象的三种方式:
- 电力系统有功无功协同优化模型:结合常规调度与新能源优化,借助Matlab+Yalmip+Cplex软件高效求解混合整数二阶锥规划问题,电力系统有功无功协同优化:MATLAB结合Yalmip与Cplex
- 西门子S7-200 Smart PLC与台达伺服电机协同控制:触摸屏驱动包装机夹袋至缝包机运动控制方案,西门子S7-200 Smart PLC与台达伺服电机协同控制:触摸屏驱动包装机夹袋至缝包机运动控
- 高效的六电池均衡管理系统,采用精准Buckboost电路,实现快速均衡与高精度充电,6电池精准均衡,高速BuckBoost电路设计,实现卓越性能与效率,6个电池均衡,buckboost电路,精度高,均
- Python-应用案例实现-人机猜拳游戏
- QQ交流频道(解压后查看).zip
- 感应电机异步电机模型预测磁链控制(MPFC)系统:优化定子磁链与成本函数的最小化输出策略,感应电机异步电机模型预测磁链控制(MPFC)系统:优化电压矢量以精准控制定子磁链,感应电机 异步电机模型预测磁
- 6.python-numpy2024-09-29.wmv
- 基于深度学习的复杂多变量预测:利用卷积神经网络与双向门控循环单元结合KDE实现精确区间概率预测-Matlab 2020及以上版本算法详解,基于CNN-BiGRU-KDE的区间预测模型:多变量单输出预
- C#使用NModbus4库创建Modbus TCP Slave(服务器)以及客户端简单通讯示例
- 锁相环(PLL)设计与进阶应用技术:理论与实践的完美结合,锁相环(PLL)设计与进阶探索:原理、方法及实际应用的深入剖析,锁相环PLL pll设计与进阶 ,核心关键词:锁相环(PLL); PLL设计与
- GPU加速AES算法的密码学应用:多工作流与T表性能测试实践,利用GPU加速AES算法的密码学实践:从源码到编译执行全流程解析,X00139-密码学利用gpu加速aes算法 源码文件: 测试gpu:
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
- 1
- 2
- 3
- 4
- 5
- 6
前往页