【问题标题】:Sentiment Analysis R Naive Bayes in german德语中的情绪分析 R 朴素贝叶斯
【发布时间】:2017-11-06 11:30:20
【问题描述】:

我的朴素贝叶斯算法有问题,我无法找出原因。我尝试了一个在线教程,所以一开始我开始创建一些训练数据。在下面的代码中,我只使用了很少的训练数据,但这是同样的问题。

pos_tweets =  rbind(
  c('Ich liebe das auto', 'positive'),
  c('Diese Aussicht ist großartig', 'positive'),
  c('toller morgen', 'positive'),
  c('ich freue mich so', 'positive'),
  c('du bist aber lieb, danke', 'positive')
)

neg_tweets = rbind(
  c('ich hasse autos', 'negative'),
  c('der blick ist horror', 'negative'),
  c('voll müde heute', 'negative'),
  c('schreckliche stille', 'negative'),
  c('er ist ein feind', 'negative')
)

test_tweets = rbind(
    c('Schöne Momente erlebt', 'positive'),
    c('zusammen macht es gleich doppelt spass', 'positive'),
    c('Yeah, toller Tag', 'positive'),
    c('Super schöne Umgebung', 'positive'),
    c('es zieht ein leichter wind auf, sehr angenehm', 'positive')
)

tweetsbind = rbind(pos_tweets, neg_tweets, test_tweets)

matrix1= create_matrix(tweetsbind[,1], language="german", 
                      removeStopwords=FALSE, removeNumbers=TRUE, 
                      stemWords=FALSE) 
mat1 = as.matrix(matrix1)

现在我训练我的模型:

classifier1 = naiveBayes(mat1[1:10,], as.factor(tweetsbind[1:10,2]) )

现在我想使用它:

predicted = predict(classifier1, mat1[11:15,]); predicted

当我查看我的模型时,它看起来很不错,因为负面词被标记为负面,正面词被标记为正面。

但是在使用模型分析测试数据时,它只输出否定,即使陈述明显是肯定的,并且使用的词也存在于训练集中。


我的新代码是:

# search for some twitter data
happy <- searchTwitter(":)",n = 10000, lang ='de')
happy_text <- sapply(happy, function (x) x$getText())
sad <- searchTwitter(":(",n = 10000, lang ='de')
sad_text <- sapply(sad, function (x) x$getText())

# create the matrix
tweets <- rbind(sad_text[1:2500,], happy_text[1:2500,]) # if I use more training data, I get a storage error
tweet <- as.matrix(tweets)
matrix= create_matrix(tweet[,2], language= "german", removeStopwords=FALSE, removeNumbers=TRUE,  stemWords=FALSE) 
matrixdoc = as.matrix(matrix)

# transform to factor and train the model
X <- as.data.frame(matrixdoc[1:5000,])
X$out <- as.factor(tweet[1:5000,3])
X <- as.data.frame(lapply(X, factor))
classifierstack <- naiveBayes(out ~ ., data=X)

# predict
predicted = predict(classifierstack, mat1[11:15,],type = "raw" )

这就是结果:一切都是消极的,即使我所有的输入都是非常积极的(我改变了它们)。

     negativ       positiv
[1,]       1 5.828223e-176
[2,]       1 4.110223e-244
[3,]       1 3.274458e-244
[4,]       1 3.534996e-176
[5,]       1  0.000000e+00

如果我试试这个:

> predict(classifierstack, "zeigt", type = "raw" )
     negativ positiv
[1,]     0.5     0.5

-> 它总是输出 0.5 0.5 并且最终总是负数:/

