Naive Bayes e1071将每个姓氏归为同一血统

时间:2016-02-16 23:13:57

标签: r machine-learning text-classification naivebayes

我是新手,我希望使用Naive Bayes分类器来识别基于3克姓氏的血统。

奇怪的是,下面的代码将(大多数)姓氏归类为日本血统 - JPN。

library(e1071)
library(caret)
test <- read.csv("https://dl.dropboxusercontent.com/u/116353/test.csv", header=T)
test <- test[,-1]
train <- read.csv("https://dl.dropboxusercontent.com/u/116353/train.csv", header=T)
train <- train[,-1]
model  <- naiveBayes(nation~., data=train, laplace=1) 
predictions <- predict(model, newdata=test[,-1], threshold = 0.01)
confusionMatrix(predictions, test$nation)

一些线索:

当我增加训练集的大小时,问题会变得更糟,每个人都被归类为JPN;

我很确定数据很好,因为我测试了其他分类器(SVM和Carvar&amp; Trenkle)并且它们表现良好。 谢谢!

1 个答案:

答案 0 :(得分:0)

如果你看一下你将会想到的概率

> predictions <- predict(model, newdata=test[,-1], threshold = 0.01,type="raw")
> head(predictions)
               EAS          GER           IBR           ITA JAP
[1,] 1.836872e-109 1.535376e-40 2.058015e-228 1.351274e-124   1
[2,] 1.286083e-276 1.133225e-45  0.000000e+00 2.507828e-157   1
[3,] 4.008149e-163 1.257501e-87 3.181309e-136 7.501385e-191   1
[4,]  2.477674e-71 2.008653e-85  0.000000e+00 9.385175e-133   1
[5,] 1.539279e-289 6.367960e-23 1.251711e-114 1.656610e-149   1
[6,]  2.359038e-95 6.367960e-23 3.390903e-110 8.556824e-156   1

这看起来很尴尬。

检查所学习的概率并不能为第一眼提供真正的内在:

head(t(as.data.frame(model$tables)))
              EAS       GER         IBR         ITA        JAP
acc.1 0.000000000 0.0000000 0.002253944 0.016165245 0.00000000
acc.2 0.000000000 0.0000000 0.047440015 0.126124942 0.00000000
ach.1 0.011730205 0.0154321 0.006010518 0.003816794 0.00931677
ach.2 0.107721635 0.1234544 0.077323244 0.061669116 0.09614740
ada.1 0.002932551 0.0000000 0.006010518 0.002694207 0.03105590
ada.2 0.054100023 0.0000000 0.077323244 0.051841606 0.17360366

让我们看一下第一个例子的特征

 head(test[,test[1,]==1])
  ato ena
1   1   1

接下来检查列车数据中特征出现的次数:

> tail(cumsum(train[,c("ato","ena")]))
Error in `[.data.frame`(train, , c("ato", "ena")) : 
  undefined columns selected

OPPSY (WTF的技术术语)实际上它是ena

显然,您的测试集中的功能在训练集中没有出现。在实施ML模型时,采用最后一个因素元素并不罕见。

你应该使用较小的n-gram长度,使用类似Katz-back的估计值或过滤测试数据以仅出现在训练集中。

你提高门槛的策略并不糟糕。

predictions <- predict(model, newdata=test[,-1], threshold = 0.1)
 confusionMatrix(predictions, test$nation)
          Reference
Prediction EAS GER IBR ITA JAP
       EAS 166  49 127 280  77
       GER  10   7  24  88   5
       IBR  39  11 152 223  29
       ITA  15   3  31 419   4
       JAP  11   4  21  87  62

Overall Statistics

               Accuracy : 0.4146  
相关问题