具有两个布尔数组的For循环的If条件中的ValueError

时间:2019-09-10 07:00:31

标签: python for-loop if-statement valueerror

我正在运行一个带If语句的For-Loop,其中包括两个布尔数组,以创建一个新数组。

我已经尝试了所有我可以在StackOverflow上找到的解决方案,并使用Logical_and或bitwise_and交换了&,并且还使用了建议的a.any()和a.all()方法,我仍然遇到相同的ValueError。

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

for i in (y_valid, y_pred):
    CM = []
    if (y_valid[i] == False) & (y_pred[i] == False):
        CM == 0
    elif (y_valid[i] == False) & (y_pred[i] == True):
        CM == 1
    elif (y_valid[i] == True) & (y_pred[i] == False):
        CM == 2
    elif (y_valid[i] == True) & (y_pred[i] == True):
        CM == 3

我希望得到一个包含0-3之间数字的数组CM

我的输出:

ValueError                                Traceback (most recent call last)
<ipython-input-107-259ac7895185> in <module>
      1 for i in (y_valid, y_pred):
      2     CM = []
----> 3     if (y_valid[i] == False) & (y_pred[i] == False):
      4         CM == 0
      5     elif (y_valid[i] == False) & (y_pred[i] == True):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

5 个答案:

答案 0 :(得分:1)

您的错误正在循环中。您正在遍历一系列真值陈述,这就是为什么您收到与使用any()all()有关的错误的原因,因为您需要定义是否要查看是否存在any() True值或条件中的all()真值。

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

for i in (y_valid, y_pred):
    print(i)
    break

[Out]: [True, False, True, False, False, True]

执行此操作的更快方法是将数组中的值压缩。 压缩值会创建要迭代的元组列表:

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

print(list(zip(y_valid, y_pred)))

[Out]: [(True, False), (False, False), (True, True), (False, True), (False, False), (True, False)]

最后,您可能希望将值存储在列表中的每个点。在这种情况下,您需要修改分配,如下所示:

CM = []
for valid, pred in zip(y_valid, y_pred):
    if not valid and not pred:
        CM.append(0)
    elif not valid and pred:
        CM.append(1)
    elif valid and not pred:
        CM.append(2)
    else:
        CM.append(3)

答案 1 :(得分:1)

如果我理解正确,则需要以下条件:

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

# Move this out of the loop so you do not reset it every time!
CM = []

# To make i integer (as required for it to be list index) 
# use range and len.
for i in range(len(y_valid)):
    # Dropping comparisons with True/False values since 
    # the following would be the same. Also note that `&` is 
    # a bitwise operator in python while `and` is boolean
    if not y_valid[i] and not y_pred[i]:
        # In every iteration we are now appending to the same CM list
        CM.append(0)
    elif not y_valid[i] and y_pred[i]:
        CM.append(1)
    elif y_valid[i] and not y_pred[i]:
        CM.append(2)
    elif y_valid[i] and y_pred[i]:
        CM.append(3)

print(CM)

输出为:

$ python /tmp/test.py 
[2, 0, 3, 1, 0, 2]

检查代码中的注释,以了解我对原始注释所做的更改。让我知道你是否有疑问

答案 2 :(得分:1)

您可以使用布尔值可以转换为整数的事实:

not y_valid[i] and not y_pred[i] => int(y_valid[i]) == 0 and int(y_pred[i]) == 0 => 2*y_valid[i] + y_pred[i] == 0
not y_valid[i] and y_pred[i]     => int(y_valid[i]) == 0 and int(y_pred[i]) == 1 => 2*y_valid[i] + y_pred[i] == 1
y_valid[i] and not y_pred[i]     => int(y_valid[i]) == 1 and int(y_pred[i]) == 0 => 2*y_valid[i] + y_pred[i] == 2
y_valid[i] and y_pred[i]         => int(y_valid[i]) == 1 and int(y_pred[i]) == 1 => 2*y_valid[i] + y_pred[i] == 3

并使用列表推导来创建CM

>>> y_valid = [True, False, True, False, False, True]
>>> y_pred = [False, False, True, True, False, False]

>>> [2*y_v+y_p for y_v, y_p in zip(y_valid, y_pred)]
[2, 0, 3, 1, 0, 2]

(请注意:zip将占用y_valid[i], y_pred[i]中的每个i in range(min(len(y_valid), len(y_pred)))。

答案 3 :(得分:0)

考虑两个列表的长度相同。

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

CM = -1
for i in range(len(y_pred)):
    if (y_valid[i] == False) and (y_pred[i] == False):
        CM == 0
    elif (y_valid[i] == False) and (y_pred[i] == True):
        CM == 1
    elif (y_valid[i] == True) and (y_pred[i] == False):
        CM == 2
    elif (y_valid[i] == True) and (y_pred[i] == True):
        CM == 3

但是,python中有更好的方法遍历列表或多个列表。

您的代码基本上遍历tuple
(y_valid, y_pred)基本上是tuple。您的代码遍历每个列表的元组。

for i in (y_valid, y_pred):
    print(i)

输出:

[True, False, True, False, False, True]
[False, False, True, True, False, False]

遍历多个列表的Python方法是zip

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

CM = -1
for y_valid_i, y_pred_i in zip(y_valid, y_pred):
    if not y_valid_i and not y_pred_i:
        CM == 0
    elif not y_valid_i and y_pred_i:
        CM == 1
    elif y_valid_i and not y_pred_i:
        CM == 2
    elif y_valid_i and y_pred_i:
        CM == 3

但是,使用变量CM的方式很可能会遇到更多错误。

答案 4 :(得分:0)

y_valid = [True, False, True, False, False, True]
y_pred = [False, False, True, True, False, False]

for i in (y_valid, y_pred):
  print(i)

返回

[True, False, True, False, False, True]
[False, False, True, True, False, False]

因此python将(y_valid, y_pred)视为元组。 尝试:

for i in range(len(y_valid)):
    # Code comes here

for i in range(len(y_pred)):
    # Code comes here