您正在寻找的是Parameter Tuning。基本上,首先选择一个估计器,然后定义一个超参数空间(即所有可能的参数及其要调整的各自值)、一个交叉验证方案和评分函数。现在根据您搜索参数空间的选择,您可以选择以下内容:
详尽的网格搜索
在这种方法中,sklearn 创建了一个由用户使用GridSearchCV 方法定义的所有可能的超参数值组合的网格。例如:
my_clf = DecisionTreeClassifier(random_state=0,class_weight='balanced')
param_grid = dict(
classifier__min_samples_split=[5,7,9,11],
classifier__max_leaf_nodes =[50,60,70,80],
classifier__max_depth = [1,3,5,7,9]
)
在这种情况下,指定的网格是分类器__min_samples_split、分类器__max_leaf_nodes 和分类器__max_depth 值的叉积。该文档指出:
GridSearchCV 实例实现了通常的估算器 API:当将其“拟合”到数据集上时,所有可能的参数值组合都会被评估并保留最佳组合。
使用 GridSearch 的示例:
#Create a classifier
clf = LogisticRegression(random_state = 0)
#Cross-validate the dataset
cv=StratifiedKFold(n_splits=n_splits).split(features,labels)
#Declare the hyper-parameter grid
param_grid = dict(
classifier__tol=[1.0,0.1,0.01,0.001],
classifier__C = np.power([10.0]*5,list(xrange(-3,2))).tolist(),
classifier__solver =['newton-cg', 'lbfgs', 'liblinear', 'sag'],
)
#Perform grid search using the classifier,parameter grid, scoring function and the cross-validated dataset
grid_search = GridSearchCV(clf, param_grid=param_grid, verbose=10,scoring=make_scorer(f1_score),cv=list(cv))
grid_search.fit(features.values,labels.values)
#To get the best score using the specified scoring function use the following
print grid_search.best_score_
#Similarly to get the best estimator
best_clf = grid_logistic.best_estimator_
print best_clf
您可以阅读更多关于它的文档here 以了解各种内部方法等,以检索最佳参数等。
随机搜索
sklearn 没有彻底检查超参数空间,而是实现了RandomizedSearchCV 来对参数进行随机搜索。该文档指出:
RandomizedSearchCV 实现了对参数的随机搜索,其中每个设置都是从可能的参数值的分布中采样的。
您可以从here 了解更多信息。
您可以阅读更多关于其他方法的信息here。
供参考的替代链接:
编辑:在您的情况下,如果您想最大化模型的召回率,您只需指定 recall_score from sklearn.metrics 作为评分函数。
如果您希望最大化问题中所述的“误报”,您可以参考this answer 从confusion matrix 中提取“误报”。然后使用make scorer函数并将其传递给GridSearchCV对象进行调优。