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

How to get coefficients with cross validation model

如何使用交叉验证模型获取系数?当我进行交叉验证时,我得到了 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_

您可能想要使用 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'] 访问指标)