Tensor-Like Outer Product Question
I am trying to perform the following operation: X is an m by n matrix, and I want to store outer products of the form Y[i] = numpy.outer(X[i,:], X[i,:]), leading to the relation Y[i,j,k] = X[i,j]*X[i,k] for i = 0,...,m-1; j,k = 0,...,n-1. I am trying to think of how to do this using tensordot, but so far I am finding no inspiration. Some far, my only solution has been to loop over i Y = numpy.empty([m,n,n]) for i in range(m): Y[i] = numpy.outer(X[i,:], X[i,:]) but this is fairly slow as for my dataset, m is of order 10^7 or 10^8 and n is around 20. Any help on how to vectorize/tensorize this operation to avoid the for loop would be much appreciated. -- View this message in context: http://old.nabble.com/Tensor-Like-Outer-Product-Question-tp29339226p29339226... Sent from the Numpy-discussion mailing list archive at Nabble.com.
2010/8/3 matt_in_nyc <mkraning@gmail.com>:
X is an m by n matrix, and I want to store outer products of the form Y[i] = numpy.outer(X[i,:], X[i,:]), leading to the relation Y[i,j,k] = X[i,j]*X[i,k] for i = 0,...,m-1; j,k = 0,...,n-1. I am trying to think of how to do this using tensordot, but so far I am finding no inspiration.
What about: X[:, :, numpy.newaxis] * X[:, numpy.newaxis, :] ? untested. Friedrich
participants (2)
-
Friedrich Romstedt
-
matt_in_nyc