【发布时间】:2017-05-06 20:36:18
【问题描述】:
我需要测试数组中的布尔值,并根据答案对矩阵应用元素操作。我似乎得到了 ROW 的布尔答案,而不是单个元素本身。我如何测试并获得每个元素的答案?
我有一个概率矩阵
probs = np.array([[0.1, 0.2, 0.3, 0.3, 0.7],
[0.1, 0.2, 0.3, 0.3, 0.7],
[0.7, 0.2, 0.6, 0.1, 0.0]])
和一个测试数组矩阵
tst = ([False, False, True, True, False],
[True, False, True, False, False],
)
t = np.asarray(tst).astype('bool')
我写的这段代码输出了答案,但显然测试了整行,因为一切都是假的。
for row in tst:
mat = []
for row1 in probs:
temp = []
if row == True:
temp.append(row1)
else: temp.append(row1-1)
mat.append(temp)
mat
Out[42]:
[[array([-0.9, -0.8, -0.7, -0.7, -0.3])],
[array([-0.9, -0.8, -0.7, -0.7, -0.3])],
[array([-0.3, -0.8, -0.4, -0.9, -1. ])]]
我需要新的矩阵
[[-0.9, -0.8, 0.3, 0.3, -0.3],
[-0.9, -0.8, 0.3, 0.3, -0.3],
[-0.3, -0.8, 0.6, 0.1, -1]
对于 tst 中的第一个数组。非常感谢您的帮助!
【问题讨论】:
标签: python matrix boolean operations