Take the 2-minute tour ×

I'm new to python, so I have some problems with the efficiency of my computation. I'm using this code to fill my H matrix and my h vector (x_tr, x_te and c are lists):

for l in xrange(0, b):
    for ls in xrange(0, b):
        H[l][ls] = 1.0/n_tr * numpy.sum([numpy.exp(-((numpy.linalg.norm(x_tr[i]-c[l])**2 + numpy.linalg.norm(x_tr[i]-c[ls])**2)/(2*s**2))) for i in range(0, n_tr)])
    h[l] = 1.0/n_te * numpy.sum([numpy.exp(-((numpy.linalg.norm(x_te[j]-c[l])**2)/(2*s**2))) for j in range(0, n_te)])

I think it might be inefficient to use 2 loops... Is there any easy way to speed my calculation up? I've been told, that I might use Vectorization, but I somehow don't know how this works

Thanks for your help :)

share|improve this question
What is unclear to you? Have you tried a numpy tutorial? –  Hans Then Sep 26 '12 at 9:11
Can I even use Vectorization to speed my calculation up? I tried some tutorials, but I didn't really understand, how I can refer their examples to my computation –  hukd321 Sep 26 '12 at 9:16
Yes, vectorization will speed up your calculation. I will try to post some help. –  Hans Then Sep 26 '12 at 9:34
Vectorization is removing loops from a calculation over a matrix and a vector or a matrix and another matrix. To understand how that is done, you, might want to read up on Linear Algebra. I see that someone else has already posted a code example. –  Hans Then Sep 26 '12 at 9:40
I think, you should first split up your calculation steps a bit more to make clear, what you want to do and to avoid confusion. Is it homework? –  bmu Sep 26 '12 at 11:06

1 Answer 1

Example of vectorization:

>>> x_te = np.arange(10)
>>> c = np.range(5)
>>> (x_te[:,None] - c).sum(axis=0)
array([45, 35, 25, 15,  5])

is equivalent to:

np.array([np.sum(xte[i]-c[j] for i in range(xte.size)) for j in range(c.size)])

That said:

as x_te[j] and c[l] are two scalars in your loops, your np.linalg.norm(x[j]-c[i])**2 is just (x[j]-c[i]), right ? So your h could be calculated as

h = 1.0/n_te * numpy.sum([numpy.exp(-(x_te[: None]-c))/(2*s**2))) 

Which should get you started for H...

EDIT You should probably check some documentation on broadcasting.

share|improve this answer
Thanks for your help! Using this code for h, I get this error: TypeError: unsupported operand type(s) for -: 'list' and 'list' –  hukd321 Sep 26 '12 at 9:48
Which tells you that you're using lists instead of numpy arrays as I showed you in the example: x_te and c should be arrays.. –  Pierre GM Sep 26 '12 at 9:50
ahh... yeah, you're right... I should've seen that... x_te[i] and c[j] are no scalars, they are dependend on the input dimensionality, that's why I've used numpy.linalg.norm(...) –  hukd321 Sep 26 '12 at 10:21
Ah. Then, if x_te[i] and c[j] are ndarrays, you can use the broadcasting tricks I showed you (the x_te[:,None]-c) with a bit of trial and error (unless yuo seriously update your question) –  Pierre GM Sep 26 '12 at 10:26

Your Answer


