sp2*_*sp2 2 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
Run Code Online (Sandbox Code Playgroud)
我想打印与每个特征相关的系数
#Print co-efficients of features
for i in range(0, nFeatures):
print samples.columns[i],":", coef[0][i]
Run Code Online (Sandbox Code Playgroud)
这个没有交叉验证,它提供系数
#Create SVM model using a linear kernel
model = svm.SVC(kernel='linear', C=C).fit(x_train, y_train)
coef = model.coef_
Run Code Online (Sandbox Code Playgroud)
您可能想要使用model_selection.cross_validate (with 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_)
Run Code Online (Sandbox Code Playgroud)
应该给你想要你正在寻找的,希望!(您可以通过cv_results['train_score']
和访问指标cv_results['test_score']
)
归档时间: |
|
查看次数: |
3303 次 |
最近记录: |