Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not all these observations are directly relevant to your issues as expressed, but..:</p> <p>a. why are the key in prefs, as shown, longs? unless you have billions of users, simple ints will be fine and save you a little memory.</p> <p>b. your code:</p> <pre><code>centroids = [prefs[random.choice(users)] for i in range(k)] </code></pre> <p>can give you repeats (two identical centroids), which in turn would not make the K-means algorithm happy. Just use the faster and more solid</p> <pre><code>centroids = [prefs[u] for random.sample(users, k)] </code></pre> <p>c. in your code as posted you're calling a function <code>simple_pearson</code> which you never define anywhere; I assume you mean to call <code>sim_func</code>, but it's really hard to help on different issues while at the same time having to guess how the code you posted differs from any code that might actually be working</p> <p>d. one more indication that this posted code may be different from anything that might actually work: you set <code>bestmatch=(0,0)</code> but then test with <code>if d &lt; bestmatch[1]:</code> -- how is the test ever going to succeed? is the distance function returning negative values?</p> <p>e. the point of a defaultdict is that just accessing <code>row[m]</code> magically adds an item to <code>row</code> at index <code>m</code> (with the value obtained by calling the defaultdict's factory, here 0.0). That item will then take up memory forevermore. You absolutely DON'T need this behavior, and therefore your code:</p> <pre><code> row = prefs[user_id] for m in items: if row[m] &gt; 0.0: centroids[i][m]+=(row[m]/len_best) </code></pre> <p>is wasting huge amount of memory, making <code>prefs</code> into a dense matrix (mostly full of 0.0 values) from the sparse one it used to be. If you code instead</p> <pre><code> row = prefs[user_id] for m in row: centroids[i][m]+=(row[m]/len_best) </code></pre> <p>there will be no growth in <code>row</code> and therefore in <code>prefs</code> because you're looping over the keys that <code>row</code> already has.</p> <p>There may be many other such issues, major like the last one or minor ones -- as an example of the latter, </p> <p>f. don't divide a bazillion times by <code>len_best</code>: compute its inverse one outside the loop and multiply by that inverse -- also you don't need to do that multiplication inside the loop, you can do it at the end in a separate since it's the same value that's multiplying every item -- this saves no memory but avoids wantonly wasting CPU time;-). OK, these are <em>two</em> minor issues, I guess, not just one;-).</p> <p>As I mentioned there may be many others, but with the density of issues already shown by these six (or seven), plus the separate suggestion already advanced by S.Lott (which I think would not fix your main out-of-memory problem, since his code still addressing the <code>row</code> defaultdict by too many keys it doesn't contain), I think it wouldn't be very productive to keep looking for even more -- maybe start by fixing these ones and if problems persist post a separate question about those...?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload