我正在使用以下代码对我的数据运行统一过滤:
from scipy.ndimage.filters import uniform_filter
a = np.arange(1000)
b = uniform_filter(a, size=10)滤镜现在的工作方式就好像一个步幅被设置为// 2。如何调整代码使滤镜的步幅不是大小的一半?
发布于 2020-09-02 20:18:12
你似乎误解了uniform_filter在做什么。
在本例中,它创建了一个数组b,将每个a[i]替换为以a[i]为中心的大小为10的块的平均值。所以,就像这样:
for i in range(0, len(a)): # for the 1D case
b[i] = mean(a[i-10//2:i+10//2]请注意,这会尝试访问索引范围在0..1000之外的值。在默认情况下,uniform_filter假设位置0之前的数据只是位置0之后的数据的反映。在最后也是如此。
还要注意,b使用与a相同的类型。在a为整数类型的示例中,平均值也将计算为整数,这可能会导致一些精度损失。
以下是一些代码和图表来说明正在发生的事情:
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import uniform_filter
fig, axes = plt.subplots(ncols=2, figsize=(15,4))
for ax in axes:
if ax == axes[1]:
a = np.random.uniform(-1,1,50).cumsum()
ax.set_title('random curve')
else:
a = np.arange(50, dtype=float)
ax.set_title('values from 0 to 49')
b = uniform_filter(a, size=10)
ax.plot(a, 'b-')
ax.plot(-np.arange(0, 10)-1, a[:10], 'b:') # show the reflection at the start
ax.plot(50 + np.arange(0, 10), a[:-11:-1], 'b:') # show the reflection at the end
ax.plot(b, 'r-')
plt.show()

https://stackoverflow.com/questions/63688390
复制相似问题