<h2>解决方案</h2>
<pre class="lang-py prettyprint-override"><code>data[~np.isin(data, outliers).all(axis=1)]
</code></pre>
<h2>台阶</h2>
<p>从以下两个数据帧开始:</p>
<pre class="lang-py prettyprint-override"><code>In [3]: data
Out[3]:
x y
0 0.0 0.998440
1 1.0 2.188544
2 4.0 7.572174
3 7.0 6.138442
4 11.0 11.737930
5 0.0 1.043314
6 1.0 1.733181
7 4.0 7.424136
8 7.0 6.138442
9 11.0 9.737930
In [4]: outliers
Out[4]:
x y
0 4.0 7.572174
1 7.0 6.138442
2 11.0 11.737930
3 4.0 7.424136
4 7.0 6.138442
</code></pre>
<p>我们可以使用<code>np.isin()</code>函数检查<code>data</code>中的任何行是否有与<code>outliers</code>中的任何行匹配的值:</p>
<pre class="lang-py prettyprint-override"><code>In [5]: np.isin(data, outliers)
Out[5]:
array([[False, False],
[False, False],
[ True, True],
[ True, True],
[ True, True],
[False, False],
[False, False],
[ True, True],
[ True, True],
[ True, False]])
</code></pre>
<p>因为我们需要完全匹配(x坐标和y坐标),所以沿第一个轴(跨列)使用<code>all()</code>:</p>
<pre class="lang-py prettyprint-override"><code>In [6]: np.isin(data, outliers).all(axis=1)
Out[6]:
array([False, False, True, True, True, False, False, True, True,
False])
</code></pre>
<p>这个布尔掩码告诉我们哪些行与异常值完全匹配。我们需要做的就是反转掩码(因为我们想要过滤异常值),并使用该掩码索引到<code>data</code>:</p>
<pre class="lang-py prettyprint-override"><code>In [7]: data[~np.isin(data, outliers).all(axis=1)]
Out[7]:
x y
0 0.0 0.998440
1 1.0 2.188544
5 0.0 1.043314
6 1.0 1.733181
9 11.0 9.737930
</code></pre>
<p>从那里,您可以使用<code>x</code>和<code>y</code>列执行任何您喜欢的操作</p>
<h2>替代解决方案,从1D阵列开始</h2>
<p>如果您有单独的1D数组<code>X</code>和<code>y</code>,并且离群值也是如此,您可以将它们压缩到元组中,并将它们添加到集合中,然后从集合中减去离群值:</p>
<pre class="lang-py prettyprint-override"><code>points = set(zip(X, y))
outliers = set(zip(outlier_x, outlier_y))
X_new, y_new = zip(*(points - outliers))
</code></pre>