Re: [Tutor] Problem understanding the asarray function of numpy
On 9/11/2014 6:34 AM, Radhika Gaonkar wrote: I have an implementation of lsa, that I need to modify. I am having some trouble understanding the code. This is the line where I am stuck: DocsPerWord = sum(asarray(self.A > 0, 'i'), axis=1) Python doesn't provide an axis parameter for the sum builtin -- this sum function comes from numpy. See http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html for details. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem understanding the asarray function of numpy
Radhika Gaonkar wrote: > I have an implementation of lsa, that I need to modify. I am having some > trouble understanding the code. This is the line where I am stuck: > > DocsPerWord = sum(asarray(self.A > 0, 'i'), axis=1) > > The link for this implementation is : > http://www.puffinwarellc.com/index.php/news-and-articles/articles/33-latent-semantic-analysis-tutorial.html?showall=1 > > Here, A is a matrix of size vocabulary_Size X number_ofDocs > As far as the documentation of asarray is concerned, this should return an > array interpretation of the matrix A. But, we need to sum each row. What > is happening here? A numpy array can be multidimensional: >>> import numpy >>> a = numpy.asarray([[1,2,3],[4,5,6],[7,8,9]]) >>> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a.shape (3, 3) >>> a[0,0] 1 >>> a[2,2] 9 So this "array" works very much like a 3x3 matrix. Now let's investigate numpy.sum() (which must not be confused with the Python's built-in sum() function): >>> numpy.sum(a) 45 >>> numpy.sum(a, axis=0) array([12, 15, 18]) >>> numpy.sum(a, axis=1) array([ 6, 15, 24]) Playing around in interactive interpreter is often helpful to learn what a function or snippet of code does. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem understanding the asarray function of numpy
I have an implementation of lsa, that I need to modify. I am having some trouble understanding the code. This is the line where I am stuck: DocsPerWord = sum(asarray(self.A > 0, 'i'), axis=1) The link for this implementation is : http://www.puffinwarellc.com/index.php/news-and-articles/articles/33-latent-semantic-analysis-tutorial.html?showall=1 Here, A is a matrix of size vocabulary_Size X number_ofDocs As far as the documentation of asarray is concerned, this should return an array interpretation of the matrix A. But, we need to sum each row. What is happening here? Thanks! -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor