简体   繁体   English

带有不需要的零的python插值

[英]python interpolation with undesired zeros

I have sparse list of data. 我的数据清单很少。 There are approximately 20 to 50 zero values between every two adjacent non-zeros (these are the meaningful values of my data). 每两个相邻的非零之间大约有20到50个零值(这些是我的数据的有意义的值)。

I want to interpolate these non-zero values. 我想对这些非零值进行插值。 The result I get is undesired since python interpolates the majority zeros too, making the interpolated curve with a lot of sparks! 我得到的结果是不希望有的,因为python也会对多数零进行插值,使得插值曲线上有很多火花! I want something very smooth... 我想要一些很光滑的东西

The idea is to have python ignore the zeros or get rid of the zeros in the list, and still let the list remember the index of each non-zero value. 想法是让python忽略零或删除列表中的零,而仍然让列表记住每个非零值的索引。

I guess what I can do is simply interpolate between a list of index and a list of real values, does anyone know how to do it elegantly? 我想我能做的只是在索引列表和实数值列表之间进行插值,有人知道如何优雅地做到这一点吗?

You could create a list of tuples containing (index, value) and interpolate using only these values. 您可以创建一个包含(索引,值)的元组列表,并仅使用这些值进行插值。

data = [(i, val) for i, val in enumerate(sparse_list) if val != 0]

or considering primero´s answer probably use an ordered dict. 或考虑底漆的答案可能使用命令。

I'm not really sure I understand the question, but from what i understand I would think of something like this: 我不太确定我是否理解这个问题,但是根据我的理解,我会想到这样的事情:

a = [1,0,0,0,0,1,0,0,0,1] # initial array
b = { index : value for index, value in enumerate(a) if value != 0 }

And now you have a dictionary b with the former indexes in f.keys() and the non-zero values in f.values() 现在你有一个字典b相在f.keys)前指数f.values非零值()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM