访问 gridsearchcv 随机森林中的 model.best_estimator_.feature_importances_

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

我正在使用 RAPID AI 的 cuml。我使用 gridsearchcv 来查找最佳参数,但是我无法获得最佳特征(用于特征选择目的)。

这是我的代码:

combined_df=cpd.concat([train_df,evaluate_df])
combined_df=combined_df.astype('float32')
test_fold = [0] * len(train_df) + [1] * len(evaluate_df)

p_test3 = {'n_estimators':[45,50,200,500],'max_depth':[20,25,15,10]}#'max_features':[30,50,60,70,80]

tuning = GridSearchCV(estimator =cuRFr(n_streams=1, min_samples_split=2, min_samples_leaf=1, random_state=0), 
            param_grid = p_test3, scoring='r2', cv=PredefinedSplit(test_fold=test_fold))

tuning.fit(combined_df.iloc[:,2:].to_numpy(dtype='float32'),combined_df['Mcap_w'].to_numpy(dtype='float32'))

我尝试时收到错误

tuning.feature_importances_

----> 1 best_features = tuning.best_estimator_.feature_importances_  File base.pyx:330, in cuml.interna ls.base.Base.__getattr__()

AttributeError: feature_importances_
python machine-learning random-forest cuml
1个回答
-1
投票

在使用 RAPIDS AI cuML 的 RandomForestRegressor 执行网格搜索后,您似乎在访问最佳估计器的 feature_importances_ 属性时遇到问题。让我们一步步解决这个问题。

理解错误

您遇到的错误表明 feature_importances_ 属性无法从您的模型直接访问。这可能是因为 GridSearchCV 不会自动公开特定于估计器的属性,例如 feature_importances_。

解决方法

  1. 检查兼容性:确保您的 cuML 版本支持 RandomForestRegressor 的 feature_importances_。此功能通常可用,但其可访问性可能因版本而异。

  2. 访问最佳估计器:您已正确尝试访问 best_estimator_ 属性。此步骤至关重要,因为 feature_importances_ 是模型的属性(本例中为 RandomForestRegressor),而不是 GridSearchCV 对象的属性。

    best_model = 调整.best_estimator_

  3. 访问特征重要性:一旦你有了最好的估计器,理论上你应该能够直接从中访问 feature_importances_ 属性,前提是模型支持它。

    尝试: feature_importances = best_model.feature_importances_ 打印(特征重要性) 除了属性错误: print("所选模型不支持feature_importances_")

其他注意事项

  • 文档检查:如果feature_importances_不可用,请查看 您的版本的 cuML 文档。有可能的是 RandomForestRegressor 可能会通过 不同的方法或属性。
  • cuML 版本:确保您使用的是最新版本的 cuML,因为较新的版本可能包含与以下内容相关的增强功能或错误修复: 特征重要性。
  • 替代方法:如果无法直接访问特征重要性,请考虑评估特征的替代方法 重要性或提供此功能的其他模型。

您的方法基本上是正确的,但问题似乎与 cuML 的具体实现或版本有关。确认您的 cuML 版本中 feature_importances_ 的可用性并确保您从 best_estimator_ 访问它应该可以解决该问题。如果问题仍然存在,请考虑联系 RAPIDS AI 支持或其社区论坛以获得更有针对性的帮助。

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