只需要写一个自定义的评价函数,然后调用make_scorer即可
自定义评价函数
from sklearn.metrics import make_scorer
def customize_score(true_value, predict):
# 自定义函数,
# true_value 为series格式(index,value)
# predict 为ndarry格式[1,2,3,4...]
return 0.5
my_scorer = make_scorer(customize_score, greater_is_better=True)
直接使用自定义评价函数
from sklearn.linear_model import LinearRegression
liner_model = LinearRegression()
my_scorer(liner_model, features_test, target_test)
score = my_scorer(liner_model, x_test_std, y_test)
print(score)
在交叉验证中使用自定义评价函数
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_validate
liner_model = LinearRegression()
scoring = {
'customize_score': my_scorer
}
kfold = KFold(n_splits=10, random_state=0)
cv_cross = cross_validate(liner_model, x_train_std, y_train, cv=kfold, scoring=scoring)
print(cv_cross['test_customize_score'].mean()) # 交叉验证的均值
print(cv_cross['test_customize_score'].std()) # 交叉验证的方差
若使用网格搜索,只需要设置GridSearchCV(scoring=scoring)