I have a tensorflow array names tf-array
and a numpy array names np_array
. I want to find specific rows in tf_array
with regards to np-array
.
tf-array = tf.constant(
[[9.968594, 8.655439, 0., 0. ],
[0., 8.3356, 0., 8.8974 ],
[0., 0., 6.103182, 7.330564 ],
[6.609862, 0., 3.0614321, 0. ],
[9.497023, 0., 3.8914037, 0. ],
[0., 8.457685, 8.602337, 0. ],
[0., 0., 5.826657, 8.283971 ]])
I also have an np-array:
np_array = np.matrix(
[[2, 5, 1],
[1, 6, 4],
[0, 0, 0],
[2, 3, 6],
[4, 2, 4]]
Now I want to keep the elements in tf-array
in which the combination of n
(here n is 2)
of them (index of them) is in the value of np-array
. What does it mean?
For example, in tf-array
, in the first column, indexes which has value are: (0,3,4)
. Is there any row in np-array
which contains any combination of these two indexes: (0,3), (0,4) or (3,4)
. Actually, there is no such row. So all the elements in that column became zero
.
Indexes for the second column in tf-array
is (0,1) (0,5) (1,5)
. As you see the record (1,5) is available in the np-array
in the first row. Thats why we keep those in the tf-array
.
So the final result should be like this:
[[0. 0. 0. 0. ]
[0. 8.3356 0. 8.8974 ]
[0. 0. 6.103182 7.330564 ]
[0. 0. 3.0614321 0. ]
[0. 0. 3.8914037 0. ]
[0. 8.457685 8.602337 0. ]
[0. 0. 5.826657 8.283971 ]]
I am looking for a very efficient approach as I have large number of data.
Update1
I could get this with the below code which is giving True
where there is value and the zero mask to false
:
[[ True True False False]
[False True False True]
[False False True True]
[ True False True False]
[ True False True False]
[False True True False]
[False False True True]]
with tf.Session() as sess:
where = tf.not_equal(tf-array, 0.0)
print(sess.run(where))
But how can I compare theese matrix with np_array
?
Thank you in advance!