Ali_Sh
Ali_Sh

Reputation: 2816

How to get Boolean matrix for similar lists in two different-size numpy arrays of lists

At first, I write a small example of to lists:

F = [[1,2,3],[3,2,7],[4,4,1],[5,6,3],[1,3,7]]          # (1*5)     5 lists
S = [[1,3,7],[6,8,1],[3,2,7]]                          # (1*3)     3 lists

I want to get Boolean matrix for the same 'list's in two F and S:

[False, True, False, False, True]                      #  (1*5)    5 Booleans for 5 lists of F

By using IM = reduce(np.in1d, (F, S)) it gives results for each number in each lists of F:

[ True  True  True  True  True  True False False  True False  True  True
  True  True  True]       # (1*15)

By using IM = reduce(np.isin, (F, S)) it gives results for each number in each lists of F, too, but in another shape:

[[ True  True  True]
 [ True  True  True]
 [False False  True]
 [False  True  True]
 [ True  True  True]]           # (5*3)

The true result will be achieved by code IM = [i in S for i in F] for the example lists, but when I'm using this code for my two main bigger numpy arrays of lists:

https://drive.google.com/file/d/1YUUdqxRu__9-fhE1542xqei-rjB3HOxX/view?usp=sharing

numpy array: 3036 lists

https://drive.google.com/file/d/1FrggAa-JoxxoRqRs8NVV_F69DdVdiq_m/view?usp=sharing

numpy array: 300 lists

It gives wrong answer. For the main files it must give 3036 Boolean, in which 'True' is only 300 numbers. I didn't understand why this get wrong answers?? It seems it applied only on the 3rd characters in each lists of F. It is preferred to use reduce function by the two functions, np.in1d and np.isin, instead of the last method. How could to solve each of the three above methods??

Upvotes: 0

Views: 6416

Answers (2)

Ali_Sh
Ali_Sh

Reputation: 2816

The problem have been solved by converting the inputs to list by 'tolist' with Both of IM = [i in S for i in F] and IM = [x for x in map(lambda m: m in S, F)] codes; however these type of converting reduce precision in small amounts. It would be useful if the problem could be solved by numpy functions such as np.logical_and besides reduce.

Upvotes: 0

the23Effect
the23Effect

Reputation: 580

Let me know if this works,

[x for x in map(lambda m: m in S, F)]

Upvotes: 1

Related Questions