MATCH function in python?
<p>Is there a way to do an Excel match() function in Python, such that:</p>

<p>In a graph like this  </p>

<p><img src="http://i.stack.imgur.com/O3oiF.png" alt="K-Means Elbow Method results">...</p>

<p>...wherein I cut-off at y = 90, I would like to print which corresponding x value is the closest.</p>

<p>Based on my investigation the value/answer should be 4, but how can I possibly print or store this in a variable? </p>

<pre><code>In: print(bss/tss*100)

Out: [  1.21976032e-14   7.42743185e+01   8.51440985e+01   9.21584826e+01
        9.59771981e+01   9.74117561e+01   9.82980987e+01   9.90505760e+01
        9.92982678e+01   9.94756800e+01   9.96396123e+01   9.97126077e+01
        9.97593424e+01   9.98030600e+01   9.98344280e+01   9.98692896e+01
        9.98840717e+01   9.99020097e+01   9.99142963e+01]
</code></pre>

<p>You are looking for an <em>argmin</em> function.</p>

<pre><code>from numpy import *

print argmin(abs(data - threshold))
</code></pre>

<p>will find the <em>index</em> of the smallest deviation from the threshold. It's much more precise to specify the math relationship (<code>argmin(abs(...))</code>) than to hide this in a vague "match" function. This way, it is clear how to use e.g. two thresholds (an upper and a lower threshold), or a non-constant threshold function. We can find the closest point of <em>two</em> functions the same way.</p>

python match cluster-analysis k-means