【问题标题】:python the-truth-value-of-an-array.. ambigious errorpython the-truth-value-of-an-array .. 模棱两可的错误
【发布时间】:2016-01-06 17:52:04
【问题描述】:

我正在尝试使用 numpy 将 2 个向量相乘并进行比较,由于某种原因,它给了我错误。数据来自 CSV 文件 我找到了

The truth value of an array with more than one element is ambigous when trying to index an array

还有更多这种类型的情况,但它与我的情况确实不同.. 我的代码:

import sys
import numpy as np
data = np.loadtxt(sys.argv[1],  delimiter = ',')
X = data[:, 1:]
Y = data[:, 0]
   #argv[1] is mnist_train_1vs7vs8.csv
wMatrix=(3,len(X[0]))
np.zeros(wMatrix)
for i in range(0,len(Y)):
        maxWx=0
        for wIndex in range (0,1):
            if ( np.dot(wMatrix[wIndex] ,X[i]) < np.dot(wMatrix[wIndex+1],X[i]) ):
                maxWx=wIndex+1

它给了我错误:

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

我只是想将一个向量与一个相同大小的向量相乘,我不明白为什么它不会让我.. 帮助..?

【问题讨论】:

  • 错误不是由乘法引起的,而是由比较引起的。您是否尝试过打印出您正在比较的值?

标签: python arrays csv numpy vector


【解决方案1】:

如果您按照@kindall 的建议进行操作并打印出np.dot(wMatrix[wIndex] ,X[i]) &lt; np.dot(wMatrix[wIndex+1],X[i]) 的值,您会看到它是一个布尔数组。

你可能应该这样做:

res = np.dot(wMatrix[wIndex] ,X[i]) < np.dot(wMatrix[wIndex+1],X[i])
if res.all():
   ...

这取决于你在&lt; 中的实际意思:)

【讨论】:

  • 哇,解决方案正是错误消息所说的! :-)
猜你喜欢
  • 2023-02-03
  • 2020-07-24
  • 2022-12-01
  • 2021-12-28
  • 2022-12-02
  • 2018-11-07
  • 2022-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多