如何使用交叉验证模型获取系数

时间:2019-01-22 11:19:23

标签: python svm cross-validation

如何通过交叉验证模型获得系数?进行交叉验证时,我会获得CV模型的分数,如何获得系数?

#Split into training and testing
x_train, x_test, y_train, y_test = train_test_split(samples, scores, test_size = 0.30, train_size = 0.70)

clf = svm.SVC(kernel='linear', C=1)
scores = cross_val_score(clf, x_train, y_train, cv=5)
scores

我想打印与每个功能相关的系数

   #Print co-efficients of features
    for i in range(0, nFeatures):
    print samples.columns[i],":", coef[0][i]

这个没有交叉验证,它提供系数

#Create SVM model using a linear kernel
model = svm.SVC(kernel='linear', C=C).fit(x_train, y_train)
coef = model.coef_

1 个答案:

答案 0 :(得分:2)

您可能希望使用model_selection.cross_validate(与return_estimator=True一起使用)而不是cross_val_score。它更加灵活,因此您可以访问用于每个折页的估算器:

from sklearn.svm import SVC
from sklearn.model_selection import cross_validate

clf = SVC(kernel='linear', C=1)
cv_results = cross_validate(clf, x_train, y_train, cv=5, return_estimator=True)

for model in cv_results['estimator']:
    print(model.coef_)

应该给您希望的东西,希望! (您可以通过cv_results['train_score']cv_results['test_score']访问指标)