我想将SVM与LeaveOneOut交叉验证(Loocv)一起使用。代码如下:
from sklearn.svm import SVC
from sklearn.model_selection import LeaveOneOut, train_test_split
import numpy as np
import pandas as pd
iRec = 'KSBPSSM_6_DCT_MIXED_49_937_937_1874_SMOTTMK.csv'
D = pd.read_csv(iRec, header=None) # Using pandas
X = D.iloc[:, :-1].values
y = D.iloc[:, -1].values
from sklearn.utils import shuffle
X, y = shuffle(X, y) # Avoiding bias
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75,
test_size=0.25)
tpot = SVC(kernel='rbf', C=2.123, gamma=0.0039, cv=LeaveOneOut(),
probability=True,)
tpot.fit(X_train, y_train)
print(tpot.score(X_test, y_test))
tpot.export('tpot_pipeline_' + str(index) + '.py')
运行代码时,我收到以下错误:
Traceback (most recent call last):
File "E:/PhD Folder/PhD research/DNA-binding Proteins literature
papers/Effective DNA binding protein prediction by using key features via
Chou’s general PseAAC_Code_dataset_10_10_2018/DNA_Binding-
master/SVM_jackknife_test.py", line 18, in <module>
tpot = SVC(kernel='rbf', C=2.123, gamma=0.0039, cv=LeaveOneOut(),
probability=True,)
TypeError: __init__() got an unexpected keyword argument 'cv'
有人可以帮我吗
答案 0 :(得分:0)
首先,看看SVC documentation和Cross-Validation Documentation (sklearn)。
SVC()
不使用任何cv
参数,事实上,模型不考虑交叉验证。 CV用于检查性能并防止过度拟合。
交叉验证文档中使用的示例实际上与SVC
一起使用。
在您的情况下,您可以按以下方式使用cross_val_score:
tpot = SVC(kernel='rbf', C=2.123, gamma=0.0039, probability=True)
scores = cross_val_score(tpot, X_test, y_test, cv=LeaveOneOut())
print(scores.mean())