微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ValueError:具有多个元素的数组的真值不明确使用a.any或a.all在进行KFold交叉验证时

如何解决ValueError:具有多个元素的数组的真值不明确使用a.any或a.all在进行KFold交叉验证时

我正在尝试执行以下代码进行KFold交叉验证:

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)

通过运行代码,我得到以下错误

  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",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().

是什么原因导致该错误以及如何解决此问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。