我正在對資料集運行交叉驗證并得到
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
kFCVEvaluate for 回圈第二次迭代開始時出錯。我究竟做錯了什么?
我查看了有關此錯誤的其他帖子,它們是關于使用和/或運算子的,但我不使用任何邏輯運算子。
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)
uj5u.com熱心網友回復:
測驗我的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]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/364540.html
標籤:Python 蟒蛇-3.x 麻木的 机器学习 scikit-学习
上一篇:PythonSKlearnTfidfVectorizer引數錯誤
下一篇:標記化和分段之間的區別
