numpy:如何平滑数组列中的 x、y、z 值?

问题描述 投票:0回答:0

我有加速度计数据——为了简单起见,只是加速度——像这样加载到一个 numpy 数组中(玩具数据;列是 x、y、z):

import numpy as np
import pandas as pd
from scipy.interpolate import make_interp_spline, BSpline
idx = [1,2,3,4]
idx = np.array(idx,dtype=int)
accel = [[-0.7437,0.1118,-0.5367],
         [-0.5471,0.0062,-0.6338],
         [-0.7437,0.1216,-0.5255],
         [-0.4437,0.3216,-0.3255],
]
accel = np.array(accel,dtype=float)

我想平滑这些值。我在 here 中找到了一个关于如何使用 bspline 执行此操作的示例。问题是该示例使用一维数组。我在 Python 方面还不够好,无法弄清楚如何为 2D 数组重现它。我可以提取 x 的列切片并像这样平滑它:

x = accel[:,0]
idxnew = np.linspace(idx.min(), idx.max(), 200) 
spl = make_interp_spline(idx, x, k=3)
x_smooth = spl(idxnew)

但我不确定如何将其写回新的、更长的数组的第一列。我试过了

accel_sm = []
accel_sm = np.array(idx,dtype=float)
np.hstack(accel_sm,x_smooth)

但这会抛出“TypeError: expected bytes-like object, not str”。我有点卡住了。谁能指出我正确的方向?

python arrays numpy spline
© www.soinside.com 2019 - 2024. All rights reserved.