使用相同的参数运行单个模型无法重现GridSearchCV/RandomizedSearchCV的结果

我正在使用5折交叉验证运行RandomizedSearchCV以寻找最佳参数。我有一个保留集(X_test)用于预测。我的代码部分是:

svc= SVC(class_weight=class_weights, random_state=42)Cs = [0.01, 0.1, 1, 10, 100, 1000, 10000]gammas = [1e-1, 1e-2, 1e-3, 1e-4, 1e-5]param_grid = {'C': Cs,              'gamma': gammas,              'kernel': ['linear', 'rbf', 'poly']}my_cv = TimeSeriesSplit(n_splits=5).split(X_train)rs_svm = RandomizedSearchCV(SVC(), param_grid, cv = my_cv, scoring='accuracy',                               refit='accuracy', verbose = 3, n_jobs=1, random_state=42)rs_svm.fit(X_train, y_train)y_pred = rs_svm.predict(X_test)cm = confusion_matrix(y_test, y_pred)clfreport = classification_report(y_test, y_pred)print (rs_svm.best_params_)

结果是分类报告:使用RandomizedSearchCV后的结果

现在,我有兴趣使用选定的参数通过独立运行的模型(不使用RandomizedSearchCV)来重现这个结果:

from sklearn.model_selection import TimeSeriesSplittcsv=TimeSeriesSplit(n_splits=5)for train_index, test_index in tcsv.split(X_train):    train_index_ = int(train_index.shape[0])    test_index_ = int(test_index.shape[0])    X_train_, y_train_ = X_train[0:train_index_],y_train[0:train_index_]    X_test_, y_test_ = X_train[test_index_:],y_train[test_index_:]    class_weights = compute_class_weight('balanced', np.unique(y_train_), y_train_)    class_weights = dict(enumerate(class_weights))    svc= SVC(C=0.01, gamma=0.1, kernel='linear', class_weight=class_weights, verbose=True,             random_state=42)    svc.fit(X_train_, y_train_)    y_pred_=svc.predict(X_test)cm = confusion_matrix(y_test, y_pred_)clfreport = classification_report(y_test, y_pred_)

根据我的理解,分类报告应该是一样的,但在我运行后得到的结果是:

独立模型的结果

有没有人有建议为什么会发生这种情况?


回答:

根据您的第一个代码片段,您使用RandomizedSearchCV来寻找最佳超参数,您不需要再次进行分割;因此,在您的第二个代码片段中,您应该仅使用找到的超参数和类权重,使用整个训练集进行拟合,然后在测试集上进行预测:

class_weights = compute_class_weight('balanced', np.unique(y_train), y_train)class_weights = dict(enumerate(class_weights))svc= SVC(C=0.01, gamma=0.1, kernel='linear', class_weight=class_weights, verbose=True, random_state=42)svc.fit(X_train, y_train)y_pred_=svc.predict(X_test)cm = confusion_matrix(y_test, y_pred)clfreport = classification_report(y_test, y_pred)

讨论使用验证、训练和测试集的顺序可能有助于澄清过程…

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中创建了一个多类分类项目。该项目可以对…

发表回复

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