Python - Best way to find the 1d center of mass in a binary numpy array
<p>Suppose I have the following Numpy array, in which I have one and only one continuous slice of <code>1</code>s:</p>

<pre><code>import numpy as np
x = np.array([0,0,0,0,1,1,1,0,0,0], dtype=1)
</code></pre>

<p>and I want to find the index of the 1D center of mass of the <code>1</code> elements. I could type the following:</p>

<pre><code>idx = np.where( x )[0]
idx_center_of_mass = int(0.5*(idx.max() + idx.min()))
# this would give 5
</code></pre>

<p>(Of course this would lead to rough approximation when the number of elements of the <code>1</code>s slice is even.)
Is there any better way to do this, like a computationally more efficient oneliner?</p>