【发布时间】:2015-10-22 02:13:48
【问题描述】:
有没有办法对针对选定类的分数(例如“f1”)优化的参数值运行网格搜索,而不是所有类的默认分数?
[编辑] 假设这样一个网格搜索应该返回一组参数,使一个分数(例如,'f1'、'accuracy'、'recall')最大化,而不是所有类别的总分类。这种方法似乎很有用,例如对于高度不平衡的数据集,当尝试构建一个分类器时,该分类器在具有少量实例的类上执行合理的工作。
具有默认评分方法的 GridSearchCV 示例(此处:所有类的“f1”):
from __future__ import print_function
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4, 1e-5],
'C': [1, 50, 100, 500, 1000, 5000]},
{'kernel': ['linear'], 'C': [1, 100, 500, 1000, 5000]}]
clf = GridSearchCV(SVC(), tuned_parameters, cv=4, scoring='f1', n_jobs=-1)
clf.fit(X_train, y_train)
print("Best parameters set found on development set:")
print()
print(clf.best_estimator_)
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
如何优化参数以获得所选类的最佳性能,或在 GridSearchCV 中合并一系列 class_weight 的测试?
【问题讨论】:
标签: python scikit-learn