我正在尝试实现顶级的召回/精度评分函数,以插入GridSearchCV。但是,我无法弄清楚怎么了。我想做的是让我的评分函数在概率预测,实际标签以及理想情况下以百分比为单位的阈值中使用。然后,我将排序分数对分数进行排序,然后确定在十分阈值之内的转换率。例如。前10%的人口的转化率。转换率将是我输出的分数。越高越好。但是,当我在下面运行代码时,我没有获得概率分数,也不了解评分函数的输入是什么。下面的打印语句仅返回1和0而不是概率。
def top_decile_conversion_rate(y_prob, y_actual):
# Function goes in here
print y_prob, y_actual
return 0.5
features = pd.DataFrame({"f1":np.random.randint(1,1000,500) , "f2":np.random.randint(1,1000,500),
"label":[round(x) for x in np.random.random_sample(500)]})
my_scorer = make_scorer(top_decile_conversion_rate, greater_is_better=True)
gs = grid_search.GridSearchCV(
estimator=LogisticRegression(),
param_grid={'C': [i for i in range(1, 3)], 'class_weight': [None], 'penalty':['l2']},
cv=2,
scoring=my_scorer )
model = gs.fit(features[["f1","f2"]], features.label)
解决方案是在添加一个称为seads_proba = true的参数中,在make_scorer函数中!这样可以。
def top_decile_conversion_rate(y_prob, y_actual):
# Function goes in here
print "---prob--"
print y_prob
print "---actual--"
print y_actual
print "---end--"
return 0.5
features = pd.DataFrame({"f1":np.random.randint(1,1000,500) , "f2":np.random.randint(1,1000,500),
"label":[round(x) for x in np.random.random_sample(500)]})
my_scorer = make_scorer(top_decile_conversion_rate, greater_is_better=True,needs_proba=True)
gs = grid_search.GridSearchCV(
estimator=LogisticRegression(),
param_grid={'C': [i for i in range(1, 3)], 'class_weight': [None], 'penalty':['l2']},
cv=20,
scoring=my_scorer )
model = gs.fit(features[["f1","f2"]], features.label)