本文整理汇总了Python中sklearn.metrics.make_scorer方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.make_scorer方法的具体用法?Python metrics.make_scorer怎么用?Python metrics.make_scorer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics
的用法示例。
在下文中一共展示了metrics.make_scorer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cross_val_score_with_score_func_regression
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例2: test_grid_search_cv_results_multimetric
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例3: apply_gridsearch
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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
示例4: mae_cv
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例5: mse_cv
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例6: tse_cv
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例7: tae_cv
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例8: test_with_make_scorer_accuracy_score
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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]])
示例9: convert_sklearn_metric_function
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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
示例10: test_with_gridsearchcv3_auto
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例11: test_with_randomizedsearchcv
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例12: test_clone_operator_choice
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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])
示例13: test_with_gridsearchcv_auto_wrapped_pipe1
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例14: backtest
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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)
示例15: crossvalidate_churn_model
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import make_scorer [as 别名]
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