Python 填充numpy数组中具有相同值的非零元素之间的零
我有一个带整数的1D numpy numpy数组,其中我想用前一个非零值替换零,当且仅当下一个非零值相同时 例如,一个数组:Python 填充numpy数组中具有相同值的非零元素之间的零,python,arrays,numpy,vectorization,Python,Arrays,Numpy,Vectorization,我有一个带整数的1D numpy numpy数组,其中我想用前一个非零值替换零,当且仅当下一个非零值相同时 例如,一个数组: in: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1]) out: [1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1] 应该成为 out: [1,1,1,1,0,0,2,0,3,3,3,3,3,1,1,1] 有没有矢量化的方法可以做到这一点?我找到了一些填充零值的方法,但没有找到例外情况下的填充方法,即不使
in: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1])
out: [1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1]
应该成为
out: [1,1,1,1,0,0,2,0,3,3,3,3,3,1,1,1]
有没有矢量化的方法可以做到这一点?我找到了一些填充零值的方法,但没有找到例外情况下的填充方法,即不使用不同的值填充整数中的零。这是一种矢量化方法,其灵感来源于此解决方案中的正向填充部分以及
掩蔽和切片
def forward_fill_ifsame(x):
# Get mask of non-zeros and then use it to forward-filled indices
mask = x!=0
idx = np.where(mask,np.arange(len(x)),0)
np.maximum.accumulate(idx,axis=0, out=idx)
# Now we need to work on the additional requirement of filling only
# if the previous and next ones being same
# Store a copy as we need to work and change input data
x1 = x.copy()
# Get non-zero elements
xm = x1[mask]
# Off the selected elements, we need to assign zeros to the previous places
# that don't have their correspnding next ones different
xm[:-1][xm[1:] != xm[:-1]] = 0
# Assign the valid ones to x1. Invalid ones become zero.
x1[mask] = xm
# Use idx for indexing to do the forward filling
out = x1[idx]
# For the invalid ones, keep the previous masked elements
out[mask] = x[mask]
return out
样本运行-
In [289]: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,3,1,0,1])
In [290]: np.vstack((x, forward_fill_ifsame(x)))
Out[290]:
array([[1, 0, 1, 1, 0, 0, 2, 0, 3, 0, 0, 0, 3, 1, 0, 1],
[1, 1, 1, 1, 0, 0, 2, 0, 3, 3, 3, 3, 3, 1, 1, 1]])
In [291]: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,1,1,0,1])
In [292]: np.vstack((x, forward_fill_ifsame(x)))
Out[292]:
array([[1, 0, 1, 1, 0, 0, 2, 0, 3, 0, 0, 0, 1, 1, 0, 1],
[1, 1, 1, 1, 0, 0, 2, 0, 3, 0, 0, 0, 1, 1, 1, 1]])
In [293]: x = np.array([1,0,1,1,0,0,2,0,3,0,0,0,1,1,0,2])
In [294]: np.vstack((x, forward_fill_ifsame(x)))
Out[294]:
array([[1, 0, 1, 1, 0, 0, 2, 0, 3, 0, 0, 0, 1, 1, 0, 2],
[1, 1, 1, 1, 0, 0, 2, 0, 3, 0, 0, 0, 1, 1, 0, 2]])
你所说的例外是什么意思?你有非数值吗?我指的是iff语句,不能用不同的值填充整数中的零。啊哈,好吧,我读“异常”是指编程异常,而不是规则的异常。抱歉。@BramZijlstra相同元素之间总是只有一个零,即[1,0,1…,3,0,3,…,1,0,1]
?不,可能出现一个以上的零。@Divakar当我研究张量分解时,任务之一是分析人们通常用来链接页面的术语。这是在真实的数据集上完成的。而分析结果却不是那么好。因为,几乎总是有人用“在这里看”、“在这篇文章中”、“另一个博客”、“在这个链接上”等词,这些词都不那么有趣;因此,在进行超链接时,最好使用问题主题。即。“转发填充NaN值的最有效方法”,这将使链接标题更具信息性:),并且更美观well@Divakar,您总是用您理解看似复杂的需求并找到直接的、可能是最有效的解决方案的能力来激励我