0

I'm trying to multiply 2 vectors using numpy and compare them, for some reasong it gives me error.. the data is from a CSV file I have found

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

and more case of this type, but it is really different from my case.. my code:

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

it gives me error:

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

I'm just trying to multiply a vector with a vector of the same size, and i don't understand why it won't let me.. HELP..?

Community
  • 1
  • 1
Zohar Argov
  • 143
  • 1
  • 1
  • 11
  • 1
    The error isn't being caused by the multiplication, but by the comparison. Have you tried *printing out* the values you're comparing? – kindall Jan 06 '16 at 17:54

1 Answers1

3

If you do as @kindall suggested and print out the value of np.dot(wMatrix[wIndex] ,X[i]) < np.dot(wMatrix[wIndex+1],X[i]) you'd see it's an array of booleans.

You should probably do something like:

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

This depends on when you actually mean in < :)

Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49