我正在进行一个项目,处理一个大型数据集。
我需要在Sklearn的KFold交叉验证库中训练SVM分类器。
import pandas as pdfrom sklearn import svmfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import cross_val_scorex__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_synthY = y_df_chunk_synthscores = 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.
有没有其他正确的方法来做同样的事情?
请帮助我解决这个问题。
回答:
确实,model_selection.cross_val_score
使用输入模型来拟合数据,因此不需要预先拟合。然而,它拟合的并不是作为输入的实际对象,而是一个副本,因此在尝试预测时会出现This SVC instance is not fitted yet...
的错误。
查看在cross_val_score
中调用的cross_validate
的源代码,在评分步骤中,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. ...