<div dir="ltr"><font size="4">This was amazingly helpful, thanks! I'll check the denominator of my correlation, but I'm pretty sure that's correct. But it won't hurt to double check it. When I took a slice of my similarity matrix all the correlations were floats in between -1 and +1, so that's a good sign that my computation was correct albeit very time consuming. The reason I didn't use Numpy arrays is because the professor for this class doesn't know a lot of Python, and he uses Microsoft Visual Studio to run my Python programs. I don't know if Numpy is a part of that installation. Numpy is not part of the standard Python installation, so if I submit a program that contains anything from the Numpy library then he won't be able to run my code. I emailed him and asked him about his Python installation, but he didn't get back to me.</font><div><font size="4"><br></font></div><div><font size="4">Thanks for the feedback! Very much appreciated!!!</font></div><div><font size="4"><br></font></div><div><font size="4">Best,</font></div><div><font size="4"><br></font></div><div><font size="4">Douglas.</font></div><div><font size="4"> </font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 10, 2015 at 8:52 AM, Sunhwan Jo <span dir="ltr"><<a href="mailto:sunhwanj@gmail.com" target="_blank">sunhwanj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">1. Your “correlation” function takes most of the execution time.<div><br></div><div><div></div><blockquote type="cite"><div>def Correlation(p, q):</div><div> global PQ_Ratings</div><div> sum1 = 0</div><div> sum2 = 0</div><div> numeratorProduct = 1</div><div> denominatorProduct1 = 1</div><div> denominatorProduct2 = 1</div><div> for key in filter( lambda x: x[0] == p or x[0] == q, PQ_Ratings.keys( ) ):</div><div> if key[0] == p:</div><div> sum1+= PQ_Ratings[key] - AverageRatingsOfItems[key[1]]</div><div> else:</div><div> sum2+= PQ_Ratings[key] - AverageRatingsOfItems[key[1]]</div><div> numeratorProduct+= sum1*sum2</div><div> denominatorProduct1+= sum1**2</div><div> denominatorProduct2+= sum2**2</div><div> return numeratorProduct/(math.sqrt(denominatorProduct1)*math.sqrt(denominatorProduct2))</div></blockquote><div><br></div><div>By changing sum1 and sum2 as list comprehension can increase the execution speed about 10x (rough estimate using your code). In addition, the denominator is also wrong. It should be *sum of squared differences* not *square of sum of differences*, but I’m not concerned at this yet.</div><div><br></div><div><blockquote type="cite">def Correlation(p, q):<br> global PQ_Ratings<br> sum1 = 0<br> sum2 = 0<br> numeratorProduct = 1<br> denominatorProduct1 = 1<br> denominatorProduct2 = 1<br> keys = [key for key in PQ_Ratings.keys() if key[0] == p or key[0] == q]<br> sum1 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == p])<br> sum2 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == q])<br> numeratorProduct+= sum1*sum2<br> denominatorProduct1+= sum1**2<br> denominatorProduct2+= sum2**2<br> return numeratorProduct/(math.sqrt(denominatorProduct1)*math.sqrt(denominatorProduct2))</blockquote><br></div><div>2. You don’t have to re-calculate sum1 each time. “sum1" only depends on “p”. So, you can calculate that only in the outer loop and reuse it.</div><div><br></div><div></div><blockquote type="cite"><div>keys = PQ_Ratings.keys()<br>for i in range(1, len(SimilarityMatrix)):<br> sum1 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == i])<br><br> for j in range(i + 1, len(SimilarityMatrix)):<br> sum2 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == j])<br> numeratorProduct = sum1*sum2 + 1<br> denominatorProduct1 = sum1**2 + 1<br> denominatorProduct2 = sum2**2 + 1<br> SimilarityMatrix[i][j] = numeratorProduct/(math.sqrt(denominatorProduct1)*math.sqrt(denominatorProduct2))</div></blockquote><div><br></div><div>This will again speed up but the total execution time is about 200 minutes with +900 users.</div><div><br></div><div>3. Is there any reason not to use NumPy array? Using NumPy it finishes less than a fraction of a minute. Notice I also fixed the bug in the nominator and the denominator.</div><div><br></div><div><blockquote type="cite">import numpy as np<br>nitems = max(AverageRatingsOfItems.keys())<br>nusers = max([key[0] for key in PQ_Ratings.keys()])<br>avg_rating = np.zeros(nitems)<br>pq_rating = np.zeros((nusers, nitems))<br>keys = PQ_Ratings.keys()<br>for key in keys:<br> pq_rating[key[0]-1, key[1]-1] = PQ_Ratings[key]<br>keys = AverageRatingsOfItems.keys()<br>for key in keys:<br> avg_rating[key-1] = AverageRatingsOfItems[key]<br><br>startTime = time.time( )<br><br>#### Let's finish building up our similarity matrix for this problem.<br>keys = PQ_Ratings.keys()<br>for i in range(1, len(SimilarityMatrix)):<br> #sum1 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == i])<br> diff1 = np.sum(pq_rating[i-1] - avg_rating)<br><br> for j in range(i + 1, len(SimilarityMatrix)):<br> #sum2 = sum([PQ_Ratings[key] - AverageRatingsOfItems[key[1]] for key in keys if key[0] == j])<br> diff2 = np.sum(pq_rating[j-1] - avg_rating)<br> numeratorProduct = np.sum(diff1*diff2)<br> denominatorProduct1 = np.sum(diff1**2)<br> denominatorProduct2 = np.sum(diff2**2)<br> SimilarityMatrix[i][j] = numeratorProduct/(math.sqrt(denominatorProduct1)*math.sqrt(denominatorProduct2))</blockquote><br></div><div><br></div><div><br></div><div><blockquote type="cite"><div><div class="h5"><div>On Nov 9, 2015, at 7:44 PM, Lewit, Douglas <<a href="mailto:d-lewit@neiu.edu" target="_blank">d-lewit@neiu.edu</a>> wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr"><font size="4">Hey guys,</font><div><font size="4"><br></font></div><div><font size="4">I need some advice on this one. I'm attaching the homework assignment so that you understand what I'm trying to do. I went as far as the construction of the Similarity Matrix, which is a matrix of Pearson correlation coefficients.</font></div><div><font size="4"><br></font></div><div><font size="4">My problem is this. u1.base (which is also attached) contains Users (first column), Items (second column), Ratings (third column) and finally the time stamp in the 4th and final column. (Just discard the 4th column. We're not using it for anything. )</font></div><div><font size="4"><br></font></div><div><font size="4">It's taking HOURS for Python to build the similarity matrix. So what I did was:</font></div><div><font size="4"><br></font></div><div><font size="4"><b>head -n 5000 u1.base > practice.base</b></font></div><div><font size="4"><br></font></div><div><font size="4">and I also downloaded the PyPy interpreter for Python 3. Then using PyPy (or pypy or whatever) I ran my program on the first ten thousand lines of data from u1.base stored in the new text file, practice.base. Not a problem!!! I still had to wait a couple minutes, but not a couple hours!!! </font></div><div><font size="4"><br></font></div><div><font size="4">Is there a way to make this program work for such a large set of data? I know my program successfully constructs the Similarity Matrix (i.e. similarity between users) for 5,000, 10,000, 20,000 and even 25,000 lines of data. But for 80,000 lines of data the program becomes very slow and overtaxes my CPU. (The fan turns on and the bottom of my laptop starts to get very hot.... a bad sign! )</font></div><div><font size="4"><br></font></div><div><font size="4">Does anyone have any recommendations? ( I'm supposed to meet with my prof on Tuesday. I may just explain the problem to him and request a smaller data set to work with. And unfortunately he knows very little about Python. He's primarily a C++ and Java programmer. )</font></div><div><font size="4"><br></font></div><div><font size="4">I appreciate the feedback. Thank you!!!</font></div><div><font size="4"><br></font></div><div><font size="4">Best,</font></div><div><font size="4"><br></font></div><div><font size="4">Douglas Lewit</font></div><div><font size="4"><br></font></div><div><font size="4"><br></font></div></div>
</div></div><span><Homework3_Revision2.py></span><span><u1.base></span><span><practice2.base></span><span><HW3.pdf></span>_______________________________________________<br>Chicago mailing list<br><a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br><a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br></div></blockquote></div><br></div></div><br>_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org">Chicago@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/chicago" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
<br></blockquote></div><br></div>