【问题标题】:Using Rtexttools lib for machine learning使用 Rtexttools lib 进行机器学习
【发布时间】:2017-03-13 05:48:51
【问题描述】:

我有以下训练集:

    Text,y
    MRR 93345,1
    MRR 93434,1
    MRR 93554,1
    MRR 938900,1
    MRR 93970,1
    MRR 937899,1
    MRR 93868,1
    MRR 938769,1
    MRR 93930,1
    MRR 92325,1
    MRR 931932,1
    MRR 933922,1
    MRR 934390,1
    MRR 93204,1
    MRR 93023,1
    MRR 930982,1
    MRR 87678,-1
    MRR 87956,-1
    MRR 87890,-1
    MRR 878770,-1
    MRR 877886,-1
    MRR 87678367,-1
    MRR 8790,-1
    MRR 87345,-1
    MRR 87149,-1
    MRR 873790,-1
    MRR 873493,-1
    MRR 874303,-1
    MRR 874343,-1
    MRR 874304,-1
    MRR 879034,-1
    MRR 879430,-1
    MRR 87943,-1
    MRR 879434,-1
    MRR 871984,-1
    MRR 873949,-1

我的代码如下:

# Create the document term matrix
dtMatrix <- create_matrix(data["Text"],language="english", removePunctuation=TRUE, stripWhitespace=TRUE,
                          toLower=TRUE,
                          removeStopwords=TRUE,
                          stemWords=TRUE, removeSparseTerms=.998) 

# Configure the training data
container <- create_container(dtMatrix, data$y, trainSize=1:nrow(dtMatrix), virgin=FALSE) 
# train a SVM Model
model <- train_model(container, "SVM", kernel="linear" ,cost=1)

# new data
predictionData <- list("MRR 93111") 

# create a prediction document term matrix 
predMatrix <- create_matrix(predictionData, originalMatrix=dtMatrix,language="english", removePunctuation=TRUE, stripWhitespace=TRUE,
                            toLower=TRUE,
                            removeStopwords=TRUE,
                            stemWords=TRUE, removeSparseTerms=.998) 

# create the corresponding container
predSize = length(predictionData);
predictionContainer <- create_container(predMatrix, labels=rep(0,predSize), testSize=1:predSize, virgin=FALSE) 

# predict
results <- classify_model(predictionContainer, model)

现在通过使用我想预测的 train_model 函数:MRR 93111 as y=1。 这意味着如果字符串以“MRR 93”开头,则输出应为 1,而词干“MRR 87”则为 -1。实际上它不起作用,因为我得到MRR 93111 -1 0.5778781

此外,如果我以不同的方式对训练集进行分类……或者如果我对同一个数据集运行脚本多次,结果似乎会发生变化,这对我来说听起来很奇怪。

UPDATE1:输入(数据)

structure(list(Text = structure(c(26L, 28L, 30L, 34L, 36L, 31L, 
32L, 33L, 35L, 21L, 24L, 27L, 29L, 25L, 22L, 23L, 10L, 20L, 14L, 
13L, 12L, 11L, 15L, 3L, 1L, 5L, 4L, 7L, 9L, 8L, 16L, 18L, 17L, 
19L, 2L, 6L), .Label = c("MRR 87149", "MRR 871984", "MRR 87345", 
"MRR 873493", "MRR 873790", "MRR 873949", "MRR 874303", "MRR 874304", 
"MRR 874343", "MRR 87678", "MRR 87678367", "MRR 877886", "MRR 878770", 
"MRR 87890", "MRR 8790", "MRR 879034", "MRR 87943", "MRR 879430", 
"MRR 879434", "MRR 87956", "MRR 92325", "MRR 93023", "MRR 930982", 
"MRR 931932", "MRR 93204", "MRR 93345", "MRR 933922", "MRR 93434", 
"MRR 934390", "MRR 93554", "MRR 937899", "MRR 93868", "MRR 938769", 
"MRR 938900", "MRR 93930", "MRR 93970"), class = "factor"), Y = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 
-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 
-1L, -1L, -1L, -1L, -1L, -1L)), .Names = c("Text", "Y"), class = "data.frame", row.names = c(NA, 
-36L))

【问题讨论】:

  • 您能提供给我们 dput 而不是写出您的训练集吗?
  • UPDATE1:你需要这个吗?

标签: r svn machine-learning


【解决方案1】:

您的问题是您的代码在单词级别上使用了训练数据和分类。

> dtMatrix$dimnames$Terms
 [1] "87149"    "871984"   "87345"    "873493"   "873790"   "873949"   "874303"   "874304"   "874343"   "87678"    "87678367"
[12] "877886"   "878770"   "87890"    "8790"     "879034"   "87943"    "879430"   "879434"   "87956"    "92325"    "93023"   
[23] "930982"   "93111"    "931932"   "93204"    "93345"    "933922"   "93434"    "934390"   "93554"    "937899"   "93868"   
[34] "938769"   "938900"   "93930"    "93970"    "mrr"

我不完全确定 SVM 是如何处理这些数字字符串的,但它似乎不太关心字符串的 93 部分。将字符串拆分为 caharcter 对单个数字施加了更大的权重:

df$Text <- sapply(1:length(df$Text), function(i) paste(unlist(strsplit(df$Text[i], split = "")), collapse = " "))

