我必须对包含 800K 文本的文本分类进行一些工作。我一直在尝试运行在以下链接中找到的实际示例:
http://garonfolo.dk/herbert/2015/05/r-text-classification-using-a-k-nearest-neighbour-model/
一切都进展顺利,直到我收到以下说明:
# Transform dtm to matrix to data frame - df is easier to work with
mat.df <- as.data.frame(data.matrix(dtm), stringsAsfactors = FALSE)
运行几个小时后,我收到一条错误消息:
Error: cannot allocate vector of size 583.9 Gb
In addition: Warning messages:
1: In vector(typeof(x$v), prod(nr, nc)) :
Reached total allocation of 8076Mb: see help(memory.size)
2: In vector(typeof(x$v), prod(nr, nc)) :
Reached total allocation of 8076Mb: see help(memory.size)
3: In vector(typeof(x$v), prod(nr, nc)) :
Reached total allocation of 8076Mb: see help(memory.size)
4: In vector(typeof(x$v), prod(nr, nc)) :
Reached total allocation of 8076Mb: see help(memory.size)
有办法克服这个错误吗?
例如,是否可以拆分 data.matrix(dtm) 以分块运行作业,然后以某种方式合并它们?还是用另一种方式或用 Python 来解决这个问题更好?谢谢
最佳答案
在此之前as.data.frame()
调用,输入这行代码:
dtm <- removeSparseTerms(dtm, sparse=0.9)
。
论点sparse=...
是 0 到 1 之间的数字。它与您要保留的文档数量成正比。以上,不是 90%。通常,您会通过反复试验找到正确/最佳值。就您而言,您最终可能会得到一个奇怪的数字,例如 0.79333。取决于你想做什么。
removeSparseTerms()
删除术语,但保持较小结果矩阵中的文档数量不变。因此,您将从 12165735 * 800000 元素矩阵变为 476 * 800000 矩阵。现在可以在您的计算机上进行处理。
如果没有,请尝试对大矩阵进行巧妙的按列拆分-应用-组合技巧。
关于800K 文档的 R 文本分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40809626/