---------- Forwarded message ---------- From: shu wei <[email protected]> Date: Wed, Mar 9, 2011 at 6:34 PM Subject: Re: [Numpy-discussion] How to sum weighted matrices To: Nicolas SCHEFFER <[email protected]>
Thanks very much. I just copied the code your guys gave me. Both of them work great I used to program in Matlab. not familiar with the array multiplication in python numpy. I have looked around for some basic explanation on this. However,I am still very confused with the array axes. Could someone explain a little bit about these? Many thanks in advance On Tue, Mar 8, 2011 at 11:29 AM, Nicolas SCHEFFER < [email protected]> wrote: > Or just with a dot: > > === > In [17]: np.tensordot(weights, matrices, (0,0)) > > Out[17]: > array([[ 5., 5., 5.], > [ 5., 5., 5.]]) > > In [18]: np.dot(matrices.T,weights).T > > Out[18]: > array([[ 5., 5., 5.], > [ 5., 5., 5.]]) > == > make matrices.T C_CONTIGUOUS for maximum speed. > > -n > > > On Mon, Mar 7, 2011 at 6:03 PM, shu wei <[email protected]> wrote: > > Thanks very much. It works. > > > > On Mon, Mar 7, 2011 at 11:53 AM, <[email protected]> wrote: > >> > >> for your problem, you can do: > >> > >> ---------------------------- > >> > >> import numpy as np > >> > >> weights = np.array([1,2]) > >> > >> matrix1 = np.ones((2,3)) > >> matrix2 = 2*np.ones((2,3)) > >> > >> matrices = np.array([matrix1,matrix2]) > >> > >> weighted_sum = np.tensordot(weights, matrices, (0,0)) > >> > >> -------------------------- > >> > >> On Mon, Mar 07, 2011 at 06:16:15AM -0600, shu wei wrote: > >> > Hello all, > >> > > >> > I am new to python and numpy. > >> > My question is how to sum up N weighted matrices. > >> > For example w=[1,2] (N=2 case) > >> > m1=[1 2 3, > >> > 3 4 5] > >> > > >> > m2=[3 4 5, > >> > 4 5 6] > >> > I want to get a matrix Y=w[1]*m1+w[2]*m2 by using a loop. > >> > > >> > My original problem is like this > >> > X=[1 2 3, > >> > 3 4 5, > >> > 4 5 6] > >> > > >> > a1=[1 2 3] 1st row of X > >> > m1=a1'*a1 a matirx > >> > a2=[3 4 5] 2nd row of X > >> > m2=a2'*a2 > >> > a3=[ 4 5 6] 3rd row of X > >> > m3=a3'*a3 > >> > > >> > I want to get Y1=w[1]*m1+w[2]*m2 > >> > Y2=w[1]*m2+w[2]*m3 > >> > So basically it is rolling and to sum up the weighted matries > >> > I have a big X, the rolling window is relatively small. > >> > > >> > I tried to use > >> > > >> > sq=np.array([x[i].reshape(-1,1)*x[i] for i in np.arange(0,len(x)]) > # > >> > s=len(x) > >> > m=np.array([sq[i:i+t] for i in np.arange(0,s-t+1)]) # t is the > len(w) > >> > > >> > then I was stuck, I tried to use a loop somethig like > >> > Y=np.array([np.sum(w[i]*m[j,i],axis=0) for i in np.arange(0,t)] ) > >> > Any suggestion is welcome. > >> > > >> > sue > >> > >> > _______________________________________________ > >> > NumPy-Discussion mailing list > >> > [email protected] > >> > http://mail.scipy.org/mailman/listinfo/numpy-discussion > >> > >> > >> -- > >> There are two things children should get > >> from their parents: roots and wings. > >> > >> The king who needs to remind his people of his rank, is no king. > >> > >> A beggar's mistake harms no one but the beggar. A king's mistake, > >> however, harms everyone but the king. Too often, the measure of > >> power lies not in the number who obey your will, but in the number > >> who suffer your stupidity. > >> _______________________________________________ > >> NumPy-Discussion mailing list > >> [email protected] > >> http://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > > > _______________________________________________ > > NumPy-Discussion mailing list > > [email protected] > > http://mail.scipy.org/mailman/listinfo/numpy-discussion > > > > >
_______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
