python – perfectly adjusted polynomial curve

I want to iteratively adjust a curve to the python data with the following approach:

  1. Adjust a polynomial curve (or any nonlinear approach)
  2. Rejection values> 2 standard deviation from the mean of the curve
  3. repeat steps 1 and 2 until all values ​​are within the confidence interval of the curve

I can adjust a polynomial curve as follows:

vals = array ([0.00441025, 0.0049001 , 0.01041189, 0.47368389, 0.34841961,
       0.3487533 , 0.35067096, 0.31142986, 0.3268407 , 0.38099566,
       0.3933048 , 0.3479948 , 0.02359819, 0.36329588, 0.42535543,
       0.01308297, 0.53873956, 0.6511364 , 0.61865282, 0.64750302,
       0.6630047 , 0.66744816, 0.71759617, 0.05965622, 0.71335208,
       0.71992683, 0.61635697, 0.12985441, 0.73410642, 0.77318621,
       0.75675988, 0.03003641, 0.77527201, 0.78673995, 0.05049178,
       0.55139476, 0.02665514, 0.61664748, 0.81121749, 0.05521697,
       0.63404375, 0.32649395, 0.36828268, 0.68981099, 0.02874863,
       0.61574739])
x_values ​​= np.linspace (0, 1, len (vals))
poly_degree = 3

coeffs = np.polyfit (x_values, vals, poly_degree)
poly_eqn = np.poly1d (coeffs)
y_hat = poly_eqn (x_values)

How to do steps 2 and 3?