我正在尝试在tensorflow框架中为一维张量实现类似ffill方法的熊猫。
我需要在张量中传播最后一个而不是NaN值:
[1,nan,nan,0,nan,nan,2,nan,3] => [1,1,1,0,0,0,2,2,3]
我用tf.scan尝试了此实现,但没有运气:
def ffill(previous_output, current_input):
condition = tf.equal(current_input,np.nan)
return tf.cond(condition,lambda: previous_output,lambda : current_input)
initializer = tf.constant(0.0)
filled_tensor = tf.scan(ffill, nan_tensor, initializer)
请帮助
答案 0 :(得分:4)
这是一种实现方法:
import tensorflow as tf
# Input data
input = tf.placeholder(tf.float32, [None])
# Find non-NaN values
mask = ~tf.is_nan(input)
# Take non-NaN values and precede them with a NaN
values = tf.concat([[math.nan], tf.boolean_mask(input, mask)], axis=0)
# Use cumsum over mask to find the index of the non-NaN value to pick
idx = tf.cumsum(tf.cast(mask, tf.int64))
# Gather values
result = tf.gather(values, idx)
# Test
with tf.Session() as sess:
input_test = [1, math.nan, math.nan, 0, math.nan, math.nan, 2, math.nan, 3]
print(sess.run(result, feed_dict={input: input_test}))
输出:
[1. 1. 1. 0. 0. 0. 2. 2. 3.]