- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现朴素贝叶斯算法来进行文本分类。我有大约 1000 个用于培训的文档和 400 个用于测试的文档。我认为我已经正确实现了培训部分,但我在测试部分感到困惑。以下是我所做的简要说明:
在我的训练函数中:
vocabularySize= GetUniqueTermsInCollection();//get all unique terms in the entire collection
spamModelArray[vocabularySize];
nonspamModelArray[vocabularySize];
for each training_file{
class = GetClassLabel(); // 0 for spam or 1 = non-spam
document = GetDocumentID();
counterTotalTrainingDocs ++;
if(class == 0){
counterTotalSpamTrainingDocs++;
}
for each term in document{
freq = GetTermFrequency; // how many times this term appears in this document?
id = GetTermID; // unique id of the term
if(class = 0){ //SPAM
spamModelArray[id]+= freq;
totalNumberofSpamWords++; // total number of terms marked as spam in the training docs
}else{ // NON-SPAM
nonspamModelArray[id]+= freq;
totalNumberofNonSpamWords++; // total number of terms marked as non-spam in the training docs
}
}//for
for i in vocabularySize{
spamModelArray[i] = spamModelArray[i]/totalNumberofSpamWords;
nonspamModelArray[i] = nonspamModelArray[i]/totalNumberofNonSpamWords;
}//for
priorProb = counterTotalSpamTrainingDocs/counterTotalTrainingDocs;// calculate prior probability of the spam documents
}
我认为我正确理解并实现了培训部分,但我不确定我是否可以正确实现测试部分。在这里,我尝试检查每个测试文档,并计算每个文档的 logP(spam|d) 和 logP(non-spam|d)。然后我比较这两个数量以确定类别(垃圾邮件/非垃圾邮件)。
在我的测试函数中:
vocabularySize= GetUniqueTermsInCollection;//get all unique terms in the entire collection
for each testing_file:
document = getDocumentID;
logProbabilityofSpam = 0;
logProbabilityofNonSpam = 0;
for each term in document{
freq = GetTermFrequency; // how many times this term appears in this document?
id = GetTermID; // unique id of the term
// logP(w1w2.. wn) = C(wj)∗logP(wj)
logProbabilityofSpam+= freq*log(spamModelArray[id]);
logProbabilityofNonSpam+= freq*log(nonspamModelArray[id]);
}//for
// Now I am calculating the probability of being spam for this document
if (logProbabilityofNonSpam + log(1-priorProb) > logProbabilityofSpam +log(priorProb)) { // argmax[logP(i|ck) + logP(ck)]
newclass = 1; //not spam
}else{
newclass = 0; // spam
}
}//for
我的问题是;我想返回每个类别的概率,而不是精确的 1 和 0(垃圾邮件/非垃圾邮件)。我想看看例如newclass = 0.8684212 所以我可以稍后应用阈值。但我在这里很困惑。如何计算每个文档的概率?我可以使用 logProbabilities 来计算它吗?
最佳答案
由属于 < 类的一组特征 {F1, F2, ..., Fn} 描述的数据的概率em>C,根据朴素贝叶斯概率模型,为
P(C|F) = P(C) * (P(F1|C) * P(F2|C) * ... * P(Fn|C)) / P(F1, ..., Fn)
您拥有除 1/P( F1, ..., Fn) 之外的所有项(以对数形式)术语,因为您正在实现的朴素贝叶斯分类器中未使用该术语。 (严格来说,MAP 分类器。)
您还必须收集这些特征的频率,并从中计算
P(F1, ..., Fn) = P(F1) * ... * P(Fn)
关于machine-learning - 记录实现朴素贝叶斯进行文本分类的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5451004/
是否可以在表格 View 中提供单独的单元格样式? 最佳答案 是的,看看几乎所有的苹果应用程序。他们倾向于使用不同控件的表格进行设置。这就是表格单元格具有不同重用标识符的原因。这样您就可以将正确的类型
我想知道是否可以在 Javascript 中做这样的事情: one(plus(nine())); // returns 10 看起来是这样,但我不确定如何将 one() 的值传递给 plus() 函数
我正在编写一个算法,在给定模型的情况下,我计算数据集列表的可能性,然后需要对每个可能性进行归一化(概率)。所以像 [0.00043, 0.00004, 0.00321] 这样的东西可能会被转换成 [0
我想检测是否可以进行局域网唤醒。 在我的路由器(Tomato 固件)上有一个包含信息的表格 - 当显示设备“事件(在 ARP 中)”时 - 可以通过 WOL(离线 Linux 电脑)打开该设备。 我想
我正在寻找一种方法来解析具有几个可能使用的不同终止字符的子字符串。我应该使用不同的方法还是有办法使用正则表达式来整理字符? 我当前的代码使用: smallstring = bigstring.subs
目前我有可能使用 surefire 插件在 maven 上运行多个测试,如下所示: mvn clean test -Dsurefire.suiteXmlFiles=test1.xml,test2.xm
最近,我成功地为 HDFS 和 YARN 启用了 HA。现在我有一个事件的和备用的名称节点,自动故障转移工作正常。我正在使用 Cloudera Manager 和 CDH 5。 我有以下问题。 例如,
我想要最简单的动词,它给出给定长度的所有 bool 列表的列表。 例如 f=. NB. Insert magic here f 2 0 0 0 1 1 0 1 1 f 3 0 0 0
这将是一个井字游戏实现: data Row = A | B | C deriving (Show, Read, Eq, Ord, Enum, Bounded) data Column = X
这是一个假设性的问题: 我想构建一个 Chrome 扩展程序,它会跟踪用户在该扩展程序处于事件状态的网页子集上的点击情况,并通过 AJAX 将数据作为 POST 或 GET 请求发送到我在某处运行的外
我们想使用 Entity Framework (.NET 4.0) 构建可以处理 Sql Server、MySQL 和 Oracle 的应用程序。也许 Sqlite 也是。 通过配置文件中的一些设置应
是否可以在 iPhone 上通过指定网络的 SSID 来创建数据连接? 是否可以从应用程序检查具有指定 SSID 的网络的信号/可用性? 问候,斯腾 最佳答案 遗憾的是,如果不使用私有(private
我正在使用各种 lambda 表达式语法测试性能差异。如果我有一个简单的方法: public IEnumerable GetItems(int point) { return this.ite
Effective Java 第 2 版的第 16 条,支持组合优于继承 说如下 “如果父类(super class)在后续版本中获得了一个新方法并且你运气不好给子类一个具有相同签名的方法和不同的返回
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我想知道 Tic Tac Toe 有多少种可能性,所以我在网上搜索并找到了一个数学定理,它表明 Tic Tac Toe 中有 255168 种可能的游戏。 网站:http://www.se16.inf
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我想在单个值中存储 4 个 boolean 可能性。例如,我想要一个单一的值来判断一个人是否: IsSingle IsGraduate IsMale IsLookingForPartner 那么将它们
我的 Wicket 口应用程序遇到了一些问题。 问题出在一个名为 OverviewPage 的页面上,这里有一些面板,例如 ListPanel,其中有我的 RepeatingView。 这个Repea
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!