擅长:python、mysql、java
<p>您可以使用<a href="https://www.python.org/dev/peps/pep-0289/" rel="nofollow">generator
expression</a>来创建迭代器
它可以遍历<code>linregress</code>调用序列。因为它是一个
一次通过迭代器,对<code>linregress</code>的调用将延迟到必要时。你知道吗</p>
<p>然后可以使用<a href="https://docs.python.org/2/library/itertools.html#itertools.dropwhile" rel="nofollow">^{<cd3>}</a>在生成器表达式上迭代,删除结果直到<code>R**2</code>值大于0.995:</p>
<pre><code>import scipy.stats as stats
import itertools as IT
regressions = (stats.linregress([df['p_rel'].iloc[:-i], df['y_BET'].iloc[:-i])
for i in range(len(df)))
slope, intercept, r_value, p_value, std_err = next(IT.dropwhile(
lambda x: x[2]**2 < 0.995, regressions))
</code></pre>
<p>也可以将其包装到函数中:</p>
<pre><code>def regress_dropping_tail_outliers(x, y, threshold=0.995):
regressions = (stats.linregress([x.iloc[:-i], y.iloc[:-i]) for i in range(len(x)))
return next(IT.dropwhile(lambda x: x[2]**2 < threshold, regressions))
slope, intercept, r_value, p_value, std_err = regress_dropping_tail_outliers(
df['p_rel'], df['y_BET'])
</code></pre>