本文整理匯總了Python中sklearn.metrics.explained_variance_score方法的典型用法代碼示例。如果您正苦於以下問題:Python metrics.explained_variance_score方法的具體用法?Python metrics.explained_variance_score怎麽用?Python metrics.explained_variance_score使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.metrics
的用法示例。
在下文中一共展示了metrics.explained_variance_score方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_metrics_from_list
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_metrics_from_list():
"""
Check getting functions from a list of metric names
"""
default = ModelBuilder.metrics_from_list()
assert default == [
metrics.explained_variance_score,
metrics.r2_score,
metrics.mean_squared_error,
metrics.mean_absolute_error,
]
specifics = ModelBuilder.metrics_from_list(
["sklearn.metrics.adjusted_mutual_info_score", "sklearn.metrics.r2_score"]
)
assert specifics == [metrics.adjusted_mutual_info_score, metrics.r2_score]
示例2: test_regression_metrics_at_limits
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_regression_metrics_at_limits():
assert_almost_equal(mean_squared_error([0.], [0.]), 0.00, 2)
assert_almost_equal(mean_squared_log_error([0.], [0.]), 0.00, 2)
assert_almost_equal(mean_absolute_error([0.], [0.]), 0.00, 2)
assert_almost_equal(median_absolute_error([0.], [0.]), 0.00, 2)
assert_almost_equal(max_error([0.], [0.]), 0.00, 2)
assert_almost_equal(explained_variance_score([0.], [0.]), 1.00, 2)
assert_almost_equal(r2_score([0., 1], [0., 1]), 1.00, 2)
assert_raises_regex(ValueError, "Mean Squared Logarithmic Error cannot be "
"used when targets contain negative values.",
mean_squared_log_error, [-1.], [-1.])
assert_raises_regex(ValueError, "Mean Squared Logarithmic Error cannot be "
"used when targets contain negative values.",
mean_squared_log_error, [1., 2., 3.], [1., -2., 3.])
assert_raises_regex(ValueError, "Mean Squared Logarithmic Error cannot be "
"used when targets contain negative values.",
mean_squared_log_error, [1., -2., 3.], [1., 2., 3.])
示例3: test_regression_custom_weights
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_regression_custom_weights():
y_true = [[1, 2], [2.5, -1], [4.5, 3], [5, 7]]
y_pred = [[1, 1], [2, -1], [5, 4], [5, 6.5]]
msew = mean_squared_error(y_true, y_pred, multioutput=[0.4, 0.6])
maew = mean_absolute_error(y_true, y_pred, multioutput=[0.4, 0.6])
rw = r2_score(y_true, y_pred, multioutput=[0.4, 0.6])
evsw = explained_variance_score(y_true, y_pred, multioutput=[0.4, 0.6])
assert_almost_equal(msew, 0.39, decimal=2)
assert_almost_equal(maew, 0.475, decimal=3)
assert_almost_equal(rw, 0.94, decimal=2)
assert_almost_equal(evsw, 0.94, decimal=2)
# Handling msle separately as it does not accept negative inputs.
y_true = np.array([[0.5, 1], [1, 2], [7, 6]])
y_pred = np.array([[0.5, 2], [1, 2.5], [8, 8]])
msle = mean_squared_log_error(y_true, y_pred, multioutput=[0.3, 0.7])
msle2 = mean_squared_error(np.log(1 + y_true), np.log(1 + y_pred),
multioutput=[0.3, 0.7])
assert_almost_equal(msle, msle2, decimal=2)
示例4: test_cross_val_score_with_score_func_regression
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [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)
示例5: eva_regress
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def eva_regress(y_true, y_pred):
"""Evaluation
evaluate the predicted resul.
# Arguments
y_true: List/ndarray, ture data.
y_pred: List/ndarray, predicted data.
"""
mape = MAPE(y_true, y_pred)
vs = metrics.explained_variance_score(y_true, y_pred)
mae = metrics.mean_absolute_error(y_true, y_pred)
mse = metrics.mean_squared_error(y_true, y_pred)
r2 = metrics.r2_score(y_true, y_pred)
print('explained_variance_score:%f' % vs)
print('mape:%f%%' % mape)
print('mae:%f' % mae)
print('mse:%f' % mse)
print('rmse:%f' % math.sqrt(mse))
print('r2:%f' % r2)
示例6: eval_metrics_on
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def eval_metrics_on(predictions, labels):
'''
assuming this is a regression task; labels are continuous-valued floats
returns most regression-related scores for the given predictions/targets as a dictionary:
r2, mean_abs_error, mse, rmse, median_absolute_error, explained_variance_score
'''
if len(labels[0])==2: #labels is list of data/labels pairs
labels = np.concatenate([l[1] for l in labels])
predictions = predictions[:,0]
r2 = metrics.r2_score(labels, predictions)
mean_abs_error = np.abs(predictions - labels).mean()
mse = ((predictions - labels)**2).mean()
rmse = np.sqrt(mse)
median_absolute_error = metrics.median_absolute_error(labels, predictions) # robust to outliers
explained_variance_score = metrics.explained_variance_score(labels, predictions) # best score = 1, lower is worse
return {'r2':r2, 'mean_abs_error':mean_abs_error, 'mse':mse, 'rmse':rmse,
'median_absolute_error':median_absolute_error,
'explained_variance_score':explained_variance_score}
示例7: test
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test(self):
"""
Tests the regressor using the dataset and writes:
1- coefficient r2
2- explained variance
3- mean absolute error
4- mean squared error
:return: None
"""
X = np.array([precedent['facts_vector'][self.important_facts_index] for precedent in self.dataset])
y_pred = self.model.predict(X)
y_true = np.array([precedent['outcomes_vector'][self.outcome_index]
for precedent in self.dataset])
r2 = metrics.r2_score(y_true, y_pred)
variance = metrics.explained_variance_score(y_true, y_pred)
mean_abs_error = metrics.mean_absolute_error(y_true, y_pred)
mean_squared_error = metrics.mean_squared_error(y_true, y_pred)
Log.write('R2: {0:.2f}'.format(r2))
Log.write('Explained Variance: {0:.2f}'.format(variance))
Log.write('Mean Absolute Error: {0:.2f}'.format(mean_abs_error))
Log.write('Mean Squared Error: {0:.2f}'.format(mean_squared_error))
示例8: test_explained_variance_score
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_explained_variance_score(self):
try:
from sklearn.metrics import explained_variance_score as skevs
except:
unittest.TestCase.skipTest(self, "sklearn is not found in the libraries")
skevs_score1 = skevs(self.local_reg1.target, self.local_reg1.p_target)
dlpyevs_score1 = explained_variance_score('target', 'p_target', castable=self.reg_table1)
self.assertAlmostEqual(skevs_score1, dlpyevs_score1)
skevs_score2 = skevs(self.local_reg1.target, self.local_reg2.p_target)
dlpyevs_score2 = explained_variance_score(self.reg_table1.target, self.reg_table2.p_target,
id_vars='id1')
self.assertAlmostEqual(skevs_score2, dlpyevs_score2)
示例9: grid_search_init_n_components
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
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)
示例10: test_multi
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_multi(self):
if os.path.isdir('sad/testm'):
shutil.rmtree('sad/testm')
sad_opts = '--rc --shifts "0,21"'
sad_opts += ' -o sad/testm -q "" -p 4'
cmd = 'basenji_sad_multi.py %s %s %s %s' % \
(sad_opts, self.params_file, self.model_file, self.vcf_file)
subprocess.call(cmd, shell=True)
saved_h5 = h5py.File('sad/saved/sad.h5', 'r')
this_h5 = h5py.File('sad/testm/sad.h5', 'r')
saved_keys = sorted(saved_h5.keys())
this_keys = sorted(this_h5.keys())
assert(len(saved_keys) == len(this_keys))
assert(saved_keys == this_keys)
for key in saved_h5:
if key[-4:] != '_pct':
saved_value = saved_h5[key][:]
this_value = this_h5[key][:]
if saved_value.dtype.char == 'S':
np.testing.assert_array_equal(saved_value, this_value)
else:
np.testing.assert_allclose(saved_value, this_value, atol=1e-1, rtol=5e-2)
r2 = explained_variance_score(saved_value.flatten(), this_value.flatten())
assert(r2 > 0.999)
saved_h5.close()
this_h5.close()
shutil.rmtree('sad/testm')
示例11: _calculate_optimal_reconstruction_orders
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def _calculate_optimal_reconstruction_orders(self,
timeseries,
components):
'''Calculates the optimal component ordering for reconstructing
each of the timeseries. This is done by simply ranking the components
in terms of how much variance they explain for each timeseries in the
original data.
'''
optimal_orders = optimal_component_ordering(
timeseries,
components
)
optimal_orders = optimal_orders.astype(int)
order_explained_variance = np.zeros_like(optimal_orders).astype(float)
for ts_idx in range(timeseries.shape[1]):
ts_comp = components[ts_idx, :, :]
ts_comp = ts_comp[:, optimal_orders[:, ts_idx]]
# ts_comp = np.cumsum(ts_comp, axis=1)
order_explained_variance[:, ts_idx] = np.apply_along_axis(
partial(explained_variance_score, timeseries[:, ts_idx]),
0,
ts_comp
)
return optimal_orders, order_explained_variance
示例12: test_model_builder_metrics_list
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_model_builder_metrics_list(metrics_: Optional[List[str]]):
model_config = {
"sklearn.multioutput.MultiOutputRegressor": {
"estimator": "sklearn.linear_model.LinearRegression"
}
}
data_config = get_random_data()
evaluation_config: Dict[str, Any] = {"cv_mode": "full_build"}
if metrics_:
evaluation_config.update({"metrics": metrics_})
machine = Machine(
name="model-name",
dataset=data_config,
model=model_config,
evaluation=evaluation_config,
project_name="test",
)
_model, machine = ModelBuilder(machine).build()
expected_metrics = metrics_ or [
"sklearn.metrics.explained_variance_score",
"sklearn.metrics.r2_score",
"sklearn.metrics.mean_squared_error",
"sklearn.metrics.mean_absolute_error",
]
assert all(
metric.split(".")[-1].replace("_", "-")
in machine.metadata.build_metadata.model.cross_validation.scores
for metric in expected_metrics
)
示例13: score
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def score(
self,
X: Union[np.ndarray, pd.DataFrame],
y: Union[np.ndarray, pd.DataFrame],
sample_weight: Optional[np.ndarray] = None,
) -> float:
"""
Returns the explained variance score between auto encoder's input vs output
Parameters
----------
X: Union[np.ndarray, pd.DataFrame]
Input data to the model
y: Union[np.ndarray, pd.DataFrame]
Target
sample_weight: Optional[np.ndarray]
sample weights
Returns
-------
score: float
Returns the explained variance score
"""
if not hasattr(self, "model"):
raise NotFittedError(
f"This {self.__class__.__name__} has not been fitted yet."
)
out = self.model.predict(X)
return explained_variance_score(y, out)
示例14: test_regression_metrics
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def test_regression_metrics(n_samples=50):
y_true = np.arange(n_samples)
y_pred = y_true + 1
assert_almost_equal(mean_squared_error(y_true, y_pred), 1.)
assert_almost_equal(mean_squared_log_error(y_true, y_pred),
mean_squared_error(np.log(1 + y_true),
np.log(1 + y_pred)))
assert_almost_equal(mean_absolute_error(y_true, y_pred), 1.)
assert_almost_equal(median_absolute_error(y_true, y_pred), 1.)
assert_almost_equal(max_error(y_true, y_pred), 1.)
assert_almost_equal(r2_score(y_true, y_pred), 0.995, 2)
assert_almost_equal(explained_variance_score(y_true, y_pred), 1.)
示例15: compute
# 需要導入模塊: from sklearn import metrics [as 別名]
# 或者: from sklearn.metrics import explained_variance_score [as 別名]
def compute(labels, pred_scores):
return explained_variance_score(labels, pred_scores)