【发布时间】:2017-12-01 12:46:02
【问题描述】:
我创建了一个简单的脚本来在随机森林分类器上应用网格搜索,虽然我过去曾使用过它,但现在它似乎坏了,我找不到原因。
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
rfc = RandomForestClassifier(n_estimators=100, n_jobs=-1).fit(X, y)
grid_values = {'criterion':['gini','entropy'], 'max_features':['log2', 5, 10, 15, 20, 25], 'max_depth':[None, 5, 10, 15, 20],
'min_samples_split':[2, 3],'n_jobs':[-1], 'class_weight': [{0 : 1., 1: 30.}, {0 : 1., 1: 50.}, {0 : 1., 1: 100.}]}
for eval_metric in ('precision', 'accuracy'):
rfc_custom = GridSearchCV(rfc, param_grid=grid_values, scoring=eval_metric)
rfc_custom.fit(X_train, y_train)
rfc_custom.best_params_
print('Grid best parameter (max. {0}): {1}'
.format(eval_metric, rfc_custom.best_params_))
print('Grid best score ({0}): {1}'
.format(eval_metric, rfc_custom.best_score_))
当我运行它时,我收到以下警告:UndefinedMetricWarning: Precision is ill-defined and is set to 0.0 due to no predict samples.
在线搜索,我添加了此代码并且警告停止:
import warnings
import sklearn.exception
warnings.filterwarnings("ignore",category=sklearn.exceptions.UndefinedMetricWarning)
运行算法后,我得到 0.0 的精度
这是正常的,因为我收到了警告吗?我可能错过了什么吗?
【问题讨论】:
-
下面的代码只隐藏了警告,但不改变算法的行为。问题是在预测值中,有一些类,算法从未预测过,所以精度是不确定的。
-
哪一行发出了警告?
-
它在 'GridSearchCV(rfc, param_grid=grid_values,scoring=eval_metric)' 行中发出。据我了解,每个交叉验证都会产生此错误,因为同一指标会产生多个错误。
标签: python scikit-learn