我正在尝试使用 SVM 分类器使用自定义交叉验证折叠来建模二元分类问题,但它给了我错误 **需要至少一个数组来连接 ** 与 cross_val_predict。该代码在 cros_val_predict 中的 cv=3 下工作正常,但是当我使用 custom_cv 时,它会出现此错误。
下面是代码:
from sklearn.model_selection import LeavePOut
import numpy as np
from sklearn.svm import SVC
from time import *
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import cross_val_predict,cross_val_score
clf = SVC(kernel='linear',C=25)
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8],[9,10]])
y = np.array([0,1,1,0,0])
lpo = LeavePOut(2)
print(lpo.get_n_splits(X))
LeavePOut(p=2)
test_index_list=[]
train_index_list=[]
for train_index, test_index in lpo.split(X,y):
if(y[test_index[0]]==y[test_index[1]]):
pass
else:
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
train_index_list.append(train_index)
test_index_list.append(test_index)
custom_cv = zip(train_index_list, test_index_list)
scores = cross_val_score(clf, X, y, cv=custom_cv)
print(scores)
print('accuracy:',scores.mean())
predicted=cross_val_predict(clf,X,y,cv=custom_cv) # error with this line
print('Confusion matrix:',confusion_matrix(labels, predicted))
以下是错误的完整跟踪:
ValueError Traceback (most recent call last)
<ipython-input-11-d78feac932b2> in <module>()
31 print(scores)
32 print('accuracy:',scores.mean())
---> 33 predicted=cross_val_predict(clf,X,y,cv=custom_cv)
34
35 print('Confusion matrix:',confusion_matrix(labels, predicted))
关于如何解决此错误有什么建议吗?
慕尼黑5688855
相关分类