1

I am new in python and I'm using python 3.6. I need to use Leave One Out cross validation in my project . I have a data set that has a column 'pos' or 'neg' value, I want to split them in my cross validation function, I wrote this code:

negclass = []
posclass = []
loo = LeaveOneOut()
loo.get_n_splits(x)
for train_index, test_index in loo.split(x):
    x_train, x_test = x[train_index], x[test_index]
    y_train, y_test = y[train_index], y[test_index]
    for i in range(len(train_index)):
        if(y_train == 'NEG'):
            negclass.append(x_train)
        elif(y_train == 'POS'):
            posclass.append(x_train)

but it gives me that error

ValueError                                Traceback (most recent call last)
<ipython-input-21-b93d17acc034> in <module>()
      7     y_train, y_test = y[train_index], y[test_index]
      8     for i in range(len(train_index)):
----> 9         if(y_train == 'NEG'):
     10             negclass.append(x_train)
     11         elif(y_train == 'POS'):

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

How can I fix it? Thanks

There is another topic in stackoverflow https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous I've read that, logical operands caused this error there. my problem is about how to use LeaveOneOut cross validation in this case

user184551
  • 45
  • 1
  • 7
  • Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – YSelf Jun 14 '18 at 13:03
  • 1
    @YSelf same error but my question is another thing – user184551 Jun 14 '18 at 13:05
  • 1
    No, same error, same problem.You have `if x == something` with x being a numpy array. This does not make sense. Work per element, or use `any` or `all`. Or the bool array `x==something` as an index. Just not with `if`, because that does not work. – YSelf Jun 14 '18 at 14:03
  • 2
    And apparently you tried doing a loop over the elements, but forgot actually taking only one element. Currently, your `for i in range()` loop does not use `i`, but repeats the same (invalid) statement. – YSelf Jun 14 '18 at 14:05

0 Answers0