def linregressIgnoringOutliers(x, y, n_iter=<NUM_LIT:3>, nstd=<NUM_LIT:2>):
for _ in range(n_iter):<EOL><INDENT>m, n = linregress(x, y)[:<NUM_LIT:2>]<EOL>y_fit = x * m + n<EOL>dy = y - y_fit<EOL>std = (dy**<NUM_LIT:2>).mean()**<NUM_LIT:0.5><EOL>inliers = abs(dy) < nstd * std<EOL>if inliers.sum() > <NUM_LIT:2>:<EOL><INDENT>x = x[inliers]<EOL>y = y[inliers]<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return linregress(x, y)<EOL>
do linear regression [n_iter] times
successive removing [outliers]
return result of normal linregress