将dfmSparse从Quanteda包转换为R中的数据帧或数据表
我有一个dfmSparse对象(大,2.1GB),它是标记化的,带有ngrams(unigrams、bigrams、trigrams和fourgrams),并且我想将它转换为数据帧或数据表对象,其中包含以下列:Content和Frequency 我试着取消。。。但是没有起作用。我是NLP新手,我不知道使用什么方法,我没有想法,也没有在这里或谷歌找到解决方案 有关数据的一些信息:将dfmSparse从Quanteda包转换为R中的数据帧或数据表,r,dataframe,nlp,data.table,quanteda,R,Dataframe,Nlp,Data.table,Quanteda,我有一个dfmSparse对象(大,2.1GB),它是标记化的,带有ngrams(unigrams、bigrams、trigrams和fourgrams),并且我想将它转换为数据帧或数据表对象,其中包含以下列:Content和Frequency 我试着取消。。。但是没有起作用。我是NLP新手,我不知道使用什么方法,我没有想法,也没有在这里或谷歌找到解决方案 有关数据的一些信息: >str(tokfreq) Formal class 'dfmSparse' [package "quanted
>str(tokfreq)
Formal class 'dfmSparse' [package "quanteda"] with 11 slots
..@ settings :List of 1
.. ..$ : NULL
..@ weighting : chr "frequency"
..@ smooth : num 0
..@ ngrams : int [1:4] 1 2 3 4
..@ concatenator: chr "_"
..@ Dim : int [1:2] 167500 19765478
..@ Dimnames :List of 2
.. ..$ docs : chr [1:167500] "character(0).content" "character(0).content" "character(0).content" "character(0).content" ...
.. ..$ features: chr [1:19765478] "add" "lime" "juice" "tequila" ...
..@ i : int [1:54488417] 0 75 91 178 247 258 272 327 371 391 ...
..@ p : int [1:19765479] 0 3218 3453 4015 4146 4427 4637 140665 140736 142771 ...
..@ x : num [1:54488417] 1 1 1 1 5 1 1 1 1 1 ...
..@ factors : list()
>summary(tokfreq)
Length Class Mode
3310717565000 dfmSparse S4
谢谢
编辑:
这是我如何从语料库创建数据集的:
# tokenize
tokenized <- tokenize(x = teste, ngrams = 1:4)
# Creating the dfm
tokfreq <- dfm(x = tokenized)
#标记化
标记化说到“太大”,您可能会遇到内存问题。例如:
library(quanteda)
mydfm <- dfm(subset(inaugCorpus, Year>1980))
class(mydfm)
# [1] "dfmSparse"
# attr(,"package")
# [1] "quanteda"
print(object.size(mydfm), units="KB")
# 273.6 Kb
如果我理解你关于“内容”和“频率”是什么意思的问题,这应该可以做到。请注意,在这种方法中,data.frame不大于稀疏矩阵,因为您只是记录总计数,而不是存储文档行分布
myDfm <- dfm(data_corpus_inaugural, ngrams = 1:4, verbose = FALSE)
head(myDfm)
## Document-feature matrix of: 57 documents, 314,224 features.
## (showing first 6 documents and first 6 features)
## features
## docs fellow-citizens of the senate and house
## 1789-Washington 1 71 116 1 48 2
## 1793-Washington 0 11 13 0 2 0
## 1797-Adams 3 140 163 1 130 0
## 1801-Jefferson 2 104 130 0 81 0
## 1805-Jefferson 0 101 143 0 93 0
## 1809-Madison 1 69 104 0 43 0
# convert to a data.frame
df <- data.frame(Content = featnames(myDfm), Frequency = colSums(myDfm),
row.names = NULL, stringsAsFactors = FALSE)
head(df)
## Content Frequency
## 1 fellow-citizens 39
## 2 of 7055
## 3 the 10011
## 4 senate 15
## 5 and 5233
## 6 house 11
tail(df)
## Content Frequency
## 314219 and_may_he_forever 1
## 314220 may_he_forever_bless 1
## 314221 he_forever_bless_these 1
## 314222 forever_bless_these_united 1
## 314223 bless_these_united_states 1
## 314224 these_united_states_of 1
object.size(df)
## 25748240 bytes
object.size(myDfm)
## 29463592 bytes
我正试图用View()查看数据,但出现了一个错误,“太大”谢谢!我尝试了你的建议,使用了重塑2,使用了我的一部分数据(144MB),尽管如此,我收到:teste你的memory.size()
和memory.limit()
?足够的硬盘空间(可能有些例程使用临时文件?我没有太多的代码分析经验,也不知道R的引擎盖下发生了什么)我试图清理内存并重试,但同样的错误(错误立即发生)。详细信息:>memory.size()[1]263.75
和>memory.limit()[1]8134
我认为问题不在于HD(超过60GB的可用空间)。是的!就这样!工作非常好,没有内存问题,速度非常快。大小为1561MB,大多数比dfm小500MB。非常感谢。这是一个简单的解决方案,但我不知道没有你的帮助该怎么办。很高兴能提供帮助,也很高兴听到quanteda的任何经验/问题/功能请求。嗨,这对来自语料库的dfm来说非常有用,只有一个文档。我想知道,有没有一种简单的方法可以扩展到多文档语料库dfm?谢谢。一种方法是指定文档,如下所示:data.frame(…,Frequency=colSums(myDfm[1,])@KenBenoit,在Diego的示例中,dfm()
返回长度为1到4的ngram。在调用data.frame
来标记每个ngram的长度时,有没有简单的方法来创建一个附加列n
?
dfm <- dfm(inaugCorpus, ngrams = 1L:16L)
print(object.size(dfm), units="MB")
# 254.1 Mb
library(reshape2)
df <- melt(as.matrix(dfm))
print(object.size(df), units="MB")
# 1884.6 Mb
memory.size()
# [1] 3676.43
memory.size(TRUE)
# [1] 3858.12
memory.limit()
# [1] 8189
myDfm <- dfm(data_corpus_inaugural, ngrams = 1:4, verbose = FALSE)
head(myDfm)
## Document-feature matrix of: 57 documents, 314,224 features.
## (showing first 6 documents and first 6 features)
## features
## docs fellow-citizens of the senate and house
## 1789-Washington 1 71 116 1 48 2
## 1793-Washington 0 11 13 0 2 0
## 1797-Adams 3 140 163 1 130 0
## 1801-Jefferson 2 104 130 0 81 0
## 1805-Jefferson 0 101 143 0 93 0
## 1809-Madison 1 69 104 0 43 0
# convert to a data.frame
df <- data.frame(Content = featnames(myDfm), Frequency = colSums(myDfm),
row.names = NULL, stringsAsFactors = FALSE)
head(df)
## Content Frequency
## 1 fellow-citizens 39
## 2 of 7055
## 3 the 10011
## 4 senate 15
## 5 and 5233
## 6 house 11
tail(df)
## Content Frequency
## 314219 and_may_he_forever 1
## 314220 may_he_forever_bless 1
## 314221 he_forever_bless_these 1
## 314222 forever_bless_these_united 1
## 314223 bless_these_united_states 1
## 314224 these_united_states_of 1
object.size(df)
## 25748240 bytes
object.size(myDfm)
## 29463592 bytes
textstat_frequency(data_dfm_lbgexample) %>% head()
# feature frequency rank docfreq group
# 1 P 356 1 5 all
# 2 O 347 2 4 all
# 3 Q 344 3 5 all
# 4 N 317 4 4 all
# 5 R 316 5 4 all
# 6 S 280 6 4 all