[Numpy-discussion] Any easy way to do this?
robert.kern at gmail.com
Wed Mar 9 10:42:19 EST 2011
On Wed, Mar 9, 2011 at 09:37, Neal Becker <ndbecker2 at gmail.com> wrote:
> Angus McMorland wrote:
>> On 9 March 2011 09:45, Neal Becker <ndbecker2 at gmail.com> wrote:
>>> given: w[i,j,k], y[l, k]
>>> d[l,i,j] = norm(w[i,j] - y[l])
>>> for each triple (l,i,j), w[i,j]-y[l] is a vector, of which I want to find the
>>> norm, and store into d[l,i,j]
>> Is something like this what you want to do?
>> w = np.random.randint(100, size=(4,5,6))
>> y = np.random.randint(100, size=(7,6))
>> norm = lambda x, axis: np.sqrt(np.sum(x**2, axis=axis))
>> d = norm(w[:,:,None] - y[None,None], -1)
> Thanks! Now if I could understand why
> w[:,:,None] - y[None,None] is what I needed...
np.newaxis is None. Idiomatically, that should be written
d = norm(w[:,:,np.newaxis] - y[np.newaxis,np.newaxis], -1)
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion