码迷,mamicode.com
首页 > 其他好文 > 详细

“ML_for_Hackers”[2]

时间:2016-05-27 23:17:12      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:

今天在处理文本回归时,遇到一个问题,需要记录。

ranks <- read.csv(‘/Users/lvxubo/Desktop/ML_for_Hackers-master/06-Regularization/data/oreilly.csv‘,stringsAsFactors=FALSE)
library(‘tm‘)
documents <- data.frame(Text=ranks$Long.Desc.)
row.names(documents) <- 1:nrow(documents)

corpus <- Corpus(DataframeSource(documents))
corpus <- tm_map(corpus,tolower)
corpus <- tm_map(corpus,stripWhitespace)
corpus <- tm_map(corpus,removeWords,stopwords(‘english‘))
corpus <- tm_map(corpus, PlainTextDocument)

dtm <- DocumentTermMatrix(corpus)

如果没有标记的一句代码,会报错:

Error in UseMethod("meta", x) : "meta"没有适用于"character"目标对象的方法
此外: Warning message:
In mclapply(unname(content(x)), termFreq, control) :
  all scheduled cores encountered errors in user code

这是stackoverflow上的解决:

It seems this would have worked just fine in tm 0.5.10 but changes in tm 0.6.0 seems to have broken it. The problem is that the functions tolower and trim won‘t necessarily return TextDocuments (it looks like the older version may have automatically done the conversion). They instead return characters and the DocumentTermMatrix isn‘t sure how to handle a corpus of characters.

So you could change to

corpus_clean <- tm_map(news_corpus, content_transformer(tolower))

Or you can run

corpus_clean <- tm_map(corpus_clean, PlainTextDocument)

after all of your non-standard transformations (those not in getTransformations()) are done and just before you create the DocumentTermMatrix. That should make sure all of your data is in PlainTextDocument and should make DocumentTermMatrix happy.

“ML_for_Hackers”[2]

标签:

原文地址:http://www.cnblogs.com/lvlvlvlvlv/p/5536232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!