用零填充不均匀分布点列表的缺失值

我将一组点保存为数据帧 dp:


xlist   ylist

0   0.017108    0.902494

1   0.019659    0.741981

2   0.030310    0.920884

3   0.032064    0.255826

4   0.046168    0.562761

5   0.060758    0.583044

6   0.118274    1.000000

7   0.125385    1.000000

8   0.140418    1.000000

9   0.153699    1.000000

10  0.186998    0.293743

11  0.215375    0.112288

12  0.217875    0.260883

13  0.250591    0.325953

14  0.262788    0.084916

15  0.287382    0.104910

16  0.325902    0.088418

17  0.377885    0.312025

18  0.473086    0.068632

19  0.485449    0.059624

20  0.557334    0.734376

21  0.572133    0.709392

22  0.610553    0.840687

23  0.626902    0.737930

24  0.630276    0.084787

25  0.637779    0.091535

26  0.717553    0.074411

27  0.742187    0.092770

28  0.757873    0.532881

29  0.780933    0.528202

30  0.836708    0.422615

31  0.920814    0.359896

32  0.938639    0.355241

33  0.954564    0.280989

34  0.978567    0.649749

35  0.995067    0.769272

使用 pyplot.vlines 时,它们如下所示:


[plt.vlines(x,0,y) for x,y in zip(dp.xlist, dp.ylist)]

plt.show()

https://img1.sycdn.imooc.com/654c82150001d07a05890294.jpg

我想用以下代码替换 xlist:


x = linspace(0,1,num=100) ###(or num=200... not important)

并使用 ylist 中的值创建一个新的 y,其中 x 接近 xlist,并在其他位置为零。

到目前为止,我尝试的是对于每对 xlist、ylist 值,我检查我的线性空间中是否有一个点足够接近 xlist 中的点,然后为其分配相应的 ylist 值,否则,我放一个零。


for i in dp.index:


    fill = [] 


    for xa in x:

        if abs(dp.xlist[i]-xa)<0.001:

            tmp = dp.ylist[i]

        else:

            tmp = 0

        fill.append(tmp)

但我想我正在覆盖列表“填充”,这就是为什么它不起作用,但我不知道如何解决这个问题。


有没有一种快速简单的方法可以实现这一目标?


慕标5832272
浏览 114回答 2
2回答

拉风的咖菲猫

