1

Having a list of boolean conditions list I would like to generate a matrix with the lists that have False or True values

For this example values = [[True, False], [False], [True], [True, False]] the result will be

    False  True
0   1       1
1   1       0
2   0       1
3   1       1

I tried to do that as following:

nodes = [True, False]
values = [[True, False], [False], [True], [True, False]]
res = np.array([[int(cond in vals) for vals in values] for cond in nodes],
                dtype=[(node, int) for node in nodes])

But I am getting the error TypeError: data type not understood

5
  • what is the dtype suppose to represent? I thought it just needed to be int to specify that each value in the array is an int Commented May 10, 2016 at 13:59
  • @TadhgMcDonald-Jensen it represent res values type. Commented May 10, 2016 at 14:03
  • 1
    let np.array analyse your data : In [348]: array( [[True, False], [False], [True], [True, False]]) -> Out[348]: array([[True, False], [False], [True], [True, False]], dtype=object) Commented May 10, 2016 at 14:05
  • 1
    have you tried dtype=int? I think that is all you need for this to work... Commented May 10, 2016 at 14:06
  • @TadhgMcDonald-Jensen yeah that works with dtype=int thx Commented May 10, 2016 at 14:10

1 Answer 1

3

Try dtype=int, and then we have,

import numpy as np 

nodes = [True, False]
values = [[True, False], [False], [True], [True, False]]
res = np.array([[cond in vals for vals in values] for cond in nodes], dtype=int)

print(res)
# Output
[[1 0 1 1]
 [1 1 0 1]]
Sign up to request clarification or add additional context in comments.

1 Comment

when the dtype is specified you don't even need to explicitly cast to that same type, so int(cond in vals) is a bit redundant, it could just be cond in vals

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.