Thursday, 15 April 2010

python - Compute "distance matrix" -


I will try to use a simple example that I am after.

  aGrid = Np.arange (1,9) bGrid = np.arange (101, 109, 0.5) a, b = np. Mesegrid (Agrid, BGRID, Indexing = 'IJ') N.P.Ramand.Sed (66) Valid = NP .random.choice ([True, False], Ashp)  

The form of matrix I think about the valid which determines whether you "grid point (a, b) or not. If you do not have permission to stay there, then you b will have to proceed further: you need to move to the left (with

I try to create this transition matrix Staying: For each item in this valid matrix, this "travel distance" determines that you need to reach the next true item. In the example, I have set the travel distance continuously with 0.5 B amplitude. If you have already created a < Code> true , your distance is 0 .

For the given seed it is valid

  array ([[[wrong, true, true, true, true, true, false, true, true, true, wrong, false, True, false, true], [true is true, true, true, false, true, false, true, true, true, true, true, false, true, false] [ True, false, false, false, true, true, true, false, false, true, false] true, true, false, false, true, false, false, false, true, False, false, false, true, false, false, true, false] true, true, true, true, true, true, false, true, False, false, false, false, false, false, true, false, false, true, true, false, true, false, true, false, false, false, false, true, false, false, True, false, true, false, true, true, false, false, true, false, true, false, true], [false, true, false, false, true, false, true, false, true, false, false , False, true, false, false]],  

some expected output

for the very first element, we can find the value of true In the left part Can not take more - default should be np.NaN . For the next 5 elements with the first line, the distance is 0 : they are already in valid places infection [0, 6] = 0.5 : it is left to an element left Need to move on.

Then, the first two rows

  array ([[NN, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5],  

I was trying to use a combination of np.argmax and the np.argmax is the "largest element which is true , But smaller than x , for each x , while every element is x in valid . < Strong> it is highly inefficient. What would be a better way to reach it?

Perhaps this is one of the vector Apart from this, besides, I can not rely on the homogeneity of 0.5 as given in this example. The approach requires calculating the distance between the current cell and Next legal entity by using (or b ).

It seems that this is the trick:

  transition = np.array (valid, dtype = float) for category i (valid. Size [0]): For the category J (valid. [1]): Infection [i, j] = 0 if valid [i, j] second infection [i, j-1] + bgrid [j] - BGrid [J-1] if j> 0 other np.NAN  

No comments:

Post a Comment