Looking for a way to adjust the values of one array based on another array?
<p>I started with a set of bivariate data. My goal is to first find points in that data set for which the y-values are outliers. Then, I wanted to create a new data set that included not only the outlier points, but also any points with an x-value of within 0.01 of any given outlier point.</p>

<p>Then (if possible) I want to subtract the original outlier x-values from the new x-set, so that I have a group of points with x-values of between -0.01 and 0.01, with x-value now indicating distance from an original outlier x-value.</p>

<p>I have this code:</p>

<pre><code>import numpy as np

mean = np.mean(y)
SD = np.std(y)

x_indices = [i for i in range(len(y)) if ((y[i]) &gt; ((2*SD)+mean))]

expanded_indices = [i for i in range(len(x)) if np.any((abs(x[i] - x[x_indices])) &lt; 0.01)]
</code></pre>

<p>This worked great, and now I can call (and plot) x and y using the indices:</p>

<pre><code>plt.plot(x[expanded_indices],y[expanded_indices])
</code></pre>

<p>However, I have no idea how to subtract the original "x_indices" values to get an x range of -0.01 to 0.01, since everything I tried failed.</p>

<p>I want to do something like what I have below, except I know that I can't subtract two arrays of different sizes, and I'm worried I can't use np.any in this context either.</p>

<pre><code>x_values = [(x[expanded_indices] - x[indices]) if np.any((abs(x[expanded_indices] - x[indices])) &lt; 0.01)]
</code></pre>

<p>Any ideas? I'm sorry this is so long -- I'm very new at this and pretty lost. I've been giving it a go for the last few hours and any assistance would be appreciated. Thanks!</p>

<p>sample data could be as follows:
x =[0,0.994,0.995,0.996,0.997,0.998,1.134,1.245,1.459,1.499,1.500,1.501,2.103,2.104,2.105,2.106]</p>

<p>y =
[1.5,1.6,1.5,1.6,10,1.5,1.5,1.5,1.6,1.6,1.5,1.6,1.5,11,1.6,1.5]</p>

<p>Let's see if I understood you correctly. This code should find the outliers, and put an array into res for each outlier.</p>

<pre><code>import numpy as np

mean = np.mean(y)
SD = np.std(y)

x = np.array([0,0.994,0.995,0.996,0.997,0.998,1.134,1.245,1.459,1.499,1.500,1.501,2.103,2.104,2.105,2.106])
y = np.array([1.5,1.6,1.5,1.6,10,1.5,1.5,1.5,1.6,1.6,1.5,1.6,1.5,11,1.6,1.5])

outlier_indices = np.abs(y - mean) &gt; 2*SD

res = []
for x_at_outlier in x[np.flatnonzero(outlier_indices)]:
    part_res = x[np.abs(x - x_at_outlier) &lt; 0.01]
    part_res -= np.mean(part_res)
    res.append(part_res)
</code></pre>

<p><code>res</code> is now a list of arrays, with each array containing the values around one outlier. Perhaps it is easier to continue working with the data in this format?</p>

<p>If you want all of them in one numpy array:</p>

<pre><code>res = np.hstack(res)
</code></pre>

python arrays numpy indexing