TypeError:__init __()获得了意外的关键字参数'cv'

时间:2019-03-11 02:49:50

标签: python scikit-learn cross-validation

我想将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'

有人可以帮我吗

1 个答案:

答案 0 :(得分:0)

首先,看看SVC documentationCross-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())