无需循环。您可以使用 pandas 方法:dp['x_lin'] = x[np.abs(np.subtract.outer(x,dp.xlist.values)).argmin(0)]dp['y_lin'] = 0dp.y_lin[np.abs(dp.x_lin-dp.xlist)<0.001] = dp.ylist您也可以将第一行替换为等效行:dp['x_lin'] = x[np.abs(x[None,:]-dp.xlist[:,None]).argmin(1)]输出:&nbsp; &nbsp; &nbsp; &nbsp;xlist&nbsp; &nbsp; &nbsp;ylist&nbsp; &nbsp; &nbsp;x_lin&nbsp; &nbsp; &nbsp;y_lin0&nbsp; &nbsp;0.017108&nbsp; 0.902494&nbsp; 0.020202&nbsp; 0.0000001&nbsp; &nbsp;0.019659&nbsp; 0.741981&nbsp; 0.020202&nbsp; 0.7419812&nbsp; &nbsp;0.030310&nbsp; 0.920884&nbsp; 0.030303&nbsp; 0.9208843&nbsp; &nbsp;0.032064&nbsp; 0.255826&nbsp; 0.030303&nbsp; 0.0000004&nbsp; &nbsp;0.046168&nbsp; 0.562761&nbsp; 0.050505&nbsp; 0.0000005&nbsp; &nbsp;0.060758&nbsp; 0.583044&nbsp; 0.060606&nbsp; 0.5830446&nbsp; &nbsp;0.118274&nbsp; 1.000000&nbsp; 0.121212&nbsp; 0.0000007&nbsp; &nbsp;0.125385&nbsp; 1.000000&nbsp; 0.121212&nbsp; 0.0000008&nbsp; &nbsp;0.140418&nbsp; 1.000000&nbsp; 0.141414&nbsp; 1.0000009&nbsp; &nbsp;0.153699&nbsp; 1.000000&nbsp; 0.151515&nbsp; 0.00000010&nbsp; 0.186998&nbsp; 0.293743&nbsp; 0.191919&nbsp; 0.00000011&nbsp; 0.215375&nbsp; 0.112288&nbsp; 0.212121&nbsp; 0.00000012&nbsp; 0.217875&nbsp; 0.260883&nbsp; 0.222222&nbsp; 0.00000013&nbsp; 0.250591&nbsp; 0.325953&nbsp; 0.252525&nbsp; 0.00000014&nbsp; 0.262788&nbsp; 0.084916&nbsp; 0.262626&nbsp; 0.08491615&nbsp; 0.287382&nbsp; 0.104910&nbsp; 0.282828&nbsp; 0.00000016&nbsp; 0.325902&nbsp; 0.088418&nbsp; 0.323232&nbsp; 0.00000017&nbsp; 0.377885&nbsp; 0.312025&nbsp; 0.373737&nbsp; 0.00000018&nbsp; 0.473086&nbsp; 0.068632&nbsp; 0.474747&nbsp; 0.00000019&nbsp; 0.485449&nbsp; 0.059624&nbsp; 0.484848&nbsp; 0.05962420&nbsp; 0.557334&nbsp; 0.734376&nbsp; 0.555556&nbsp; 0.00000021&nbsp; 0.572133&nbsp; 0.709392&nbsp; 0.575758&nbsp; 0.00000022&nbsp; 0.610553&nbsp; 0.840687&nbsp; 0.606061&nbsp; 0.00000023&nbsp; 0.626902&nbsp; 0.737930&nbsp; 0.626263&nbsp; 0.73793024&nbsp; 0.630276&nbsp; 0.084787&nbsp; 0.626263&nbsp; 0.00000025&nbsp; 0.637779&nbsp; 0.091535&nbsp; 0.636364&nbsp; 0.00000026&nbsp; 0.717553&nbsp; 0.074411&nbsp; 0.717172&nbsp; 0.07441127&nbsp; 0.742187&nbsp; 0.092770&nbsp; 0.737374&nbsp; 0.00000028&nbsp; 0.757873&nbsp; 0.532881&nbsp; 0.757576&nbsp; 0.53288129&nbsp; 0.780933&nbsp; 0.528202&nbsp; 0.777778&nbsp; 0.00000030&nbsp; 0.836708&nbsp; 0.422615&nbsp; 0.838384&nbsp; 0.00000031&nbsp; 0.920814&nbsp; 0.359896&nbsp; 0.919192&nbsp; 0.00000032&nbsp; 0.938639&nbsp; 0.355241&nbsp; 0.939394&nbsp; 0.35524133&nbsp; 0.954564&nbsp; 0.280989&nbsp; 0.959596&nbsp; 0.00000034&nbsp; 0.978567&nbsp; 0.649749&nbsp; 0.979798&nbsp; 0.00000035&nbsp; 0.995067&nbsp; 0.769272&nbsp; 1.000000&nbsp; 0.000000输出图:

蝴蝶不菲

这看起来不错。是的,您正在覆盖每个 .但是您还为每个 xa 添加了 tmp。我不太清楚你的x是什么。但是尝试:ifill = []&nbsp;for i in dp.index:&nbsp; &nbsp; tmp = 0&nbsp; &nbsp; for xa in x:&nbsp; &nbsp; &nbsp; &nbsp; if abs(dp.xlist[i]-xa)<0.001:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = dp.ylist[i]&nbsp; &nbsp; fill.append(tmp)
打开App,查看更多内容
随时随地看视频慕课网APP