1

I would like to compute a kernel matrix in python in the fastest way as possible: the input is a matrix X= nsamples, nfeatues and the output should be a symmetric matrix D =nsamples, nsapmles

the method which I'm using right now, even though is based on iterators seems to be really slow do to the for loop... can anybody think to something better?

Thanks

my method so far is:

from itertools import combinations
def computeKernel(X,dlambda):
    nsamples=X.shape[0]
    D=numpy.zeros((nsamples,nsamples))
    for el in combinations(range(nsamples),2):
        i,j=el
        D[el]=quadraticChiDist(X[i,:],X[j,:])


    D=D+D.T
    D=numpy.exp(-dlambda*D/255)
    D=numpy.eye(D)+D    
    return D

where quadraticChiDist is the function that is evaluated for every possible pair of rows in X

1

2 Answers 2

1

You can half the running time by replacing the inner loop by

for i in range(nsamples):
    for j in range(i):
        D[i,j]=quadraticChiDist(X[i,:],X[j,:])
        D[j,i]=D[i,j]

Even if quadraticChiDist is not symmetric, that does not matter as you symmetrize you matrix by (did you forget to divide by 2 ?)::

D = D + D.T

For further speedup I would recommend to optimize the speed of quadraticChiDist.

Further I recommend http://cython.org/, especially http://docs.cython.org/src/tutorial/numpy.html. This gives you the speed of C in many cases.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, the combinations(range(nsamples),2) already returns an iterator to the upper triangular matrix indices,therefore the two methods are equivalent: timing the two version there is a small gain in the two for loops version ... I have no idea why but the gain is around 0.2 seconds on 10 rounds of the code on the same matrix (the 10 runs take 40.2 seconds))
Then you should look at improving the speed of qaudraticChiDist.
0

After some search I realized that probably the best solution is to use the pdist function in scipy. It implements several distance functions or you can pass a functor to compute the distance. However, this function is very fast (since it is implemented in c) for the provided distances, but unfortunately does not gain much for a passed functor. Indeed, in the latter case, it is basically equivalent to the suggested for loop solution in pure python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.