0

I am trying to execute the below code for KFold cross validation:

def evaluate_model(X, Y):
    results = list()
    n_inputs, n_outputs = X.shape[1], Y.shape[1]
    # define evaluation procedure
    cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
    # enumerate folds
    for train_ix, test_ix in cv.split(X):
        # prepare data
        X_train, X_test = X.iloc[list(train_ix)], X.iloc[list(test_ix)]
        y_train, y_test = Y.iloc[list(train_ix)], Y.iloc[list(test_ix)]
        # define model
        model = get_model(n_inputs, n_outputs)
        # fit model
        model.fit(X_train, y_train, verbose=0, epochs=100)
        # make a prediction on the test set
        yhat = model.predict(X_test)
        # round probabilities to class labels
        yhat = yhat.round()
        # calculate accuracy
        acc = accuracy_score(y_test, yhat)
        # store result
        print('>%.3f' % acc)
        results.append(acc)
   return results
results = evaluate_model(X, Y)

by running the code I am getting the below error:

  y_type, y_true, y_pred = _check_targets(y_true, y_pred)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 99, in _check_targets
    y_true = csr_matrix(y_true)

  File "C:\Users\XYZ\AppData\Roaming\Python\Python37\site-packages\scipy\sparse\compressed.py", line 88, in __init__
    self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))

  File "C:\Users\XYZ\AppData\Roaming\Python\Python37\site-packages\scipy\sparse\compressed.py", line 88, in __init__
    self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))

  File "C:\Users\XYZ\AppData\Roaming\Python\Python37\site-packages\scipy\sparse\coo.py", line 191, in __init__
    self.row, self.col = M.nonzero()

  File "C:\Users\XYZ\AppData\Roaming\Python\Python37\site-packages\scipy\sparse\base.py", line 287, in __bool__
    raise ValueError("The truth value of an array with more than one "

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

What is causing the error and how to resolve this?

James Z
  • 12,209
  • 10
  • 24
  • 44
Niladri
  • 29
  • 4
  • Does this answer your question? [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) – Paul H Oct 25 '20 at 15:08
  • Not really. Seems the context is different. – Niladri Oct 26 '20 at 07:41

0 Answers0