Python 应用高斯函数后填充nan值

Python 应用高斯函数后填充nan值,python,gaussian,Python,Gaussian,我将scipy.signal import gaussian中的应用于一组点,以滚动方式平滑曲线 def smooth_gaussian(data,window,std): g = gaussian(window,std,sym=True) con = np.convolve(g/g.sum(),data,mode='valid') con_shift = np.r_[np.full(int(window*0.5),np.nan),con,np.full(int(wind

我将scipy.signal import gaussian中的
应用于一组点,以滚动方式平滑曲线

def smooth_gaussian(data,window,std):
    g = gaussian(window,std,sym=True)
    con = np.convolve(g/g.sum(),data,mode='valid')
    con_shift = np.r_[np.full(int(window*0.5),np.nan),con,np.full(int(window*0.5),np.nan)]
    return con_shift 
但是,执行此操作时,函数会将值从Begging和end偏移
std
-1个位置,并用NaN填充

smooth_gaussian(x, 5, 3)

x = [0.0,
 0.0185444808621514,
 0.04353375061397004,
 0.0,
 0.0,
 0.037009453627706586,
 -0.002574811929754943,
 -0.008514606925496021,
 -0.004525364338418344,
 0.01603344879624361,
 -0.008176977610078684,
 -0.01013245952744958,
 0.03996290965967278,
 0.004585506533521074,
 0.01120825808877464,
 -0.014854987339931331,
 0.027839959669720065,
 -0.01363659727385685,
 -0.009635505397613021,
 0.015952626842982574,
 0.0,
 -0.05723201212379214,
 -0.007780069509540553,
 0.004382874236289852,
 0.0,
 0.038106785772339016,
 0.0,
 0.0020824367482471916,
 0.009206907087771247,
 -0.005312729628236836,
 0.0003880320778995375,
 0.00336355496824854,
 0.0,
 0.0,
 -0.010513187775929366,
 -0.004263141180765177,
 4.0797273904902634e-05,
 0.0026873894750915357,
 0.04737815797347586,
 0.03626967474190855,
 -0.001326958533547213,
 0.006573790793200369,
 0.0,
 -0.012210432139001255,
 -0.005373057181818514,
 0.016644712998042103,
 -0.012265613043462619,
 0.0,
 0.11261448009585626,
 0.0,
 -0.012185874851013743,
 0.0005792221659147861,
 0.0,
 0.08345293709945158,
 0.0,
 -0.005246029136623375,
 0.0015148005522818237,
 0.001830695038247017,
 0.003687504607556841,
 0.0065507216003178395,
 0.00117405250448804,
 0.014311546898334035,
 0.017903880444956242,
 -0.006553897125751214,
 0.0,
 -0.07215579508846881,
 0.0,
 0.014181691708626792,
 0.017493159929742756,
 0.0,
 0.009550277653690674,
 0.0,
 -0.003356974394831732,
 0.0,
 0.0327208871241706,
 0.002795052678830963,
 -0.0037707485693199783,
 0.06211202416416171,
 0.0,
 0.015181750267714622,
 0.00334460827605957,
 -0.00021393411588840287,
 0.02503678667112154,
 0.0,
 0.0,
 -0.00466151928887153,
 0.06231556188921662,
 0.0,
 0.0,
 0.0,
 -0.11946455099766087,
 0.004232213476864264,
 -0.015338758071829633,
 0.0,
 -0.018906328574624865]
结果是:

array([            nan,             nan,             nan,  2.10349055e-02,
    2.35864806e-03,  1.73461936e-02,  1.59561400e-02, -4.26660062e-03,
   -5.73985110e-03,  4.92612899e-03,  3.27209493e-03, -6.97831701e-03,
    1.39369457e-02,  2.09944748e-02,  8.16964809e-03, -7.64435525e-04,
    6.03942428e-03,  5.97089401e-03, -9.67266366e-03,  2.57063885e-03,
    5.54861712e-03, -2.66962686e-02, -3.04662584e-02, -3.26920778e-03,
    2.94363128e-03,  1.80616142e-02,  1.79964811e-02,  2.36393111e-03,
    5.16194246e-03,  1.90536838e-03, -1.94961572e-03,  1.61117216e-03,
    1.59253745e-03, -2.09649704e-04, -5.07213168e-03, -6.95261269e-03,
   -2.21077837e-03,  2.54849096e-03,  2.46239791e-02,  3.94073221e-02,
    1.80239542e-02,  3.53458603e-03,  2.70115693e-03, -5.70966673e-03,
   -7.78881961e-03,  4.58850968e-03,  1.91504673e-03, -1.99075206e-03,
    5.26407851e-02,  5.26412919e-02, -2.42272310e-03, -5.44020155e-03,
    2.35800108e-03,  3.92900195e-02,  3.91217709e-02,  1.76058419e-05,
   -1.69258247e-03,  1.52960978e-03,  2.83255426e-03,  4.90808043e-03,
    4.16438980e-03,  8.00317308e-03,  1.50047039e-02,  5.75233874e-03,
   -4.67031944e-03, -3.41464738e-02, -3.35421047e-02,  5.07473061e-03,
    1.48998963e-02,  8.92716005e-03,  5.00770626e-03,  4.39834755e-03,
   -1.29671612e-03, -6.21006659e-04,  1.53823996e-02,  1.66109984e-02,
    2.31579756e-03,  2.75440728e-02,  2.95660756e-02,  9.05984198e-03,
    8.72256686e-03,  2.65024164e-03,  1.17818429e-02,  1.17765298e-02,
    6.03242779e-04, -3.67528126e-04,  2.71334365e-02,  2.91908596e-02,
    1.80942572e-03, -3.48822318e-03, -5.61009027e-02, -5.46800643e-02,
   -8.72490815e-03,             nan,             nan,             nan])
我不想用零替换这些值,而是用高斯曲线正态分布的正确值。有可能这样做吗


亲切问候

试试
np.用
mode='same'进行卷积

from scipy.signal import gaussian

def smooth_gaussian(data,window,std): 
    g = gaussian(window, std, sym=True) 
    con = np.convolve(g/g.sum(), data, mode='same') 
    return con
制作测试数据(
s0
,蓝色),添加噪声(
s1
,橙色),并使其平滑(
con
,绿色):

没有nan:

In [71]: con[:10]                                                               
Out[71]: 
array([0.64101286, 0.85269195, 1.03414318, 1.05930853, 1.08433718,
       1.08190929, 1.08155865, 1.04812688, 1.02173964, 0.99050452])

In [72]: con[-10:]                                                              
Out[72]: 
array([-0.0148936 , -0.01155961, -0.03126125,  0.0143855 ,  0.03601306,
        0.05637481,  0.05888604,  0.064001  ,  0.04089501,  0.0207726 ])

您能发布所有代码吗?现在还不清楚你光滑的皮肤里有什么。对于smooting,您也可以使用,您可以为其显式设置边缘模式{'reflect','constant','nearest','mirror','wrap'},可选抱歉,已编辑。还补充说,我需要在一个滚动窗口的方式这一点。窗口大小必须为5。您应该自己定义正确的边缘行为<代码>数据
可能被零、平均值或角值包围,例如
数据=np.r\np.full(int(window*0.5),data[0]),data,np.full(int(window*0.5),data[-1])
或者您可以开发更适合您的其他内容。谢谢您的回复,我想我已经收到了。然而,从视觉上看,结果并不十分令人信服。我不知道为什么我申请的平滑效果不好:\
In [71]: con[:10]                                                               
Out[71]: 
array([0.64101286, 0.85269195, 1.03414318, 1.05930853, 1.08433718,
       1.08190929, 1.08155865, 1.04812688, 1.02173964, 0.99050452])

In [72]: con[-10:]                                                              
Out[72]: 
array([-0.0148936 , -0.01155961, -0.03126125,  0.0143855 ,  0.03601306,
        0.05637481,  0.05888604,  0.064001  ,  0.04089501,  0.0207726 ])