[英]TypeError: data type not understood while creating a numpy array
Having a list of boolean conditions list I would like to generate a matrix with the lists that have False or True values 有一个布尔条件列表,我想用包含False或True值的列表生成一个矩阵
For this example values = [[True, False], [False], [True], [True, False]]
the result will be 对于此示例, values = [[True, False], [False], [True], [True, False]]
,结果将是
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
但是我收到错误TypeError: data type not understood
Try dtype=int
, and then we have, 尝试dtype=int
,然后得到
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.