【问题标题】:Python.ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Python.ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
【发布时间】:2017-04-10 13:59:10
【问题描述】:

我正在学习如何使用 Graphlab 进行机器学习。所以,我有这个包含四列的数据集 - 有一列“名称”和另一列“评论”。

现在,我想通过产品名称获取特定产品的评论。所以,这就是我尝试过的,但我保留了错误 - ValueError:具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()。

if (products['name'] == "Vulli Sophie the Giraffe Teether"):
    print (products['review'])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-8607777f5c3b> in <module>()
----> 1 if (products['name'] == "Vulli Sophie the Giraffe Teether"):
      2     print products['review']

C:\Users\user\Anaconda2\envs\gl-env\lib\site-packages\graphlab\data_structures\sarray.pyc in __nonzero__(self)
    752         """
    753         # message copied from Numpy
--> 754         raise ValueError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")
    755 
    756     def __bool__(self):

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

编辑 -

  if (products['name'] == "Vulli Sophie the Giraffe Teether"):
        print products['name']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-1be157eebb1a> in <module>()
----> 1 if (products['name'] == "Vulli Sophie the Giraffe Teether"):
      2     print products['name']

C:\Users\user\Anaconda2\envs\gl-env\lib\site-packages\graphlab\data_structures\sarray.pyc in __nonzero__(self)
    752         """
    753         # message copied from Numpy
--> 754         raise ValueError("The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()")
    755 
    756     def __bool__(self):

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

【问题讨论】:

  • try : if (products['name'] == "Vulli Sophie the Giraffe Teether"):
  • @MohamedALANI:- 谢谢。仍然出现同样的错误。
  • 打印products['name'] 并展示给我们。
  • @Jean-FrançoisFabre: - 我已经上传了结果。谢谢。
  • 您可能需要为列表“产品”的每个元素执行此操作,这里您似乎正在将字符串列表(或系列)与字符串进行比较。

标签: python numpy graphlab


【解决方案1】:

错误消息告诉您products['name'] 是一个列表,因此您不能使用简单的比较器。

如果您想检查products['name'] 的任何值是否等于您的 标题,您应该将条件更改为:

if "Vulli Sophie the Giraffe Teether" in products['name']:
    # other stuff

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我所知道的 Graphlab似乎 使用 numpy 模块来提供数组...也就是说让我们从一些类似于你的数据开始

    In [21]: import numpy as np
    
    In [22]: prods = np.array((['a', 'b', 'c'], [1, 2, 3])) 
    
    In [23]: prods
    Out[23]: 
    array([['a', 'b', 'c'],
           ['1', '2', '3']], 
          dtype='<U1')
    

    我们有一个包含姓名('a'、'b'、'c')和评论分数(123)的向量。

    接下来,要查找特定名称的位置,您可以使用 矢量化布尔表达式,如下所示

    In [24]: prods[0] == 'b'
    Out[24]: array([False,  True, False], dtype=bool)
    

    如您所见,结果是一个布尔值向量。

    numpy 的美妙之处在于您可以用花哨的模式处理向量

    In [26]: prods[1, prods[0] == 'b']
    Out[26]: 
    array(['2'], 
          dtype='<U1')
    

    我写的是,在prods 中选择1(评论分数)作为第一个索引,第二个索引扫描布尔向量并仅使用True 项。

    如果没有匹配项会怎样?什么都没有

    In [27]: prods[1, prods[0] == 'd']
    Out[27]: 
    array([], 
          dtype='<U1')
    

    这个特定的值是False,所以你可以像这样使用它(未经测试)

    for my_name in names:
        my_review = products['review', products['name'] == my_name]
        if my_rev:
            do_stuff(my_review)
    

    【讨论】:

      猜你喜欢
      • 2020-05-12
      • 2018-12-01
      • 2016-01-26
      • 2020-07-02
      • 2014-07-04
      相关资源
      最近更新 更多