optimization - Optimisation of a simple vector operation (python) -
I, j] = a [i] - b [j] gave two arrays a and b (of the same length, but this is probably not relevant)?
To be more precise, do I have N points in a 2-dimensional space, whose posts are stored in two arrays DX and D, and more digits of N, whose position in Tx and T I need a matrix
a [i, j] = (dx [j] -tx [i]) ** 2+ (dy [j] -ty [i]) ** 2
The only way I have thought that for the range (nData) I
A = np.empty ([nData, nData] In: A [I] = (dx-tx [i]) ** 2+ (DI-TE [ii]) ** 2 returns a
The problem is very slow (Ndita is going to grow)
(By the way, x * 2x * x or slower than equivalent?)
You want to calculate the Eclidian distance between all the pairs of your partner class. scipy.distance.cdist
:
& gt; & Gt; & Gt; Will be the fastest to use. Np & gt; & Gt; & Gt; CCSSA.A. Import from CDIST & gt; & Gt; & Gt; X = np.random.rand (10, 2) & gt; & Gt; & Gt; T = np.random.rand (8, 2) & gt; & Gt; & Gt; The cdist (x, t, 'sqeuclidean') array ([[0.6,10,48, 9 82, 0.0, 43, 9, 578, 0.3, 7, 63, 14], [0.0, 27,0 9, 455, 0.3,02,35,2 9 2, 0.251,35, 9 34], [0.2,12,4 9, 888, 0.1,40,24, 9 51, 0. 2,84,41,688], [0.3, 9,221,412, 0.0, 9, 9, 4, 213, 0.1, 76, 99, 23]]]
If you want to do it yourself in numpy. Something like this should happen:
& gt; & Gt; & Gt; Np.sum ((x [,, none] - t) ** 2, pivot = -1) array ([[0.6, 10, 48, 9 82, 0.0, 43, 9, 578, 0. 3,07,63,14 9], [0.0227,0 9, 455, 0.3,02,35,2 9 2, 0.251,35, 9 34], [0.2,12,4 9, 888, 0.1,40,24, 9 51, 0.284,41,688], [0.39221412, 0.01994213, 0.17699239]] or
Or, for the X and Y coordinates Using your own separate arrays:
>> Dx, dy = xT>>> tx, t = tt. & Gt; ;> (Dx [:, none] - TX) ** 2 + (DC [, none] - Ty) ** 2 array ([[0.6,10,48, 9 82, 0.04, 43, 9, 578, 0.3,07,63,14 9], [0.02, 9, 455, 0.3, 02, 35, 9 2, 0.25.135, 9 34], [0.2, 12, 9, 888, 0.1, 40, 24, 9 51, 0.28441688], [0.39221412, 0.01994213, 0.17699239]])
Comments
Post a Comment