math - converting R code snippet to use the Matrix package? -
I'm not sure there are any R users there, but just in case:
I am A novice at R. and please "Below" the following R code snippet:
Beta < - exp (as.matrix (read.table ('beta.transpose')) WordFreq & lt; - read.table ('freq-matrix') WordProbs & lt; - WordFreq $ V1 / sum (WordFreq) infile & lt; - file ('freq-matrix') outfile & lt; - file ('doc_topic_prob_matrix', 'w' (open) for open (outfile) (in 1: 9 30 9 4) {vec & lt; - (Scans (infile, nlines = 1)) topics & lt; - (vec / WordProbs)% *% beta write.table topic, outfile, append = t, row.Name = f, col.names = F)} < / Code>
When I tried to run it on my dataset, the system was swatched and switched like crazy. Now I realize that this is a simple reason: in the file Freak-matrix There are a large (22 GB) matrix and I was trying to read it in memory.
I have been asked to use the package, because lots of freq-matrix, many zeros all There are places and it handles such cases well, will he help? If so, any indication of this code change would be most welcome, I have no experience and the introduction on the site starts reading through PDFhave make.
Many thanks
~ l
my suggestion is complete By way of shutting down, you do not give enough information about the content of your files, and I had to guess by code. Anyway, it goes here.
You do not tell it, but I'll assume that your code crashes on the second line, when you read in the larger matrix. Loop reads lines one-by-one, and should not crash. To calculate the WordProbs vector, the only reason you need is to have a large matrix, so why do not you use that same looping code by using scan
? In fact, you probably do not have to store the WordProbs
vector, simply sum (WordFreq)
- you might use the initial run through the HTF file Ho. To re-calculate existing WordProb
, rewrite the formula within the loop.
Comments
Post a Comment