cross_val_score 不符合实际输入模型?
Does cross_val_score not fit the actual input model?
我正在处理一个处理大型数据集的项目。
我需要在 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.
是否有任何其他选择以正确的方式做同样的事情?
请帮我解决这个问题。
确实model_selection.cross_val_score
使用输入模型来拟合数据,所以不一定要拟合。但是,它不适合用作输入的实际对象,而是它的 copy,因此在尝试预测时出现错误 This SVC instance is not fitted yet...
。
首先查看 cross_validate
which is called in cross_val_score
, in the scoring step, the estimator
goes through 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.
...
我正在处理一个处理大型数据集的项目。
我需要在 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.
是否有任何其他选择以正确的方式做同样的事情?
请帮我解决这个问题。
确实model_selection.cross_val_score
使用输入模型来拟合数据,所以不一定要拟合。但是,它不适合用作输入的实际对象,而是它的 copy,因此在尝试预测时出现错误 This SVC instance is not fitted yet...
。
首先查看 cross_validate
which is called in cross_val_score
, in the scoring step, the estimator
goes through 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.
...