gpt4 book ai didi

python - 迭代拟合多项式曲线

转载 作者:太空狗 更新时间:2023-10-29 21:18:02 25 4
gpt4 key购买 nike

我想使用以下方法迭代地将曲线拟合到 python 中的数据:

  1. 拟合多项式曲线(或任何非线性方法)
  2. 丢弃值 > 曲线平均值的 2 个标准差
  3. 重复步骤 1 和 2,直到所有值都在曲线的置信区间内

我可以如下拟合多项式曲线:

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)

如何执行第 2 步和第 3 步?

最佳答案

由于消除点距离预期的解决方案太远,您可能正在寻找 RANSAC (随机样本共识),即 fitting a curve (或任何其他功能)到特定范围内的数据,例如您使用 2*STD 的情况。

您可以使用 scikit-learn RANSAC与包含的回归变量(例如 LinearRegression)非常一致的估计量.对于多项式情况,您需要定义自己的回归类:

from sklearn.metrics import mean_squared_error
class PolynomialRegression(object):
def __init__(self, degree=3, coeffs=None):
self.degree = degree
self.coeffs = coeffs

def fit(self, X, y):
self.coeffs = np.polyfit(X.ravel(), y, self.degree)

def get_params(self, deep=False):
return {'coeffs': self.coeffs}

def set_params(self, coeffs=None, random_state=None):
self.coeffs = coeffs

def predict(self, X):
poly_eqn = np.poly1d(self.coeffs)
y_hat = poly_eqn(X.ravel())
return y_hat

def score(self, X, y):
return mean_squared_error(y, self.predict(X))

然后你可以使用 RANSAC

from sklearn.linear_model import RANSACRegressor
ransac = RANSACRegressor(PolynomialRegression(degree=poly_degree),
residual_threshold=2 * np.std(y_vals),
random_state=0)
ransac.fit(np.expand_dims(x_vals, axis=1), y_vals)
inlier_mask = ransac.inlier_mask_

请注意,X 变量被转换为二维数组,因为它是 sklearn RANSAC 实现所需要的,并且在我们的自定义类中变平,因为 numpy polyfit 函数适用于一维数组。

y_hat = ransac.predict(np.expand_dims(x_vals, axis=1))
plt.plot(x_vals, y_vals, 'bx', label='input samples')
plt.plot(x_vals[inlier_mask], y_vals[inlier_mask], 'go', label='inliers (2*STD)')
plt.plot(x_vals, y_hat, 'r-', label='estimated curve')

visualisation of the poly-fitting

此外,使用多项式阶数和残差距离,我得到了度数 = 4 和范围 1*STD 的以下结果

enter image description here

另一种选择是使用高阶回归器,如 Gaussian process

from sklearn.gaussian_process import GaussianProcessRegressor
ransac = RANSACRegressor(GaussianProcessRegressor(),
residual_threshold=np.std(y_vals))

谈到对 DataFrame 的泛化,您只需要设置除一列之外的所有列都是特征,剩下的一列是输出,如下所示:

import pandas as pd
df = pd.DataFrame(np.array([x_vals, y_vals]).T)
ransac.fit(df[df.columns[:-1]], df[df.columns[-1]])
y_hat = ransac.predict(df[df.columns[:-1]])

关于python - 迭代拟合多项式曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55682156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com