使用python中的scipy.interpolate,我想要删除特定数值并进行插值。

huangapple go评论57阅读模式
英文:

Using scipy.interpolate in python, I want to erase certain values and interpolate them

问题

在我的代码中,我有一些数值,如下所示:

my_list = [725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388, 801.612916, 799.38916, 809.280518, 809.186036, 811.899414, .... , 412.314528]

在我的代码中,我想要对列表中的0.0值进行插值,因为它们是异常值。但由于插值仅适用于空值,所以它无法正常工作。

我该如何删除这些0.0值并进行插值处理?

英文:

In my code, I have certain values like

my_list = [725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388, 801.612916, 799.38916, 809.280518, 809.186036, 811.899414, .... , 412.314528]

In my code, I want to interpolate the points where the list is 0.0 because they are outliers. But it is not working as interpolation only works for empty values.

How can I erase those 0.0 values and do interpolation?

答案1

得分: 5

使用您提供的数据,我们可以使用numpy创建一个“清理后”的x和y数据列表。您说数值等于0的值是异常值,但是使用浮点数进行相等性检查可能会导致问题,所以我使用了np.isclose。去除异常值后,您可以对清理后的数据进行插值。

import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt

plt.close("all")

y = np.array([725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388,
              801.612916, 799.38916, 809.280518, 809.186036, 811.899414, 
              412.314528])
x = np.arange(len(y))

outliers = np.isclose(y, 0)
y_clean = y[~outliers]
x_clean = x[~outliers]

spline = make_interp_spline(x_clean, y_clean)
y_interped = spline(x)

fig, ax = plt.subplots()
ax.plot(x_clean, y_clean, ".", label="cleaned", zorder=3)
ax.plot(x, y_interped, label="interpolated")
ax.legend()
ax.set_xlabel("x")
ax.set_ylabel("y")
fig.show()

如果像@Reinderien建议的那样,您的实际条件是值小于100,例如,被视为异常值,那么您可以将条件更改为outliers = y < 100

英文:

Using the data you provided, we can create a "cleaned" list of the x and y data using numpy. You said that the values equal to 0 are the outliers, but checking equality with floating point numbers can lead to issues, so I used np.isclose. With the outliers removed, you can interpolate the cleaned data.

import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt

plt.close(&quot;all&quot;)

y = np.array([725.998474, 0.0, 0.0, 0.0, 0.0, 789.507934, 792.585388,
              801.612916, 799.38916, 809.280518, 809.186036, 811.899414, 
              412.314528])
x = np.arange(len(y))

outliers = np.isclose(y, 0)
y_clean = y[~outliers]
x_clean = x[~outliers]

spline = make_interp_spline(x_clean, y_clean)
y_interped = spline(x)

fig, ax = plt.subplots()
ax.plot(x_clean, y_clean, &quot;.&quot;, label=&quot;cleaned&quot;, zorder=3)
ax.plot(x, y_interped, label=&quot;interpolated&quot;)
ax.legend()
ax.set_xlabel(&quot;x&quot;)
ax.set_ylabel(&quot;y&quot;)
fig.show()

使用python中的scipy.interpolate,我想要删除特定数值并进行插值。

If, as @Reinderien suggested, your actual condition is that values below 100, for example, are outliers, then you could change it to that condition (i.e. outliers = y &lt; 100).

huangapple
  • 本文由 发表于 2023年7月17日 15:09:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定