使用预测准确率作为网格搜索得分

问题描述 投票:0回答:1

我想知道我是否可以使用预测来找到最佳分数而不是在 sklearn 的 GridSearchCV 中进行交叉验证?

我有一个不平衡的数据集,我使用 RandomUnderSampler() 来获得平衡的 train_test_split 并用它来训练 RandomForestClassifier() 来预测一个名为 df2 的看不见的数据集。

我的问题是这样的:在没有改变超参数的情况下,我得到了 ~0.77 的准确度分数,但每次运行网格搜索时,X_test 的性能都会提高,但在未见过的数据集 (df2) 上的性能比默认参数差。所以我的想法是使用 .predict() 的结果来选择最佳参数,但我不知道该怎么做。有什么见解吗?

rfc=RandomForestClassifier(random_state=508312)

param_grid = { 
    'n_estimators': [10, 100, 200, 500],
    'max_features': ['sqrt', 'log2', None],
    'max_depth' : [4,8,12,16],
}

RF_clf_CV = GridSearchCV(rfc, param_grid, cv=5, verbose=True, n_jobs=-1)

# uncoment below to use the gridsearch

RF_clf_CV.fit(X_train, y_train)
RF_clf_CV.best_params_

best_params_lr = RF_clf_CV.best_params_
clf = RandomForestClassifier(**params).fit(X_train, y_train)

# predict X_train
y_pred = clf.predict(X_train)
y_pred_proba = clf.predict_proba(X_train)[:,1]
acc_score = accuracy_score(y_train, y_pred)
auc_score = roc_auc_score(y_train, y_pred_proba)
print(f'The score on X_train has an accuracy of : {acc_score:0.4f} and AUC of {auc_score:0.4f}')

# predict X_test
y_pred = clf.predict(X_test)
y_pred_proba = clf.predict_proba(X_test)[:,1]
acc_score = accuracy_score(y_test, y_pred)
auc_score = roc_auc_score(y_test, y_pred_proba)
print(f'The score on X_test has an accuracy of : {acc_score:0.4f} and AUC of {auc_score:0.4f}')

print()
# predict balanced unseen dataset df2
y_pred = clf.predict(df2_X_scaled)
y_pred_proba = clf.predict_proba(df2_X_scaled)[:,1]
acc_score = accuracy_score(df2_y, y_pred)
auc_score = roc_auc_score(df2_y,y_pred_proba)
print(f'The score on the unseen dataset has an accuracy of : {acc_score:0.4f} and AUC of {auc_score:0.4f}')

# MSE & RMSE score of the results in unseen dataset df2
mse = mean_squared_error(df2_y, y_pred)
print(f'Mean Squared Error : {mse:0.4f}')
rmse = np.sqrt(mean_squared_error(df2_y, y_pred))
print(f'Root Mean Squared Error : {rmse:0.4f}')

输出:

X_train 上的分数准确度为:0.8500,AUC 为 0.9174 X_test 的分数准确度为:0.8479,AUC 为 0.9163

未见数据集上的分数准确度为:0.6272,AUC 为 0.7007 均方误差:0.3728 均方根误差:0.6106

scikit-learn random-forest grid-search
1个回答
0
投票

如果你的训练数据集有更高的准确度,那么在你的测试中这意味着你overfit你的模型。这是一种不受欢迎的情况,因为获得的拟合不会对不属于原始训练数据集的新观察结果的响应产生准确的估计。

只是通过选择 .predict() 值来扭转局面不会改善您的模型。我认为你需要找到一种不同的方法。尝试获得一个模型,其中您的火车的准确性与您的测试相似。

© www.soinside.com 2019 - 2024. All rights reserved.