之所以用matlab实现,是因为这是数据挖掘课的几个大作业之一,作业要求,不然也不会这么蛋疼用matlab....(因为我不会matlab...)
朴素贝叶斯原理非常简单,最重要的就是概率公式:
其余的内容介绍可以参考:http://zh.wikipedia.org/wiki/%E6%9C%B4%E7%B4%A0%E8%B4%9D%E5%8F%B6%E6%96%AF%E5%88%86%E7%B1%BB%E5%99%A8
下面贴用matlab的具体实现算法:
补上readMatrix.m代码:
function [matrix, tokenlist, category] = readMatrix(filename)
fid = fopen(filename);
%Read the header line
headerline = fgetl(fid);
%Read number of documents and tokens
rowscols = fscanf(fid, '%d %d\n', 2);
%Read the list of tokens - just a long string!
%blah = fscanf(fid, '%s', 1); % required for octave
tokenlist = fgetl(fid);
% Document word matrix
% Each row represents a docum