本文整理汇总了Python中sklearn.utils.testing.assert_allclose函数的典型用法代码示例。如果您正苦于以下问题:Python assert_allclose函数的具体用法?Python assert_allclose怎么用?Python assert_allclose使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_allclose函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_allknn_fit_resample
def test_allknn_fit_resample():
allknn = AllKNN()
X_resampled, y_resampled = allknn.fit_resample(X, Y)
X_gt = np.array([[-0.53171468, -0.53735182], [-0.88864036, -0.33782387], [
-0.46226554, -0.50481004
], [-0.34474418, 0.21969797], [1.02956816, 0.36061601], [
1.12202806, 0.33811558
], [-1.10146139, 0.91782682], [0.73489726, 0.43915195], [
0.50307437, 0.498805
], [0.84929742, 0.41042894], [0.62649535, 0.46600596], [
0.98382284, 0.37184502
], [0.69804044, 0.44810796], [0.04296502, -0.37981873], [
0.28294738, -1.00125525
], [0.34218094, -0.58781961], [0.2096964, -0.61814058], [
1.59068979, -0.96622933
], [0.73418199, -0.02222847], [0.79270821, -0.41386668], [
1.16606871, -0.25641059
], [1.0304995, -0.16955962], [0.48921682, -1.38504507],
[-0.03918551, -0.68540745], [0.24991051, -1.00864997],
[0.80541964, -0.34465185], [0.1732627, -1.61323172]])
y_gt = np.array([
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2
])
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_allclose(y_resampled, y_gt, rtol=R_TOL)
开发者ID:bodycat,项目名称:imbalanced-learn,代码行数:27,代码来源:test_allknn.py
示例2: test_ridge_regression_dtype_stability
def test_ridge_regression_dtype_stability(solver):
random_state = np.random.RandomState(0)
n_samples, n_features = 6, 5
X = random_state.randn(n_samples, n_features)
coef = random_state.randn(n_features)
y = np.dot(X, coef) + 0.01 * rng.randn(n_samples)
alpha = 1.0
rtol = 1e-2 if os.name == 'nt' and _IS_32BIT else 1e-5
results = dict()
for current_dtype in (np.float32, np.float64):
results[current_dtype] = ridge_regression(X.astype(current_dtype),
y.astype(current_dtype),
alpha=alpha,
solver=solver,
random_state=random_state,
sample_weight=None,
max_iter=500,
tol=1e-10,
return_n_iter=False,
return_intercept=False)
assert results[np.float32].dtype == np.float32
assert results[np.float64].dtype == np.float64
assert_allclose(results[np.float32], results[np.float64], rtol=rtol)
开发者ID:kevin-coder,项目名称:scikit-learn-fork,代码行数:25,代码来源:test_ridge.py
示例3: test_partial_dependence_helpers
def test_partial_dependence_helpers(est, method, target_feature):
# Check that what is returned by _partial_dependence_brute or
# _partial_dependence_recursion is equivalent to manually setting a target
# feature to a given value, and computing the average prediction over all
# samples.
# This also checks that the brute and recursion methods give the same
# output.
X, y = make_regression(random_state=0)
# The 'init' estimator for GBDT (here the average prediction) isn't taken
# into account with the recursion method, for technical reasons. We set
# the mean to 0 to that this 'bug' doesn't have any effect.
y = y - y.mean()
est.fit(X, y)
# target feature will be set to .5 and then to 123
features = np.array([target_feature], dtype=np.int32)
grid = np.array([[.5],
[123]])
if method == 'brute':
pdp = _partial_dependence_brute(est, grid, features, X,
response_method='auto')
else:
pdp = _partial_dependence_recursion(est, grid, features)
mean_predictions = []
for val in (.5, 123):
X_ = X.copy()
X_[:, target_feature] = val
mean_predictions.append(est.predict(X_).mean())
pdp = pdp[0] # (shape is (1, 2) so make it (2,))
assert_allclose(pdp, mean_predictions, atol=1e-3)
开发者ID:manhhomienbienthuy,项目名称:scikit-learn,代码行数:34,代码来源:test_partial_dependence.py
示例4: test_wpearsonr
def test_wpearsonr(self):
# TODO: if unweight version changes, wp[0] format should be changed
wp = wpearsonr(self.a, self.b)
assert_allclose(wp[0], 0.6956083, atol=0.01)
wp = wpearsonr(self.a, self.b, w=self.w)
assert_allclose(wp, 0.5477226, atol=0.01)
开发者ID:flaviassantos,项目名称:pyod,代码行数:7,代码来源:test_stat_models.py
示例5: test_score_to_label
def test_score_to_label(self):
manual_scores = [0.1, 0.4, 0.2, 0.3, 0.5, 0.9, 0.7, 1, 0.8, 0.6]
labels = score_to_label(manual_scores, outliers_fraction=0.1)
assert_allclose(labels, [0, 0, 0, 0, 0, 0, 0, 1, 0, 0])
labels = score_to_label(manual_scores, outliers_fraction=0.3)
assert_allclose(labels, [0, 0, 0, 0, 0, 1, 0, 1, 1, 0])
开发者ID:flaviassantos,项目名称:pyod,代码行数:7,代码来源:test_utility.py
示例6: test_iterative_imputer_additive_matrix
def test_iterative_imputer_additive_matrix():
rng = np.random.RandomState(0)
n = 100
d = 10
A = rng.randn(n, d)
B = rng.randn(n, d)
X_filled = np.zeros(A.shape)
for i in range(d):
for j in range(d):
X_filled[:, (i+j) % d] += (A[:, i] + B[:, j]) / 2
# a quarter is randomly missing
nan_mask = rng.rand(n, d) < 0.25
X_missing = X_filled.copy()
X_missing[nan_mask] = np.nan
# split up data
n = n // 2
X_train = X_missing[:n]
X_test_filled = X_filled[n:]
X_test = X_missing[n:]
imputer = IterativeImputer(max_iter=10,
verbose=1,
random_state=rng).fit(X_train)
X_test_est = imputer.transform(X_test)
assert_allclose(X_test_filled, X_test_est, rtol=1e-3, atol=0.01)
开发者ID:psorianom,项目名称:scikit-learn,代码行数:26,代码来源:test_impute.py
示例7: test_iterative_imputer_early_stopping
def test_iterative_imputer_early_stopping():
rng = np.random.RandomState(0)
n = 50
d = 5
A = rng.rand(n, 1)
B = rng.rand(1, d)
X = np.dot(A, B)
nan_mask = rng.rand(n, d) < 0.5
X_missing = X.copy()
X_missing[nan_mask] = np.nan
imputer = IterativeImputer(max_iter=100,
tol=1e-3,
sample_posterior=False,
verbose=1,
random_state=rng)
X_filled_100 = imputer.fit_transform(X_missing)
assert len(imputer.imputation_sequence_) == d * imputer.n_iter_
imputer = IterativeImputer(max_iter=imputer.n_iter_,
sample_posterior=False,
verbose=1,
random_state=rng)
X_filled_early = imputer.fit_transform(X_missing)
assert_allclose(X_filled_100, X_filled_early, atol=1e-7)
imputer = IterativeImputer(max_iter=100,
tol=0,
sample_posterior=False,
verbose=1,
random_state=rng)
imputer.fit(X_missing)
assert imputer.n_iter_ == imputer.max_iter
开发者ID:psorianom,项目名称:scikit-learn,代码行数:33,代码来源:test_impute.py
示例8: test_dtype_match
def test_dtype_match(solver):
rng = np.random.RandomState(0)
alpha = 1.0
n_samples, n_features = 6, 5
X_64 = rng.randn(n_samples, n_features)
y_64 = rng.randn(n_samples)
X_32 = X_64.astype(np.float32)
y_32 = y_64.astype(np.float32)
# Check type consistency 32bits
ridge_32 = Ridge(alpha=alpha, solver=solver, max_iter=500, tol=1e-10,)
ridge_32.fit(X_32, y_32)
coef_32 = ridge_32.coef_
# Check type consistency 64 bits
ridge_64 = Ridge(alpha=alpha, solver=solver, max_iter=500, tol=1e-10,)
ridge_64.fit(X_64, y_64)
coef_64 = ridge_64.coef_
# Do the actual checks at once for easier debug
assert coef_32.dtype == X_32.dtype
assert coef_64.dtype == X_64.dtype
assert ridge_32.predict(X_32).dtype == X_32.dtype
assert ridge_64.predict(X_64).dtype == X_64.dtype
assert_allclose(ridge_32.coef_, ridge_64.coef_, rtol=1e-4)
开发者ID:manhhomienbienthuy,项目名称:scikit-learn,代码行数:26,代码来源:test_ridge.py
示例9: test_fit_resample_nn_obj
def test_fit_resample_nn_obj():
kind = 'borderline1'
nn_m = NearestNeighbors(n_neighbors=11)
nn_k = NearestNeighbors(n_neighbors=6)
smote = SMOTE(
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, m_neighbors=nn_m)
X_resampled, y_resampled = smote.fit_resample(X, Y)
X_gt = np.array([[0.11622591, -0.0317206], [0.77481731, 0.60935141], [
1.25192108, -0.22367336
], [0.53366841, -0.30312976], [1.52091956, -0.49283504], [
-0.28162401, -2.10400981
], [0.83680821, 1.72827342], [0.3084254, 0.33299982], [
0.70472253, -0.73309052
], [0.28893132, -0.38761769], [1.15514042, 0.0129463], [
0.88407872, 0.35454207
], [1.31301027, -0.92648734], [-1.11515198, -0.93689695], [
-0.18410027, -0.45194484
], [0.9281014, 0.53085498], [-0.14374509, 0.27370049], [
-0.41635887, -0.38299653
], [0.08711622, 0.93259929], [1.70580611, -0.11219234],
[0.3765279, -0.2009615], [0.55276636, -0.10550373],
[0.45413452, -0.08883319], [1.21118683, -0.22817957]])
y_gt = np.array([
0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0
])
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_array_equal(y_resampled, y_gt)
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:27,代码来源:test_smote.py
示例10: test_sample_with_nn_svm
def test_sample_with_nn_svm():
kind = 'svm'
nn_k = NearestNeighbors(n_neighbors=6)
svm = SVC(gamma='scale', random_state=RND_SEED)
smote = SMOTE(
random_state=RND_SEED, kind=kind, k_neighbors=nn_k, svm_estimator=svm)
X_resampled, y_resampled = smote.fit_resample(X, Y)
X_gt = np.array([[0.11622591, -0.0317206],
[0.77481731, 0.60935141],
[1.25192108, -0.22367336],
[0.53366841, -0.30312976],
[1.52091956, -0.49283504],
[-0.28162401, -2.10400981],
[0.83680821, 1.72827342],
[0.3084254, 0.33299982],
[0.70472253, -0.73309052],
[0.28893132, -0.38761769],
[1.15514042, 0.0129463],
[0.88407872, 0.35454207],
[1.31301027, -0.92648734],
[-1.11515198, -0.93689695],
[-0.18410027, -0.45194484],
[0.9281014, 0.53085498],
[-0.14374509, 0.27370049],
[-0.41635887, -0.38299653],
[0.08711622, 0.93259929],
[1.70580611, -0.11219234],
[0.47436887, -0.2645749],
[1.07844562, -0.19435291],
[1.44228238, -1.31256615],
[1.25636713, -1.04463226]])
y_gt = np.array([0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0])
assert_allclose(X_resampled, X_gt, rtol=R_TOL)
assert_array_equal(y_resampled, y_gt)
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:35,代码来源:test_smote.py
示例11: test_multilabel_representation_invariance
def test_multilabel_representation_invariance():
# Generate some data
n_classes = 4
n_samples = 50
_, y1 = make_multilabel_classification(n_features=1, n_classes=n_classes,
random_state=0, n_samples=n_samples,
allow_unlabeled=True)
_, y2 = make_multilabel_classification(n_features=1, n_classes=n_classes,
random_state=1, n_samples=n_samples,
allow_unlabeled=True)
# To make sure at least one empty label is present
y1 = np.vstack([y1, [[0] * n_classes]])
y2 = np.vstack([y2, [[0] * n_classes]])
y1_sparse_indicator = sp.coo_matrix(y1)
y2_sparse_indicator = sp.coo_matrix(y2)
for name in MULTILABELS_METRICS:
metric = ALL_METRICS[name]
# XXX cruel hack to work with partial functions
if isinstance(metric, partial):
metric.__module__ = 'tmp'
metric.__name__ = name
measure = metric(y1, y2)
# Check representation invariance
assert_allclose(metric(y1_sparse_indicator, y2_sparse_indicator),
measure,
err_msg="%s failed representation invariance between "
"dense and sparse indicator formats." % name)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:34,代码来源:test_common.py
示例12: test_normalize_option_multilabel_classification
def test_normalize_option_multilabel_classification():
# Test in the multilabel case
n_classes = 4
n_samples = 100
# for both random_state 0 and 1, y_true and y_pred has at least one
# unlabelled entry
_, y_true = make_multilabel_classification(n_features=1,
n_classes=n_classes,
random_state=0,
allow_unlabeled=True,
n_samples=n_samples)
_, y_pred = make_multilabel_classification(n_features=1,
n_classes=n_classes,
random_state=1,
allow_unlabeled=True,
n_samples=n_samples)
# To make sure at least one empty label is present
y_true += [0]*n_classes
y_pred += [0]*n_classes
for name in METRICS_WITH_NORMALIZE_OPTION:
metrics = ALL_METRICS[name]
measure = metrics(y_true, y_pred, normalize=True)
assert_array_less(-1.0 * measure, 0,
err_msg="We failed to test correctly the normalize "
"option")
assert_allclose(metrics(y_true, y_pred, normalize=False) / n_samples,
measure, err_msg="Failed with %s" % name)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:30,代码来源:test_common.py
示例13: test_symmetry
def test_symmetry():
# Test the symmetry of score and loss functions
random_state = check_random_state(0)
y_true = random_state.randint(0, 2, size=(20, ))
y_pred = random_state.randint(0, 2, size=(20, ))
# We shouldn't forget any metrics
assert_equal(SYMMETRIC_METRICS.union(
NOT_SYMMETRIC_METRICS, set(THRESHOLDED_METRICS),
METRIC_UNDEFINED_BINARY_MULTICLASS),
set(ALL_METRICS))
assert_equal(
SYMMETRIC_METRICS.intersection(NOT_SYMMETRIC_METRICS),
set([]))
# Symmetric metric
for name in SYMMETRIC_METRICS:
metric = ALL_METRICS[name]
assert_allclose(metric(y_true, y_pred), metric(y_pred, y_true),
err_msg="%s is not symmetric" % name)
# Not symmetric metrics
for name in NOT_SYMMETRIC_METRICS:
metric = ALL_METRICS[name]
# use context manager to supply custom error message
with assert_raises(AssertionError) as cm:
assert_array_equal(metric(y_true, y_pred), metric(y_pred, y_true))
cm.msg = ("%s seems to be symmetric" % name)
开发者ID:SuryodayBasak,项目名称:scikit-learn,代码行数:30,代码来源:test_common.py
示例14: test_ridge_gcv_vs_ridge_loo_cv
def test_ridge_gcv_vs_ridge_loo_cv(
gcv_mode, X_constructor, X_shape, y_shape,
fit_intercept, normalize, noise):
n_samples, n_features = X_shape
n_targets = y_shape[-1] if len(y_shape) == 2 else 1
X, y = _make_sparse_offset_regression(
n_samples=n_samples, n_features=n_features, n_targets=n_targets,
random_state=0, shuffle=False, noise=noise, n_informative=5
)
y = y.reshape(y_shape)
alphas = [1e-3, .1, 1., 10., 1e3]
loo_ridge = RidgeCV(cv=n_samples, fit_intercept=fit_intercept,
alphas=alphas, scoring='neg_mean_squared_error',
normalize=normalize)
gcv_ridge = RidgeCV(gcv_mode=gcv_mode, fit_intercept=fit_intercept,
alphas=alphas, normalize=normalize)
loo_ridge.fit(X, y)
X_gcv = X_constructor(X)
gcv_ridge.fit(X_gcv, y)
assert gcv_ridge.alpha_ == pytest.approx(loo_ridge.alpha_)
assert_allclose(gcv_ridge.coef_, loo_ridge.coef_, rtol=1e-3)
assert_allclose(gcv_ridge.intercept_, loo_ridge.intercept_, rtol=1e-3)
开发者ID:manhhomienbienthuy,项目名称:scikit-learn,代码行数:26,代码来源:test_ridge.py
示例15: test_ridge_regression_dtype_stability
def test_ridge_regression_dtype_stability(solver, seed):
random_state = np.random.RandomState(seed)
n_samples, n_features = 6, 5
X = random_state.randn(n_samples, n_features)
coef = random_state.randn(n_features)
y = np.dot(X, coef) + 0.01 * random_state.randn(n_samples)
alpha = 1.0
results = dict()
# XXX: Sparse CG seems to be far less numerically stable than the
# others, maybe we should not enable float32 for this one.
atol = 1e-3 if solver == "sparse_cg" else 1e-5
for current_dtype in (np.float32, np.float64):
results[current_dtype] = ridge_regression(X.astype(current_dtype),
y.astype(current_dtype),
alpha=alpha,
solver=solver,
random_state=random_state,
sample_weight=None,
max_iter=500,
tol=1e-10,
return_n_iter=False,
return_intercept=False)
assert results[np.float32].dtype == np.float32
assert results[np.float64].dtype == np.float64
assert_allclose(results[np.float32], results[np.float64], atol=atol)
开发者ID:manhhomienbienthuy,项目名称:scikit-learn,代码行数:26,代码来源:test_ridge.py
示例16: test_transform_target_regressor_multi_to_single
def test_transform_target_regressor_multi_to_single():
X = friedman[0]
y = np.transpose([friedman[1], (friedman[1] ** 2 + 1)])
def func(y):
out = np.sqrt(y[:, 0] ** 2 + y[:, 1] ** 2)
return out[:, np.newaxis]
def inverse_func(y):
return y
tt = TransformedTargetRegressor(func=func, inverse_func=inverse_func,
check_inverse=False)
tt.fit(X, y)
y_pred_2d_func = tt.predict(X)
assert y_pred_2d_func.shape == (100, 1)
# force that the function only return a 1D array
def func(y):
return np.sqrt(y[:, 0] ** 2 + y[:, 1] ** 2)
tt = TransformedTargetRegressor(func=func, inverse_func=inverse_func,
check_inverse=False)
tt.fit(X, y)
y_pred_1d_func = tt.predict(X)
assert y_pred_1d_func.shape == (100, 1)
assert_allclose(y_pred_1d_func, y_pred_2d_func)
开发者ID:TaihuaLi,项目名称:scikit-learn,代码行数:28,代码来源:test_target.py
示例17: test_sensitivity_specificity_extra_labels
def test_sensitivity_specificity_extra_labels(average, expected_specificty):
y_true = [1, 3, 3, 2]
y_pred = [1, 1, 3, 2]
actual = specificity_score(
y_true, y_pred, labels=[0, 1, 2, 3, 4], average=average)
assert_allclose(expected_specificty, actual, rtol=R_TOL)
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:7,代码来源:test_classification.py
示例18: test_geometric_mean_support_binary
def test_geometric_mean_support_binary():
y_true, y_pred, _ = make_prediction(binary=True)
# compute the geometric mean for the binary problem
geo_mean = geometric_mean_score(y_true, y_pred)
assert_allclose(geo_mean, 0.77, rtol=R_TOL)
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:7,代码来源:test_classification.py
示例19: test_iterative_imputer_all_missing
def test_iterative_imputer_all_missing():
n = 100
d = 3
X = np.zeros((n, d))
imputer = IterativeImputer(missing_values=0, max_iter=1)
X_imputed = imputer.fit_transform(X)
assert_allclose(X_imputed, imputer.initial_imputer_.transform(X))
开发者ID:psorianom,项目名称:scikit-learn,代码行数:7,代码来源:test_impute.py
示例20: test_pairwise_distances_data_derived_params
def test_pairwise_distances_data_derived_params(n_jobs, metric, dist_function,
y_is_x):
# check that pairwise_distances give the same result in sequential and
# parallel, when metric has data-derived parameters.
with config_context(working_memory=1): # to have more than 1 chunk
rng = np.random.RandomState(0)
X = rng.random_sample((1000, 10))
if y_is_x:
Y = X
expected_dist_default_params = squareform(pdist(X, metric=metric))
if metric == "seuclidean":
params = {'V': np.var(X, axis=0, ddof=1)}
else:
params = {'VI': np.linalg.inv(np.cov(X.T)).T}
else:
Y = rng.random_sample((1000, 10))
expected_dist_default_params = cdist(X, Y, metric=metric)
if metric == "seuclidean":
params = {'V': np.var(np.vstack([X, Y]), axis=0, ddof=1)}
else:
params = {'VI': np.linalg.inv(np.cov(np.vstack([X, Y]).T)).T}
expected_dist_explicit_params = cdist(X, Y, metric=metric, **params)
dist = np.vstack(tuple(dist_function(X, Y,
metric=metric, n_jobs=n_jobs)))
assert_allclose(dist, expected_dist_explicit_params)
assert_allclose(dist, expected_dist_default_params)
开发者ID:scikit-learn,项目名称:scikit-learn,代码行数:29,代码来源:test_pairwise.py
注:本文中的sklearn.utils.testing.assert_allclose函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论