繁体   English   中英

TypeError:创建numpy数组时无法理解数据类型

[英]TypeError: data type not understood while creating a numpy array

有一个布尔条件列表,我想用包含False或True值的列表生成一个矩阵

对于此示例, values = [[True, False], [False], [True], [True, False]] ,结果将是

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

我尝试这样做,如下所示:

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])

但是我收到错误TypeError: data type not understood

尝试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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM