1

我正在处理一个处理大型数据集的项目。

我需要在 Sklearn 的 KFold 交叉验证库中训练 SVM 分类器。

import pandas as pd
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score


x__df_chunk_synth = pd.read_csv('C:/Users/anujp/Desktop/sort/semester 4/ATML/Sem project/atml_proj/Data/x_train_syn.csv')
y_df_chunk_synth = pd.read_csv('C:/Users/anujp/Desktop/sort/semester 4/ATML/Sem project/atml_proj/Data/y_train_syn.csv')

svm_clf = svm.SVC(kernel='poly', gamma=1, class_weight=None, max_iter=20000, C = 100, tol=1e-5)
X = x__df_chunk_synth
Y = y_df_chunk_synth
scores = cross_val_score(svm_clf, X, Y,cv = 5, scoring = 'f1_weighted')
print(scores)
    
pred = svm_clf.predict(chunk_test_x)
accuracy = accuracy_score(chunk_test_y,pred)

print(accuracy)

我正在使用上述代码。我知道我正在使用 cross_val_score 函数训练我的分类器,因此每当我尝试在外部调用分类器以预测测试数据时,我都会收到错误消息:

sklearn.exceptions.NotFittedError: This SVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

有没有其他选择以正确的方式做同样的事情?

请帮我解决这个问题。

4

1 回答 1

1

Indeedmodel_selection.cross_val_score使用输入模型来拟合数据,因此不必进行拟合。但是,它不适合用作输入的实际对象,而是它的副本,因此This SVC instance is not fitted yet...在尝试预测时会出错。

cross_validate查看调用 in的源代码cross_val_score,在评分步骤中,首先estimator通过clone

scores = parallel(
    delayed(_fit_and_score)(
        clone(estimator), X, y, scorers, train, test, verbose, None,
        fit_params, return_train_score=return_train_score,
        return_times=True, return_estimator=return_estimator,
        error_score=error_score)
    for train, test in cv.split(X, y, groups))

这会创建模型的深层副本(这就是未拟合实际输入模型的原因):

def clone(estimator, *, safe=True):
    """Constructs a new estimator with the same parameters.
    Clone does a deep copy of the model in an estimator
    without actually copying attached data. It yields a new estimator
    with the same parameters that has not been fit on any data.
    ...
于 2020-07-04T21:20:24.787 回答