[英]How to find out precision, recall, specificity & F1 score of the best model which is selected in Grid Search in Python?
This is the Grid Selection step for my model. I am able to find out the best accuracy score and best parameters of that selected model but I want to find out precision, recall, specificity & F1 score of the best model which is selected in Grid Search这是我的 model 的网格选择步骤。我能够找出所选 model 的最佳准确度分数和最佳参数,但我想找出在网格中选择的最佳 model 的精度、召回率、特异性和 F1 分数搜索
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [100, 200],
'max_features': ['auto', 'sqrt', None],
'max_depth': [10, 20, 30, None],
'criterion': ['gini', 'entropy'],
'min_samples_split': [5, 10,15], 'min_samples_leaf': [1,4,6],
'bootstrap': [True, False]}]
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 5,
n_jobs = -1) #n_jobs to optimise grid search process
grid_search.fit(X_train, Y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print("\n")
print("Results for Grid Search")
print("Best Accuracy: {:.2f} %".format(best_accuracy*100))
print("Best Parameters:", best_parameters)
GridSearchCV
enables passing a list of scoring functions, provided that you specify a single scoring function to which the best parameters will be chosen to refit the model. GridSearchCV
允许传递评分函数列表,前提是您指定单个评分 function,将选择最佳参数以重新拟合 model。
grid_search = GridSearchCV(
estimator=classifier,
param_grid=parameters,
scoring=['accuracy', 'f1', 'precision', 'recall'],
refit="accuracy", # Or any other value from `scoring` list
)
Then you can access the entire cross validation results in the cv_results_
attribute.然后您可以在cv_results_
属性中访问整个交叉验证结果。 It can be easier to first pack the cv_results_
as a pandas DataFrame and then access the row corresponding to best_index_
attribute:首先将cv_results_
为 pandas DataFrame 然后访问与best_index_
属性对应的行会更容易:
cv_results = pd.DataFrame(grid_search.cv_results_)
best_model_results = cv.results.loc[grid_search.best_index_]
The you get a Series indexed by score names, so you can access, for example, "mean_test_recall"
, "mean_test_f1"
, etc.你会得到一个由分数名称索引的系列,这样你就可以访问,例如, "mean_test_recall"
, "mean_test_f1"
等。
Note that, for your question, specificity is not a built-in scoring name , but you can also supply GridSearchCV
with custom metrics , so it's possible to pass a callable calculating it (say by using confusion matrix )请注意,对于您的问题,特异性不是内置的评分名称,但您也可以为GridSearchCV
提供自定义指标,因此可以通过可调用计算它(例如使用混淆矩阵)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.