首页 > 解决方案 > 用户标准和“make_scorer”

问题描述

  1. 我需要将参数(y_true,y_pred)传递给函数«make_scorer»吗?如果是这样,它们是如何传播的..?如果可以举个例子。
  2. 如何在“评分”中设置自定义标准?
  3. 每次迭代的结果是训练还是测试的结果?

_scorer = make_scorer(f1_score,pos_label=0)

grid_searcher = GridSearchCV(clf, parameter_grid, verbose=200, scoring=_scorer)
grid_searcher.fit(X_train, y_train)
clf_best = grid_searcher.best_estimator_

[CV] class_weight=balanced, max_depth=10, n_estimators=100 ........... 
[CV] class_weight=balanced, max_depth=10, n_estimators=100, score=0.4419706300331596, total= 16.4s 
[Parallel(n_jobs=1)]: Done 12 out of 12 | elapsed: 1.7min remaining: 0.0s 
[CV] class_weight=balanced, max_depth=10, n_estimators=150 > – user287629 47 mins ago  

y_pred = clf.predict (X_test) 
r = np.sum (y_pred == 0) & (y_pred == y_test) 
s = np.sum (y_pred == 1) & (y_pred! = y_test) 
z = r / s #I need to get a z 

标签: pythonscikit-learn

解决方案


试试这个:

def T_scorer(y_true, y_pred, **kwargs):
    epsilon = 1e-8  # think of the "computational stability" ("ZeroDivisionError")
    r = np.sum((y_pred == 0) & (y_pred == y_true))
    s = np.sum((y_pred == 1) & (y_pred != y_true))
    z = r / (s + epsilon)  # I need to get a z
    return z

_scorer = make_scorer(T_scorer)

grid_searcher = GridSearchCV(clf, parameter_grid, verbose=2, scoring=_scorer)

推荐阅读