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

Viewed 266

Here is a code snippet, where I am storing the euclidean distance of a dataset by that's respective train row(two lists named train & value) in a list(named L). But when i am sorting the list, an error occurred :

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

the code :

def euclidean_distance(x,y):
       return math.sqrt(pow((x[0]-y[0]),2) + pow((x[1]-y[1]),2) + pow((x[2]-y[2]),2) + pow((x[3]-y[3]),2))        

for value in val_set:
  for train in train_set: 
    L.append((euclidean_distance(value,train),train))
  
L.sort()

that's how the val_set and train_set are generated:


    iris = datasets.load_iris()
    data = iris.data[:, :4]
    y = iris.target
    
    val_set=[]
    test_set=[]
    train_set=[]
    L=[]
    random.shuffle(data)
    
    for i in data:
      ran =random.random()
      if ran>=0 and ran<=0.7:
        train_set.append(i)
      elif ran>0.7 and ran<=0.85:
        val_set.append(i)
      else:
        test_set.append(i) 

can anyone help to solve this problem?

1 Answers

I have given the exact range, which should be sorted. it's solved my problem :

L.sort(key=lambda tup: tup[0])
Related