Python源码示例:sklearn.metrics.make_scorer()
示例1
def backtest(data_set_path,n_test_split):
X,y = prepare_data(data_set_path,as_retention=False)
tscv = TimeSeriesSplit(n_splits=n_test_split)
lift_scorer = make_scorer(calc_lift, needs_proba=True)
score_models = {'lift': lift_scorer, 'AUC': 'roc_auc'}
retain_reg = LogisticRegression(penalty='l1', solver='liblinear', fit_intercept=True)
gsearch = GridSearchCV(estimator=retain_reg,scoring=score_models, cv=tscv, verbose=1,
return_train_score=False, param_grid={'C' : [1]}, refit='AUC')
gsearch.fit(X,y)
result_df = pd.DataFrame(gsearch.cv_results_)
save_path = data_set_path.replace('.csv', '_backtest.csv')
result_df.to_csv(save_path, index=False)
print('Saved test scores to ' + save_path)
示例2
def test_cross_val_score_with_score_func_regression():
X, y = make_regression(n_samples=30, n_features=20, n_informative=5,
random_state=0)
reg = Ridge()
# Default score of the Ridge regression estimator
scores = cross_val_score(reg, X, y, cv=5)
assert_array_almost_equal(scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
# R2 score (aka. determination coefficient) - should be the
# same as the default estimator score
r2_scores = cross_val_score(reg, X, y, scoring="r2", cv=5)
assert_array_almost_equal(r2_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
# Mean squared error; this is a loss function, so "scores" are negative
neg_mse_scores = cross_val_score(reg, X, y, cv=5,
scoring="neg_mean_squared_error")
expected_neg_mse = np.array([-763.07, -553.16, -274.38, -273.26, -1681.99])
assert_array_almost_equal(neg_mse_scores, expected_neg_mse, 2)
# Explained variance
scoring = make_scorer(explained_variance_score)
ev_scores = cross_val_score(reg, X, y, cv=5, scoring=scoring)
assert_array_almost_equal(ev_scores, [0.94, 0.97, 0.97, 0.99, 0.92], 2)
示例3
def test_grid_search_cv_results_multimetric():
X, y = make_classification(n_samples=50, n_features=4, random_state=42)
n_splits = 3
params = [dict(kernel=['rbf', ], C=[1, 10], gamma=[0.1, 1]),
dict(kernel=['poly', ], degree=[1, 2])]
for iid in (False, True):
grid_searches = []
for scoring in ({'accuracy': make_scorer(accuracy_score),
'recall': make_scorer(recall_score)},
'accuracy', 'recall'):
grid_search = GridSearchCV(SVC(gamma='scale'), cv=n_splits,
iid=iid, param_grid=params,
scoring=scoring, refit=False)
grid_search.fit(X, y)
assert_equal(grid_search.iid, iid)
grid_searches.append(grid_search)
compare_cv_results_multimetric_with_single(*grid_searches, iid=iid)
示例4
def apply_gridsearch(self,model):
"""
apply grid search on ml algorithm to specified parameters
returns updated best score and parameters
"""
# check if custom evalution function is specified
if callable(self.params_cv['scoring']):
scoring = make_scorer(self.params_cv['scoring'],greater_is_better=self._greater_is_better)
else:
scoring = self.params_cv['scoring']
gsearch = GridSearchCV(estimator=model,param_grid=self.get_params_tune(),scoring=scoring,
iid=self.params_cv['iid'],cv=self.params_cv['cv_folds'],n_jobs=self.params_cv['n_jobs'])
gsearch.fit(self.X,self.y)
# update best model if best_score is improved
if (gsearch.best_score_ * self._score_mult) > (self.best_score * self._score_mult):
self.best_model = clone(gsearch.best_estimator_)
self.best_score = gsearch.best_score_
# update tuned parameters with optimal values
for key,value in gsearch.best_params_.items():
self._params[key] = value
self._temp_score = gsearch.best_score_
return self
示例5
def mae_cv(self, cv):
"""
This method performs cross-validation over median absolute error.
Parameters
----------
* cv : integer
The number of cross validation folds to perform
Returns
-------
Returns a scores of the k-fold median absolute error.
"""
mae = metrics.make_scorer(metrics.median_absolute_error)
result = cross_validate(self.reg, self.X,
self.y, cv=cv,
scoring=(mae))
return self.get_test_score(result)
示例6
def mse_cv(self, cv):
"""
This method performs cross-validation over mean squared error.
Parameters
----------
* cv : integer
The number of cross validation folds to perform
Returns
-------
Returns a scores of the k-fold mean squared error.
"""
mse = metrics.make_scorer(metrics.mean_squared_error)
result = cross_validate(self.reg, self.X,
self.y, cv=cv,
scoring=(mse))
return self.get_test_score(result)
示例7
def tse_cv(self, cv):
"""
This method performs cross-validation over trimean squared error.
Parameters
----------
* cv : integer
The number of cross validation folds to perform
Returns
-------
Returns a scores of the k-fold trimean squared error.
"""
tse = metrics.make_scorer(self.trimean_squared_error)
result = cross_validate(self.reg, self.X,
self.y, cv=cv,
scoring=(tse))
return self.get_test_score(result)
示例8
def tae_cv(self, cv):
"""
This method performs cross-validation over trimean absolute error.
Parameters
----------
* cv : integer
The number of cross validation folds to perform
Returns
-------
Returns a scores of the k-fold trimean absolute error.
"""
tse = metrics.make_scorer(self.trimean_absolute_error)
result = cross_validate(self.reg, self.X,
self.y, cv=cv,
scoring=(tse))
return self.get_test_score(result)
示例9
def test_with_make_scorer_accuracy_score(
self, net_cls, module_cls, scoring_cls, train_split, data,
):
net = net_cls(
module_cls,
callbacks=[scoring_cls(make_scorer(accuracy_score))],
batch_size=1,
max_epochs=2,
train_split=train_split,
)
net.fit(*data)
score_epochs = net.history[:, 'accuracy_score']
assert np.allclose(score_epochs, [0, 0])
score_batches = net.history[:, 'batches', :, 'accuracy_score']
assert np.allclose(score_batches, [[0, 0], [0, 0]])
示例10
def convert_sklearn_metric_function(scoring):
"""If ``scoring`` is a sklearn metric function, convert it to a
sklearn scorer and return it. Otherwise, return ``scoring`` unchanged."""
if callable(scoring):
module = getattr(scoring, '__module__', None)
# those are scoring objects returned by make_scorer starting
# from sklearn 0.22
scorer_names = ('_PredictScorer', '_ProbaScorer', '_ThresholdScorer')
if (
hasattr(module, 'startswith') and
module.startswith('sklearn.metrics.') and
not module.startswith('sklearn.metrics.scorer') and
not module.startswith('sklearn.metrics.tests.') and
not scoring.__class__.__name__ in scorer_names
):
return make_scorer(scoring)
return scoring
示例11
def test_with_gridsearchcv3_auto(self):
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score, make_scorer
lr = LogisticRegression()
from sklearn.pipeline import Pipeline
scikit_pipeline = Pipeline([(Nystroem().name(), Nystroem()), (lr.name(), LogisticRegression())])
all_parameters = get_grid_search_parameter_grids(Nystroem()>>lr, num_samples=1)
# otherwise the test takes too long
parameters = random.sample(all_parameters, 2)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
clf = GridSearchCV(scikit_pipeline, parameters, cv=2, scoring=make_scorer(accuracy_score))
iris = load_iris()
clf.fit(iris.data, iris.target)
predicted = clf.predict(iris.data)
示例12
def test_with_randomizedsearchcv(self):
from sklearn.model_selection import RandomizedSearchCV
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score, make_scorer
from scipy.stats.distributions import uniform
import numpy as np
lr = LogisticRegression()
parameters = {'solver':('liblinear', 'lbfgs'), 'penalty':['l2']}
ranges, cat_idx = lr.get_param_ranges()
min_C, max_C, default_C = ranges['C']
# specify parameters and distributions to sample from
#the loguniform distribution needs to be taken care of properly
param_dist = {"solver": ranges['solver'],
"C": uniform(min_C, np.log(max_C))}
# run randomized search
n_iter_search = 5
with warnings.catch_warnings():
warnings.simplefilter("ignore")
random_search = RandomizedSearchCV(
lr, param_distributions=param_dist, n_iter=n_iter_search, cv=5,
scoring=make_scorer(accuracy_score))
iris = load_iris()
random_search.fit(iris.data, iris.target)
示例13
def test_clone_operator_choice(self):
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score, make_scorer
from sklearn.base import clone
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
lr = LogisticRegression()
trainable = PCA() >> lr
trainable_wrapper = make_sklearn_compat(trainable)
trainable2 = clone(trainable_wrapper)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
result = cross_val_score(trainable_wrapper, X, y,
scoring=make_scorer(accuracy_score), cv=2)
result2 = cross_val_score(trainable2, X, y,
scoring=make_scorer(accuracy_score), cv=2)
for i in range(len(result)):
self.assertEqual(result[i], result2[i])
示例14
def test_with_gridsearchcv_auto_wrapped_pipe1(self):
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score, make_scorer
lr = LogisticRegression()
pca = PCA()
trainable = pca >> lr
with warnings.catch_warnings():
warnings.simplefilter("ignore")
from lale.lib.lale import GridSearchCV
clf = GridSearchCV(
estimator=trainable, lale_num_samples=1, lale_num_grids=1,
cv=2, scoring=make_scorer(accuracy_score))
iris = load_iris()
clf.fit(iris.data, iris.target)
示例15
def crossvalidate_churn_model(self,model_code,groups=True):
X,y = self.prepare_xy(groups)
params = self.cv_params(model_code)
model = self.model_instance(model_code)
tscv = TimeSeriesSplit(n_splits=3)
lift_scorer = make_scorer(top_decile_lift,needs_proba=True)
score_models = {'lift_scorer' : lift_scorer, 'AUC' : 'roc_auc'}
gsearch = GridSearchCV(estimator=model, param_grid=params, scoring=score_models, cv=tscv, n_jobs=8,verbose=5,
return_train_score=True,refit='AUC')
gsearch.fit(X, y)
result_df = pd.DataFrame(gsearch.cv_results_)
if len(params)>1:
result_df.sort_values('mean_test_AUC',ascending=False,inplace=True)
save_file_name = model_code + '_CV'
save_path = self.save_path(save_file_name, subdir=self.grouping_correlation_subdir(groups))
result_df.to_csv(save_path)
print('Saved result to ' + save_path)
return result_df
示例16
def check_scoring(estimator, scoring=None, **kwargs):
res = sklearn_check_scoring(estimator, scoring=scoring, **kwargs)
if callable(scoring):
# Heuristic to ensure user has not passed a metric
module = getattr(scoring, "__module__", None)
if (
hasattr(module, "startswith")
and module.startswith("dask_ml.metrics.")
and not module.startswith("dask_ml.metrics.scorer")
and not module.startswith("dask_ml.metrics.tests.")
):
raise ValueError(
"scoring value %r looks like it is a metric "
"function rather than a scorer. A scorer should "
"require an estimator as its first parameter. "
"Please use `make_scorer` to convert a metric "
"to a scorer." % scoring
)
if scoring in SCORERS.keys():
func, kwargs = SCORERS[scoring]
return make_scorer(func, **kwargs)
return res
示例17
def test_sklearn_custom_scoring_and_cv(tmp_dir):
tuner = sklearn_tuner.Sklearn(
oracle=kt.oracles.BayesianOptimization(
objective=kt.Objective('score', 'max'),
max_trials=10),
hypermodel=build_model,
scoring=metrics.make_scorer(metrics.balanced_accuracy_score),
cv=model_selection.StratifiedKFold(5),
directory=tmp_dir)
x = np.random.uniform(size=(50, 10))
y = np.random.randint(0, 2, size=(50,))
tuner.search(x, y)
assert len(tuner.oracle.trials) == 10
best_trial = tuner.oracle.get_best_trials()[0]
assert best_trial.status == 'COMPLETED'
assert best_trial.score is not None
assert best_trial.best_step == 0
assert best_trial.metrics.exists('score')
# Make sure best model can be reloaded.
best_model = tuner.get_best_models()[0]
best_model.score(x, y)
示例18
def test_sklearn_real_data(tmp_dir):
tuner = sklearn_tuner.Sklearn(
oracle=kt.oracles.BayesianOptimization(
objective=kt.Objective('score', 'max'),
max_trials=10),
hypermodel=build_model,
scoring=metrics.make_scorer(metrics.accuracy_score),
cv=model_selection.StratifiedKFold(5),
directory=tmp_dir)
x, y = datasets.load_iris(return_X_y=True)
x_train, x_test, y_train, y_test = model_selection.train_test_split(
x, y, test_size=0.2)
tuner.search(x_train, y_train)
best_models = tuner.get_best_models(10)
best_model = best_models[0]
worst_model = best_models[9]
best_model_score = best_model.score(x_test, y_test)
worst_model_score = worst_model.score(x_test, y_test)
assert best_model_score > 0.8
assert best_model_score >= worst_model_score
示例19
def grid_search_init_n_components(estimator, x, y, n_components_range=None, cv=10, n_jobs=-1,
scoring=None, show=True):
"""
封装grid search特定的'n_components'关键字参数最优搜索,
为AbuMLCreater中_estimators_prarms_best提供callback函数,
具体阅读AbuMLCreater._estimators_prarms_best()
:param estimator: 学习器对象
:param x: 训练集x矩阵,numpy矩阵
:param y: 训练集y序列,numpy序列
:param n_components_range: 默认None, None则会使用:
n_estimators_range = np.arange(2, np.maximum(10, int(x.shape[1]) - 1), 1)
:param cv: int,GridSearchCV切割训练集测试集参数,默认10
:param n_jobs: 并行执行的进程任务数量,默认-1, 开启与cpu相同数量的进程数
:param scoring: 测试集的度量方法,默认为None, None的情况下分类器使用accuracy进行度量,回归器使用
回归器使用可释方差值explained_variance_score,使用make_scorer对函数进行score封装
:param show: 是否进行可视化
:return: eg: (0.82154882154882158, {'n_components': 10})
"""
if n_components_range is None:
n_components_range = np.arange(2, np.maximum(10, int(x.shape[1]) - 1), 1)
return grid_search_init_kwargs(estimator, x, y, 'n_components', n_components_range,
cv=cv, n_jobs=n_jobs, scoring=scoring, show=show)
示例20
def estimate_predictive_performance(x_y,
estimator=None,
n_splits=10,
random_state=1):
"""estimate_predictive_performance."""
x, y = x_y
cv = ShuffleSplit(n_splits=n_splits,
test_size=0.3,
random_state=random_state)
scoring = make_scorer(average_precision_score)
scores = cross_val_score(estimator, x, y, cv=cv, scoring=scoring)
return scores
示例21
def validate_model(task, cv: int) -> None:
x, y = task.create_train_data()
model = task.create_model()
scores = []
def _scoring(y_true, y_pred):
report = classification_report(y_true, y_pred, output_dict=True)
logger.info(report)
scores.append(report)
return accuracy_score(y_true, y_pred)
cross_val_score(model, x, y, cv=cv, scoring=make_scorer(_scoring))
task.dump(scores)
示例22
def test_make_scorer():
# Sanity check on the make_scorer factory function.
f = lambda *args: 0
assert_raises(ValueError, make_scorer, f, needs_threshold=True,
needs_proba=True)
示例23
def test_raises_on_score_list():
# Test that when a list of scores is returned, we raise proper errors.
X, y = make_blobs(random_state=0)
f1_scorer_no_average = make_scorer(f1_score, average=None)
clf = DecisionTreeClassifier()
assert_raises(ValueError, cross_val_score, clf, X, y,
scoring=f1_scorer_no_average)
grid_search = GridSearchCV(clf, scoring=f1_scorer_no_average,
param_grid={'max_depth': [1, 2]})
assert_raises(ValueError, grid_search.fit, X, y)
示例24
def test_scoring_is_not_metric():
assert_raises_regexp(ValueError, 'make_scorer', check_scoring,
LogisticRegression(), f1_score)
assert_raises_regexp(ValueError, 'make_scorer', check_scoring,
LogisticRegression(), roc_auc_score)
assert_raises_regexp(ValueError, 'make_scorer', check_scoring,
Ridge(), r2_score)
assert_raises_regexp(ValueError, 'make_scorer', check_scoring,
KMeans(), cluster_module.adjusted_rand_score)
示例25
def test_cross_val_score_score_func():
clf = MockClassifier()
_score_func_args = []
def score_func(y_test, y_predict):
_score_func_args.append((y_test, y_predict))
return 1.0
with warnings.catch_warnings(record=True):
scoring = make_scorer(score_func)
score = cross_val_score(clf, X, y, scoring=scoring, cv=3)
assert_array_equal(score, [1.0, 1.0, 1.0])
# Test that score function is called only 3 times (for cv=3)
assert len(_score_func_args) == 3
示例26
def test_cross_val_score_multilabel():
X = np.array([[-3, 4], [2, 4], [3, 3], [0, 2], [-3, 1],
[-2, 1], [0, 0], [-2, -1], [-1, -2], [1, -2]])
y = np.array([[1, 1], [0, 1], [0, 1], [0, 1], [1, 1],
[0, 1], [1, 0], [1, 1], [1, 0], [0, 0]])
clf = KNeighborsClassifier(n_neighbors=1)
scoring_micro = make_scorer(precision_score, average='micro')
scoring_macro = make_scorer(precision_score, average='macro')
scoring_samples = make_scorer(precision_score, average='samples')
score_micro = cross_val_score(clf, X, y, scoring=scoring_micro, cv=5)
score_macro = cross_val_score(clf, X, y, scoring=scoring_macro, cv=5)
score_samples = cross_val_score(clf, X, y, scoring=scoring_samples, cv=5)
assert_almost_equal(score_micro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 3])
assert_almost_equal(score_macro, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4])
assert_almost_equal(score_samples, [1, 1 / 2, 3 / 4, 1 / 2, 1 / 4])
示例27
def test_grid_search_sparse_scoring():
X_, y_ = make_classification(n_samples=200, n_features=100, random_state=0)
clf = LinearSVC()
cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1")
cv.fit(X_[:180], y_[:180])
y_pred = cv.predict(X_[180:])
C = cv.best_estimator_.C
X_ = sp.csr_matrix(X_)
clf = LinearSVC()
cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring="f1")
cv.fit(X_[:180], y_[:180])
y_pred2 = cv.predict(X_[180:])
C2 = cv.best_estimator_.C
assert_array_equal(y_pred, y_pred2)
assert_equal(C, C2)
# Smoke test the score
# np.testing.assert_allclose(f1_score(cv.predict(X_[:180]), y[:180]),
# cv.score(X_[:180], y[:180]))
# test loss where greater is worse
def f1_loss(y_true_, y_pred_):
return -f1_score(y_true_, y_pred_)
F1Loss = make_scorer(f1_loss, greater_is_better=False)
cv = GridSearchCV(clf, {'C': [0.1, 1.0]}, scoring=F1Loss)
cv.fit(X_[:180], y_[:180])
y_pred3 = cv.predict(X_[180:])
C3 = cv.best_estimator_.C
assert_equal(C, C3)
assert_array_equal(y_pred, y_pred3)
示例28
def test_fit_grid_point():
X, y = make_classification(random_state=0)
cv = StratifiedKFold(random_state=0)
svc = LinearSVC(random_state=0)
scorer = make_scorer(accuracy_score)
for params in ({'C': 0.1}, {'C': 0.01}, {'C': 0.001}):
for train, test in cv.split(X, y):
this_scores, this_params, n_test_samples = fit_grid_point(
X, y, clone(svc), params, train, test,
scorer, verbose=False)
est = clone(svc).set_params(**params)
est.fit(X[train], y[train])
expected_score = scorer(est, X[test], y[test])
# Test the return values of fit_grid_point
assert_almost_equal(this_scores, expected_score)
assert_equal(params, this_params)
assert_equal(n_test_samples, test.size)
# Should raise an error upon multimetric scorer
assert_raise_message(ValueError, "For evaluating multiple scores, use "
"sklearn.model_selection.cross_validate instead.",
fit_grid_point, X, y, svc, params, train, test,
{'score': scorer}, verbose=True)
示例29
def main():
import sys
import numpy as np
from sklearn import cross_validation
from sklearn import svm
import cPickle
data_dir = sys.argv[1]
fet_list = load_list(osp.join(data_dir, 'c3d.list'))
pos_list = load_list(osp.join(data_dir, 'pos.urls'))
features = np.load(osp.join(data_dir, 'c3d.npy'))
fet_set = set(fet_list)
pos_idx = [fet_list.index(i) for i in pos_list if i in fet_set]
y = np.zeros(features.shape[0])
y[pos_idx] = 1
print 'n_pos', np.sum(y), 'n_neg', np.sum(1 - y)
params = {'n_estimators':[2, 4, 5, 6, 8, 10, 30]}
#params = {'n_estimators':[50, 70, 100, 120, 150, 200]}
clf = grid_search.GridSearchCV(RandomForestClassifier(n_estimators = 2, n_jobs = 4), params, scoring = metrics.make_scorer(lambda yt, yp: metrics.f1_score(yt, yp, pos_label = 0)), cv = 5)
clf.fit(features, y)
print clf.best_score_
print clf.best_estimator_
cPickle.dump(clf.best_estimator_, open(osp.join(data_dir, 'c3d-models-rfc.pkl'), 'w'))
示例30
def _score(self, y_true, y_pred, method, plot_type, score_name):
""" scoring function to be passed to make_scorer.
"""
treatment_true, outcome_true, p = self.untransform(y_true)
scores = get_scores(treatment_true, outcome_true, y_pred, p, scoring_range=(0,self.scoring_cutoff[method]), plot_type=plot_type)
return scores[score_name]