我使用 df 而不是 data 因为 data 已经是 RTextTools 中的一个对象,并且在运行代码时给了我一些问题。在创建矩阵时,必须更改最小字长选项。

dtMatrix <- create_matrix(df$Text,language="english", minWordLength=1, #!
                          removePunctuation=TRUE, stripWhitespace=TRUE,
                          toLower=TRUE, removeStopwords=TRUE,
                          stemWords=TRUE, removeSparseTerms=.998)

现在我们得到:

> dtMatrix$dimnames$Terms

[1]“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”“m”“r”

更重要的是:

> results 
  SVM_LABEL  SVM_PROB
1         1 0.9144185

我最近参加了一个关于 RTextTools 和 SVM 的研讨会,他们表示,使用 SVM,每次训练模型时都会得到略微不同的结果。我不完全确定为什么,所以我不会尝试解释,但我们推荐了一本名为“An Introduction to Statistical Learning with Applications in R”的免费书籍来阅读支持向量机。

这里是完整的代码:

df <- structure(list(Text = structure(c(26L, 28L, 30L, 34L, 36L, 31L, 
                                        32L, 33L, 35L, 21L, 24L, 27L, 29L, 25L, 22L, 23L, 10L, 20L, 14L, 
                                        13L, 12L, 11L, 15L, 3L, 1L, 5L, 4L, 7L, 9L, 8L, 16L, 18L, 17L, 
                                        19L, 2L, 6L), .Label = c("MRR   87149", "MRR 871984", "MRR 87345", 
                                                                 "MRR 873493", "MRR 873790", "MRR 873949", "MRR 874303", "MRR 874304", 
                                                                 "MRR 874343", "MRR 87678", "MRR 87678367", "MRR 877886", "MRR 878770", 
                                                                 "MRR 87890", "MRR 8790", "MRR 879034", "MRR 87943", "MRR 879430", 
                                                                 "MRR 879434", "MRR 87956", "MRR 92325", "MRR 93023", "MRR 930982", 
                                                                 "MRR 931932", "MRR 93204", "MRR 93345", "MRR 933922", "MRR 93434", 
                                                                 "MRR 934390", "MRR 93554", "MRR 937899", "MRR 93868", "MRR 938769", 
                                                                 "MRR 938900", "MRR 93930", "MRR 93970"), class = "factor"), Y = c(1L, 
                                                                                                                                   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 
                                                                                                                                   -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 
                                                                                                                                   -1L, -1L, -1L, -1L, -1L, -1L)), .Names = c("Text", "Y"), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                -36L))



df$Text <- as.character(df$Text)  
# new data
df[nrow(df)+1,] <- c("MRR    93111","")
df$Text <- sapply(1:length(df$Text), function(i) paste(unlist(strsplit(df$Text[i], split = "")), collapse = " "))

# Create the document term matrix
dtMatrix <- create_matrix(df$Text,language="english", minWordLength=1, 
                          removePunctuation=TRUE, stripWhitespace=TRUE,
                          toLower=TRUE, removeStopwords=TRUE,
                          stemWords=TRUE, removeSparseTerms=.998) 


dtMatrix$dimnames$Terms
dtMatrix$dimnames$Docs

# Configure the training data
container <- create_container(dtMatrix, df$Y, trainSize=1:36, testSize = 37, virgin=TRUE) 

container <- create_container(dtMatrix,
                              labels=df$Y, trainSize=1:36, testSize = 37, virgin=TRUE)

# train a SVM Model
model <- train_model(container, "SVM",kernel="linear" ,cost=1) ##??

results <- classify_model(container,model)

results 

【讨论】:

  • 非常感谢 JonGrub,你已经解决了这个问题。顺便说一句,我不明白为什么“stripWhitespace”标志实际上不起作用。根据我的理解,它应该删除空格,以便训练集没有空白,例如:“MRR 93345”应该变成“MRR93345”并被视为一个独特的词位。在这种情况下,我们将有一个不那么分散的矩阵,并且匹配包含“MRR93”的预测的几率绝对更高。但也许我错过了什么......
  • 您对 stripWhitespace 的作用有误。来自 tm 帮助:“具有多个空白字符的文本文档折叠为一个空白”。这可能很有用,因为将文本拆分为单词的方式是从一个空白提取到下一个空白。但是,在 RTexTools 中,它似乎根本不起作用,因为 stripWhitespace=TRUE 或 FALSE 具有相同的结果。试试dtMatrix &lt;- create_matrix("MRR bla 93111", stripWhitespace=TRUE)dtMatrix$dimnames$Docs
  • 好吧,我的想法就像它作为一个 trim() 函数工作,而它基本上只是将 N 个空格减少到 1。例如:[MRR/x20/x20/x2093111]->[MRR 93111]
  • 我在你的函数之后得到这个错误:Error in strsplit(dt$Text[i], split = "") : non-character argument.如果我在生成矩阵时将它转换为strsplit(as.character(dt$Text[i]), split = "",我得到:Error in [.simple_triplet_matrix(matrix, , sort(colnames(matrix))) : Invalid subscript type: NULL.
  • 在您提供的文本中,您的文本是类因素 (class(df$Text))。尝试df$Text &lt;- as.character(df$Text) 前一行。因子对象有时表现得很奇怪......更新:没关系,这不是问题。
猜你喜欢
  • 2012-11-16
  • 2013-06-07
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 2017-06-07
  • 2017-12-18
  • 2019-06-16
相关资源
最近更新 更多