具有多个元素的数组的真值是模棱两可的错误?

The truth value of an array with more than one element is ambiguous error?

我正在 运行 对数据集进行交叉验证并得到

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

kFCVEvaluate 的 for 循环的第二次迭代开始时出错。我做错了什么?

我查看了有关此错误的其他帖子,它们是关于使用 and/or 运算符,但我没有使用任何逻辑运算符。

from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor(random_state = 42, max_depth=5)

def split_dataset(dataset): 
        numFolds=10 
        dataSplit = list()
        dataCopy = list(dataset)
        foldSize = int(len(dataset) / numFolds)
        for _ in range(numFolds):
            fold = list()
            while len(fold) < foldSize:
                index = randrange(len(dataCopy))
                fold.append(dataCopy.pop(index))
            dataSplit.append(fold)
        return dataSplit
    
def kFCVEvaluate(dataset):

    folds = split_dataset(data)
    scores = list()
    for fold in folds:
        trainSet = list(folds)
        trainSet.remove(fold)
        trainSet = sum(trainSet, [])
        testSet = list()
        for row in fold:
            rowCopy = list(row)
            testSet.append(rowCopy)
            
        trainLabels = [row[-1] for row in trainSet]
        trainSet = [train[:-1] for train in trainSet]
        model.fit(trainSet,trainLabels)
        
        actual = [row[-1] for row in testSet]
        testSet = [test[:-1] for test in testSet]
        
        predicted = model.predict(testSet)
        
        accuracy = actual-predicted
        scores.append(accuracy)
        print(scores)
kFCVEvaluate(data)

测试我的 remove 假设

In [214]: alist = [np.array([1,2,3]), np.ones(3), np.array([4,5])]
In [215]: alist.remove(np.array([1,2,3]))
Traceback (most recent call last):
  File "<ipython-input-215-0b2a68765241>", line 1, in <module>
    alist.remove(np.array([1,2,3]))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如果列表包含列表或元组,

remove 确实有效

In [216]: alist = [[1,2,3], [1,1,1], [4,5]]
In [217]: alist.remove([1,1,1])
In [218]: alist
Out[218]: [[1, 2, 3], [4, 5]]