交叉验证评分是否未拟合实际输入模型?

我正在进行一个项目,处理一个大型数据集。

我需要在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.    ...

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注