Java中的朴素贝叶斯分类器
下面是我的一个方法,用于将幸运饼干消息分类为预测消息或明智消息 我的代码构建的数据库似乎是正确的,所以我不担心这一点。我在322条幸运饼培训信息的基础上建立了这个数据库 我尝试执行的计算如下: [(信息明智的概率)*(你发现的概率) “单词”知道在一个明智的世界里找到它的可能性 quote)]/(找到该单词的概率) 在日志空间中完成 我发现出于某种原因,一些概率是相等的(当它们确实不应该相等时,我做了一些手工计算)。TestLabels是一个由正确答案组成的文件:0代表明智的引语,1代表预测性引语,都在不同的行中Java中的朴素贝叶斯分类器,java,naivebayes,Java,Naivebayes,下面是我的一个方法,用于将幸运饼干消息分类为预测消息或明智消息 我的代码构建的数据库似乎是正确的,所以我不担心这一点。我在322条幸运饼培训信息的基础上建立了这个数据库 我尝试执行的计算如下: [(信息明智的概率)*(你发现的概率) “单词”知道在一个明智的世界里找到它的可能性 quote)]/(找到该单词的概率) 在日志空间中完成 我发现出于某种原因,一些概率是相等的(当它们确实不应该相等时,我做了一些手工计算)。TestLabels是一个由正确答案组成的文件:0代表明智的引语,1代表预测性引
public void classifyOne (Words[] db) throws IOException {
int right = 0;
int wrong = 0;
int equal = 0;
Scanner inFile1 = new Scanner(new File("testdata.txt"));
Scanner inFile2 = new Scanner(new File("testlabels.txt"));
int[] label = new int[101];
String[] allmessages = new String[101];
for (int k = 0; k < allmessages.length; k++) {
String onemessage = inFile1.nextLine();
allmessages[k] = onemessage;
String[] wordsfrommsg = onemessage.split("\\s");
//read in the class of fortune cookie message, either 1 or 0.
label[k] = Integer.parseInt(inFile2.nextLine());
//probability based on training data that a quote is either wise or predictive
double wiseprob = Math.log(170.0)-Math.log(322.0);
double predictprob = Math.log(152.0)-Math.log(322.0);
for (int j = 0; j < db.length; j++) {
for (int i = 0; i < wordsfrommsg.length; i++) {
//if you find a word in the onemessage that matches a word from the database...
if (wordsfrommsg[i].equals(db[j].word)) {
//multiply by the probability of finding that word, given that it's a prediction quote
predictprob += Math.log ((db[j].predcount) / 152.0);
//and divide by the probability that I find that word at all.
predictprob -= Math.log(((db[j].wisecount + db[j].predcount) / 704.0));
wiseprob += Math.log(((db[j].wisecount) / 170.0));
wiseprob -= Math.log(((db[j].wisecount + db[j].predcount) / 704.0));
}
}
}
//there are messages for which the probabilities are equal.
if(Math.pow(predictprob, 10)==Math.pow(wiseprob, 10)){equal+=1; System.out.println(allmessages[k]);}
if (Math.pow(predictprob, 10)>Math.pow(wiseprob, 10)) {
if (label[k] == 1) {
right++;
} else {
wrong++;
}
}
else if (Math.pow(predictprob, 10)<Math.pow(wiseprob, 10)){
if (label[k] == 1) {
wrong++;
} else {
right++;
}
}
}
System.out.println(right);
System.out.println(wrong);
System.out.println(equal);
}
public void classifyOne(Words[]db)抛出IOException{
int right=0;
int错误=0;
int等于0;
Scanner inFile1=新扫描仪(新文件(“testdata.txt”);
Scanner inFile2=新扫描仪(新文件(“testlabels.txt”);
int[]label=新int[101];
String[]allmessages=新字符串[101];
for(int k=0;kMath.pow(wiseprob,10)){
if(标签[k]==1){
右++;
}否则{
错误++;
}
}
else if(Math.pow(predictprob,10)是否有可能将带标点符号的单词与不带标点符号的单词进行比较?是否有可能将带标点符号的单词与不带标点符号的单词进行比较?