python - 拟合后如何提取点的坐标?
问题描述
我正在使用以下代码来平滑我的数据
a = get_data()
y, x = a.T
t = np.linspace(0, 1, len(x))
t2 = np.linspace(0, 1, len(x))
x2 = np.interp(t2, t, x)
y2 = np.interp(t2, t, y)
sigma = 50
x3 = gaussian_filter1d(x2, sigma)
y3 = gaussian_filter1d(y2, sigma)
x4 = np.interp(t, t2, x3)
y4 = np.interp(t, t2, y3)
plt.plot(x, y, "o-", lw=2)
plt.plot(x3, y3, "r", lw=2)
plt.plot(x4, y4, "o", lw=2)
plt.show()
我在这里找到了这段代码:
python中的线平滑算法?
x
我的问题是我需要从新拟合中获得与原始值完全相同x
的点(我已经平滑的点)。拟合效果很好,但x
新点的值不同。如何从具有相同x
值但具有新拟合y
值的新拟合中获得积分。点的x
值从 0 开始,每个点之间的间距应为 1800。
解决方案
我认为您的情况特别需要平滑的数据就像平面中的一条自由线,(x, y) = f(t)
而不是一个函数y = f(x)
也许诀窍是必须在插值之前对点进行排序(参见 参考资料numpy.interp
):
# Generate random data:
t = np.linspace(0, 3, 20)
x = np.cos(t) + 0.1*np.random.randn(np.size(t))
y = np.sin(t) + 0.1*np.random.randn(np.size(t))
# Smooth the 2D data:
sigma = 2
x_smooth = gaussian_filter1d(x, sigma)
y_smooth = gaussian_filter1d(y, sigma)
# Sort (see: https://stackoverflow.com/a/1903579/8069403)
permutation = x_smooth.argsort()
x_smooth = x_smooth[permutation]
y_smooth = y_smooth[permutation]
x_new = np.sort(x) # not mandatory
# Interpolation on the original x points:
y_smooth_new = np.interp(x_new, x_smooth, y_smooth)
# Plot:
plt.plot(x, y, label='x, y');
plt.plot(x_smooth, y_smooth, label='x_smooth, y_smooth');
plt.plot(x_new, y_smooth_new, '-ro', label='x_new, Y_smooth_new', alpha=0.7);
plt.legend(); plt.xlabel('x');
推荐阅读
- python - 使用 python 计算 aws_volume_attachment ID
- python-3.x - 给定一个 1-d numpy array ,转换为所需形状的 2-d numpy array 附加 0
- reactjs - 处理多个 React 表单输入并更新组件的状态
- docker - VS 代码停止 docker 容器
- python-3.x - 没有任何 conda 或 pip 命令正在工作。如何在 anaconda 提示符中获取我的基本环境?
- python - 如何读取文本文件数据并在 python 中发送到电报机器人?
- python - 带有随机库和时间库的 Python 代码语法操作
- python - 使用 GradientTape 时 TensorFlow 不兼容的矩阵大小
- macos - Outlook 365 Mac,如何“核弹”邮件
- sql - 在 oracle 表中查找缺失的 ID