質問

I know that this question party has been answered, but I am looking specifically at numpy and scipy. Say I have a grid

lGrid = linspace(0.1, 8, 50)

and I want to find the index that corresponds best to 2, I do

index = abs(lGrid-2).argmin()
lGrid[index]
2.034

However, what if I have a whole matrix of values instead of 2 here. I guess iteration is pretty slow. abs(lGrid-[2,4]) however will fail due to shape issues. I will need a solution that is easily extendable to N-dim matrices. What is the best course of action in this environment?

役に立ちましたか?

解決

You can use broadcasting:

from numpy import arange,linspace,argmin
vals = arange(30).reshape(2,5,3) #your N-dimensional input, like array([2,4])
lGrid = linspace(0.1, 8, 50)
result = argmin(abs(lGrid-vals[...,newaxis]),axis=-1)

for example, with input vals = array([2,4]), you obtain result = array([12, 24]) and lGrid[result]=array([ 2.03469388, 3.96938776])

他のヒント

You "guess that Iteration is pretty slow", but I guess it isn't. So I would just just iterate over the "whole Matrix of values instead of 2". Perhaps:

 for val in BigArray.flatten():
    index = abs(lGrid-val).argmin()
    yield lGrid[index]

If lGrid is failry large, then the overhead of iterating in a Python for loop is probably not big in comparison to the vecotirsed operation Happening inside it.

There might be a way you can use broadcasting and reshaping to do the whole thing in one giant operation, but would be complicated, and you might accidentally allocate such a huge array that your machine slows down to a crawl.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top