【发布时间】:2018-10-23 13:51:28
【问题描述】:
我正在尝试构建要在 GridSearhCV 对象中使用的自定义评分函数(使用 sklearn.metrics.make_scorer)。 make_scorer 的文档说:
score_func :可调用,评分函数(或损失函数) 签名
score_func(y, y_pred, **kwargs)。
这是我正在使用的代码:
class model(object):
def __init__(self):
pass
def fit(self, X, y):
score_func = make_scorer(self.make_custom_score)
clf = GradientBoostingClassifier()
model = GridSearchCV(estimator=clf,
param_grid=grid,
scoring=score_func,
cv=3)
model.fit(X, y)
return self
def make_custom_score(y_true, y_score):
df_out = pd.DataFrame()
df = pd.DataFrame({'true': y_true.tolist(), 'probability':
y_score.tolist()})
for threshold in np.arange(0.01, 1.0, 0.01):
above_thresh = df[df['probability'] > threshold].groupby('true').count().reset_index()
tp = above_thresh.loc[[1.0]]['probability'].sum()
df_threshold = pd.DataFrame({'threshold': [threshold], 'tp': tp})
df_out = df_out.append(df_threshold)
df_out = df_out.sort_values(by = ['threshold'], ascending = False)
tp_score = tp[5]
return tp_score
我得到的错误是:
TypeError: make_custom_score() 接受 2 个位置参数,但给出了 3 个。
我计划将来使用 **kwargs 为评分功能添加更多功能,因此如果可以的话,我想使用 make_scorer。
【问题讨论】:
-
看这个问题来解释自我:stackoverflow.com/questions/44741054/…
标签: python scikit-learn