Note that there are some explanatory texts on larger screens.

plurals
  1. POefficient algorithm instead of looping
    primarykey
    data
    text
    <p>I have a data set where each samples has a structure similar to this</p> <pre><code>X=[ [[],[],[],[]], [[],[]] , [[],[],[]] ,[[][]]] </code></pre> <p>for example:</p> <pre><code>X=np.array([ [ [1,2,3], [2,4,5] ,[2,3,4] ] , [ [5,6], [6,6] ] , [[2,3,1],[2,3,10],[23,1,2],[1,4,5]] ] ,"object") Y=np.array([ [ [12,14,15] ,[12,13,14] ] , [ [15,16], [16,16] ] , [[22,23,21],[32,33,11],[12,44,55]] ] ,"object") </code></pre> <p>so for <strong>every sample</strong> I need to calculate the dot product between every element of x with corresponding element of y of same index and sum the result. i.e:</p> <pre><code>result=0 for i in range(3): for n,m in itertools.product(X[i],Y[i]): print "%s, %s" % (n,m) result+=np.dot(n,m) .....: [1, 2, 3], [12, 14, 15] [1, 2, 3], [12, 13, 14] [2, 4, 5], [12, 14, 15] [2, 4, 5], [12, 13, 14] [2, 3, 4], [12, 14, 15] [2, 3, 4], [12, 13, 14] [5, 6], [15, 16] [5, 6], [16, 16] [6, 6], [15, 16] [6, 6], [16, 16] [2, 3, 1], [22, 23, 21] [2, 3, 1], [32, 33, 11] [2, 3, 1], [12, 44, 55] [2, 3, 10], [22, 23, 21] [2, 3, 10], [32, 33, 11] [2, 3, 10], [12, 44, 55] [23, 1, 2], [22, 23, 21] [23, 1, 2], [32, 33, 11] [23, 1, 2], [12, 44, 55] [1, 4, 5], [22, 23, 21] [1, 4, 5], [32, 33, 11] [1, 4, 5], [12, 44, 55] </code></pre> <p>This is my whole code:</p> <pre><code> print "***build kernel***" K = np.zeros((n_samples, n_samples)) for i in range(n_samples): for j in range(n_samples): K[i,j] = self.kernel(X[i], X[j]) def kernel(x1,x2): N=8 #number of objects result=0 for i in xrange(N): for n,m in itertools.product(x1[i],x2[i]): result+=np.dot(n,m) return result </code></pre> <p>as you can see the complexity of this algorithm is too high and also my samples are much bigger than this. so for even a small data set, i.e. contains 400 samples, I have to wait 4 hours to get the result. I am looking for a better way to implement this algorithm. P.S: I was thinking about multithreading or multiproccessing but I am not sure if it helps?!</p> <p>I appreciate any suggestion!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload