擅长:python、mysql、java
<p>为什么不把它放到函数中呢?你知道吗</p>
<pre><code>def regress(df):
slope, intercept, r_value, p_value, std_err = stats.linregress(df['p_rel'], df['y_BET'])
if r_value ** 2 < 0.995:
regress(df.iloc[:-1, :]) # call again and regress with last row removed
else:
DO STUFF WITH RESULT
</code></pre>
<p>这种递归肯定会结束:我们将回归两列(两个<code>n x 1</code>结构),并递归地将其更新为<code>n-1, n-2, ..., 2</code>。它最迟在<code>2</code>停止,因为两个<code>2 x 1</code>结构上的回归保证R平方等于1(因此在if语句中求值为false)。你知道吗</p>
<p><strong>编辑:</strong>如果要在函数外使用结果(请参见注释),则此操作有效:</p>
<pre><code>def regress(df):
slope, intercept, r_value, p_value, std_err = stats.linregress(df['p_rel'], df['y_BET'])
if r_value ** 2 < 0.995:
return regress(df.iloc[:-1, :]) # call again and regress with last row removed
else:
return slope, intercept, r_value, p_value, std_err
# call like so:
slope, intercept, r_value, p_value, std_err = regress(df)
# use the results here
</code></pre>