Home > Software engineering >  The truth value of an array with more than one element is ambiguous when iterating an array
The truth value of an array with more than one element is ambiguous when iterating an array

Time:06-23

I'm implementing an array iteration in python like the following .

def incrementalAdd(self,p,eps,Minpts):
        self.num = self.num   1
        print("\nADDING point " str(self.num))
        self.visited = []
        self.newCores = []
        UpdSeedIns = []
        foundClusters = []
        NeighbourPoints = self.regionQuery(p,eps)
        if len(NeighbourPoints) >= int(Minpts):
            self.newCores.append(p) 
        self.visited.append(p)
        for pt in NeighbourPoints:
          print('pt:')
          print(pt)
          print('self.visited:')
          print(self.visited)
          i=0  
          for i in range(len(pt)):
            if pt[i] not in self.visited:

If I print the values of pt and self.visited, I get the following values:

pt:
[766.13 389.14]
self.visited:
[array([766.13, 389.14])]

however it shows me an error:

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

I appreciate if anyone can suggest me a solution to solve this.

CodePudding user response:

self.visited:
[array([766.13, 389.14])]

This mean you have list holding single array, thus the error, for example

import numpy as np
x = 389.14
visited = [np.array([766.13, 389.14])]
print(x in visited) # ValueError: The truth value of an array with more than one element is ambiguous....

you need to use flat structure rather than nested as visited, if it always has exactly one element which is 1D array you might just access it

import numpy as np
x = 389.14
visited = [np.array([766.13, 389.14])]
print(x in visited[0]) # True
  • Related