【问题讨论】:

  • 您使用的是e1071 包中的naiveBayes 吗?
  • 是的,我使用的是 e1071 包
  • 您的更新不可重现,因此看不出您的数据出了什么问题。 testS 是什么?您是否得到了与我在下面对您的原始数据/问题所做的相同的结果?
  • 是的 testS 只是 csv 中的一些测试数据,但在这种情况下,我测试了 mat1[11:15,] 中的旧数据。我认为分类模型存在问题,因为 negaiv 始终为 1 并且 positiv 低得多,因此它总是返回负值,但我该如何改变这一点。我更新了代码(删除了 testS),现在它应该可以重现了。

标签: r sentiment-analysis


【解决方案1】:

您缺少训练数据。如果我运行你的代码,我会得到

> predicted = predict(classifier1, mat1[11:15,]); predicted
[1] negative negative negative positive negative
Levels: negative positive

所以只有前两个元素是错误的——后三个确实应该是消极的、积极的和消极的。如果我们查看在 feinde sind doof 中找到的单词的分类器信息,我们会发现

                             feinde
as.factor(tweetsbind[1:10, 2]) [,1] [,2]
                      negative    0    0
                      positive    0    0

                              sind
as.factor(tweetsbind[1:10, 2]) [,1] [,2]
                      negative    0    0
                      positive    0    0

                              doof
as.factor(tweetsbind[1:10, 2]) [,1] [,2]
                      negative    0    0
                      positive    0    0

所以确实没有要分类的信息,它默认为第一级类别,negative。尝试在您要预测的单词之间存在重叠的地方向它提供更多信息,并且它应该可以工作。


更新如果你运行

> predicted = predict(classifier1, mat1[11:15,], type="raw"); predicted
         negative     positive
[1,] 9.999959e-01 4.093637e-06
[2,] 7.329224e-01 2.670776e-01
[3,] 1.000000e+00 4.598781e-11
[4,] 9.898881e-05 9.999010e-01
[5,] 1.000000e+00 1.608783e-16

然后您可以查看各个概率。适合您的“问题”是输入被读取为数字(而不是二进制因子),因此您不会看到(按行)加起来为 1 的条件概率。根据naiveBayes 的手册页,您将获得高斯均值和 sds。你可以像这样得到条件概率:

X <- as.data.frame(mat1[1:10,])
X$out <- as.factor(tweetsbind[1:10,2])
X <- as.data.frame(lapply(X, factor))
naiveBayes(out ~ ., data=X)

这会给你

          hab
Y          0
  negative 1
  positive 1
          dich
Y          0
  negative 1
  positive 1
          lieb
Y            0   1
  negative 1.0 0.0
  positive 0.8 0.2

这些是 P(lieb|positive) 概率,您需要使用贝叶斯公式来反转概率。

谷歌“零问题”和“朴素贝叶斯”以获取在训练和测试部分均不存在单词时进行轻微改进的指导(请参阅laplace 参数)。

【讨论】:

  • 非常感谢您的回答,对我帮助很大!你解释的情况我理解:) 但是如果你看句子“c('hab dich lieb', 'positive') 那么通常“lieb”应该是肯定的,因为训练集包括句子:c('du bist aber lieb, danke', 'positive'). 所以我不明白为什么算法返回这个句子是否定的? lieb as.factor(tweetsbind[1:10, 2]) [,1] [,2]negative 0.0 0.0000000正 0.2 0.4472136
  • 是的,但是你有两个词不能提高概率。我会更新答案
  • 谢谢,但如果我使用你的代码,我只会得到负面结果(我什至用 5000 条训练数据推文进行了尝试。> 预测的负面结果 [1,] 1 2.610912e-223 [2 ,] 1 1.147911e-223 [3,] 1 2.610912e-223 [4,] 1 2.610912e-223 [5,] 1 8.703038e-224
  • 请更新您的问题,以便我们准确了解您的操作。会更容易关注
猜你喜欢
  • 2019-04-01
  • 2015-08-24
  • 1970-01-01
  • 2020-02-28
  • 2021-08-10
  • 2019-10-05
  • 2018-12-14
  • 2015-08-27
  • 2012-07-02
相关资源
最近更新 更多