• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python testing.assert_raises_regexp函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.utils.testing.assert_raises_regexp函数的典型用法代码示例。如果您正苦于以下问题:Python assert_raises_regexp函数的具体用法?Python assert_raises_regexp怎么用?Python assert_raises_regexp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了assert_raises_regexp函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: check_scoring_validator_for_single_metric_usecases

def check_scoring_validator_for_single_metric_usecases(scoring_validator):
    # Test all branches of single metric usecases
    estimator = EstimatorWithoutFit()
    pattern = (r"estimator should be an estimator implementing 'fit' method,"
               r" .* was passed")
    assert_raises_regexp(TypeError, pattern, scoring_validator, estimator)

    estimator = EstimatorWithFitAndScore()
    estimator.fit([[1]], [1])
    scorer = scoring_validator(estimator)
    assert_true(scorer is _passthrough_scorer)
    assert_almost_equal(scorer(estimator, [[1]], [1]), 1.0)

    estimator = EstimatorWithFitAndPredict()
    estimator.fit([[1]], [1])
    pattern = (r"If no scoring is specified, the estimator passed should have"
               r" a 'score' method\. The estimator .* does not\.")
    assert_raises_regexp(TypeError, pattern, scoring_validator, estimator)

    scorer = scoring_validator(estimator, "accuracy")
    assert_almost_equal(scorer(estimator, [[1]], [1]), 1.0)

    estimator = EstimatorWithFit()
    scorer = scoring_validator(estimator, "accuracy")
    assert_true(isinstance(scorer, _PredictScorer))

    # Test the allow_none parameter for check_scoring alone
    if scoring_validator is check_scoring:
        estimator = EstimatorWithFit()
        scorer = scoring_validator(estimator, allow_none=True)
        assert_true(scorer is None)
开发者ID:chavan-vjti,项目名称:scikit-learn,代码行数:31,代码来源:test_score_objects.py


示例2: test_base_estimator

def test_base_estimator():
    # Test different base estimators.
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.svm import SVC

    # XXX doesn't work with y_class because RF doesn't support classes_
    # Shouldn't AdaBoost run a LabelBinarizer?
    clf = AdaBoostClassifier(RandomForestClassifier())
    clf.fit(X, y_regr)

    clf = AdaBoostClassifier(SVC(), algorithm="SAMME")
    clf.fit(X, y_class)

    from sklearn.ensemble import RandomForestRegressor
    from sklearn.svm import SVR

    clf = AdaBoostRegressor(RandomForestRegressor(), random_state=0)
    clf.fit(X, y_regr)

    clf = AdaBoostRegressor(SVR(), random_state=0)
    clf.fit(X, y_regr)

    # Check that an empty discrete ensemble fails in fit, not predict.
    X_fail = [[1, 1], [1, 1], [1, 1], [1, 1]]
    y_fail = ["foo", "bar", 1, 2]
    clf = AdaBoostClassifier(SVC(), algorithm="SAMME")
    assert_raises_regexp(ValueError, "worse than random",
                         clf.fit, X_fail, y_fail)
开发者ID:0664j35t3r,项目名称:scikit-learn,代码行数:28,代码来源:test_weight_boosting.py


示例3: test_ransac_resid_thresh_no_inliers

def test_ransac_resid_thresh_no_inliers():
    # When residual_threshold=0.0 there are no inliers and a
    # ValueError with a message should be raised
    base_estimator = LinearRegression()
    ransac_estimator = RANSACRegressor(base_estimator, min_samples=2, residual_threshold=0.0, random_state=0)

    assert_raises_regexp(ValueError, "No inliers.*residual_threshold.*0\.0", ransac_estimator.fit, X, y)
开发者ID:mittelman,项目名称:scikit-learn,代码行数:7,代码来源:test_ransac.py


示例4: test_check_scoring

def test_check_scoring():
    # Test all branches of check_scoring
    estimator = EstimatorWithoutFit()
    pattern = (r"estimator should a be an estimator implementing 'fit' method,"
               r" .* was passed")
    assert_raises_regexp(TypeError, pattern, check_scoring, estimator)

    estimator = EstimatorWithFitAndScore()
    estimator.fit([[1]], [1])
    scorer = check_scoring(estimator)
    assert_true(scorer is _passthrough_scorer)
    assert_almost_equal(scorer(estimator, [[1]], [1]), 1.0)

    estimator = EstimatorWithFitAndPredict()
    estimator.fit([[1]], [1])
    pattern = (r"If no scoring is specified, the estimator passed should have"
               r" a 'score' method\. The estimator .* does not\.")
    assert_raises_regexp(TypeError, pattern, check_scoring, estimator)

    scorer = check_scoring(estimator, "accuracy")
    assert_almost_equal(scorer(estimator, [[1]], [1]), 1.0)

    estimator = EstimatorWithFit()
    scorer = check_scoring(estimator, "accuracy")
    assert_true(isinstance(scorer, _PredictScorer))

    estimator = EstimatorWithFit()
    scorer = check_scoring(estimator, allow_none=True)
    assert_true(scorer is None)
开发者ID:AlexanderFabisch,项目名称:scikit-learn,代码行数:29,代码来源:test_score_objects.py


示例5: test_underflow_or_overlow

def test_underflow_or_overlow():
    with np.errstate(all="raise"):
        # Generate some weird data with hugely unscaled features
        rng = np.random.RandomState(0)
        n_samples = 100
        n_features = 10

        X = rng.normal(size=(n_samples, n_features))
        X[:, :2] *= 1e300
        assert_true(np.isfinite(X).all())

        # Use MinMaxScaler to scale the data without introducing a numerical
        # instability (computing the standard deviation naively is not possible
        # on this data)
        X_scaled = MinMaxScaler().fit_transform(X)
        assert_true(np.isfinite(X_scaled).all())

        # Define a ground truth on the scaled data
        ground_truth = rng.normal(size=n_features)
        y = (np.dot(X_scaled, ground_truth) > 0.0).astype(np.int32)
        assert_array_equal(np.unique(y), [0, 1])

        model = SGDClassifier(alpha=0.1, loss="squared_hinge", n_iter=500)

        # smoke test: model is stable on scaled data
        model.fit(X_scaled, y)
        assert_true(np.isfinite(model.coef_).all())

        # model is numerically unstable on unscaled data
        msg_regxp = (
            r"Floating-point under-/overflow occurred at epoch #.*"
            " Scaling input data with StandardScaler or MinMaxScaler"
            " might help."
        )
        assert_raises_regexp(ValueError, msg_regxp, model.fit, X, y)
开发者ID:richlewis42,项目名称:scikit-learn,代码行数:35,代码来源:test_sgd.py


示例6: test_linearsvc_parameters

def test_linearsvc_parameters():
    # Test possible parameter combinations in LinearSVC
    # Generate list of possible parameter combinations
    losses = ['hinge', 'squared_hinge', 'logistic_regression', 'foo']
    penalties, duals = ['l1', 'l2', 'bar'], [True, False]

    X, y = make_classification(n_samples=5, n_features=5)

    for loss, penalty, dual in itertools.product(losses, penalties, duals):
        clf = svm.LinearSVC(penalty=penalty, loss=loss, dual=dual)
        if ((loss, penalty) == ('hinge', 'l1') or
                (loss, penalty, dual) == ('hinge', 'l2', False) or
                (penalty, dual) == ('l1', True) or
                loss == 'foo' or penalty == 'bar'):

            assert_raises_regexp(ValueError,
                                 "Unsupported set of arguments.*penalty='%s.*"
                                 "loss='%s.*dual=%s"
                                 % (penalty, loss, dual),
                                 clf.fit, X, y)
        else:
            clf.fit(X, y)

    # Incorrect loss value - test if explicit error message is raised
    assert_raises_regexp(ValueError, ".*loss='l3' is not supported.*",
                         svm.LinearSVC(loss="l3").fit, X, y)
开发者ID:abhisg,项目名称:scikit-learn,代码行数:26,代码来源:test_svm.py


示例7: test_non_positive_precomputed_distances

def test_non_positive_precomputed_distances():
    # Precomputed distance matrices must be positive.
    bad_dist = np.array([[0., -1.], [1., 0.]])
    for method in ['barnes_hut', 'exact']:
        tsne = TSNE(metric="precomputed", method=method)
        assert_raises_regexp(ValueError, "All distances .*precomputed.*",
                             tsne.fit_transform, bad_dist)
开发者ID:BasilBeirouti,项目名称:scikit-learn,代码行数:7,代码来源:test_t_sne.py


示例8: test_no_sparse_on_barnes_hut

def test_no_sparse_on_barnes_hut():
    # No sparse matrices allowed on Barnes-Hut.
    random_state = check_random_state(0)
    X = random_state.randn(100, 2)
    X[(np.random.randint(0, 100, 50), np.random.randint(0, 2, 50))] = 0.0
    X_csr = sp.csr_matrix(X)
    tsne = TSNE(n_iter=199, method="barnes_hut")
    assert_raises_regexp(TypeError, "A sparse matrix was.*", tsne.fit_transform, X_csr)
开发者ID:sofianehaddad,项目名称:scikit-learn,代码行数:8,代码来源:test_t_sne.py


示例9: test_lda_no_component_error

def test_lda_no_component_error():
    # test `transform` and `perplexity` before `fit`
    rng = np.random.RandomState(0)
    X = rng.randint(4, size=(20, 10))
    lda = LatentDirichletAllocation()
    regex = r"^no 'components_' attribute"
    assert_raises_regexp(NotFittedError, regex, lda.transform, X)
    assert_raises_regexp(NotFittedError, regex, lda.perplexity, X)
开发者ID:AlexandreAbraham,项目名称:scikit-learn,代码行数:8,代码来源:test_online_lda.py


示例10: test_k_means_n_init

def test_k_means_n_init():
    rnd = np.random.RandomState(0)
    X = rnd.normal(size=(40, 2))

    # two regression tests on bad n_init argument
    # previous bug: n_init <= 0 threw non-informative TypeError (#3858)
    assert_raises_regexp(ValueError, "n_init", KMeans(n_init=0).fit, X)
    assert_raises_regexp(ValueError, "n_init", KMeans(n_init=-1).fit, X)
开发者ID:CamDavidsonPilon,项目名称:scikit-learn,代码行数:8,代码来源:test_k_means.py


示例11: test_lda_no_component_error

def test_lda_no_component_error():
    # test `perplexity` before `fit`
    rng = np.random.RandomState(0)
    X = rng.randint(4, size=(20, 10))
    lda = LatentDirichletAllocation()
    regex = ("This LatentDirichletAllocation instance is not fitted yet. "
             "Call 'fit' with appropriate arguments before using this method.")
    assert_raises_regexp(NotFittedError, regex, lda.perplexity, X)
开发者ID:daniel-perry,项目名称:scikit-learn,代码行数:8,代码来源:test_online_lda.py


示例12: test_pca_initialization_not_compatible_with_precomputed_kernel

def test_pca_initialization_not_compatible_with_precomputed_kernel():
    # Precomputed distance matrices must be square matrices.
    tsne = TSNE(metric="precomputed", init="pca")
    assert_raises_regexp(
        ValueError,
        'The parameter init="pca" cannot be ' 'used with metric="precomputed".',
        tsne.fit_transform,
        np.array([[0.0], [1.0]]),
    )
开发者ID:sofianehaddad,项目名称:scikit-learn,代码行数:9,代码来源:test_t_sne.py


示例13: test_distance_not_available

def test_distance_not_available():
    # 'metric' must be valid.
    tsne = TSNE(metric="not available", method='exact')
    assert_raises_regexp(ValueError, "Unknown metric not available.*",
                         tsne.fit_transform, np.array([[0.0], [1.0]]))

    tsne = TSNE(metric="not available", method='barnes_hut')
    assert_raises_regexp(ValueError, "Metric 'not available' not valid.*",
                         tsne.fit_transform, np.array([[0.0], [1.0]]))
开发者ID:BasilBeirouti,项目名称:scikit-learn,代码行数:9,代码来源:test_t_sne.py


示例14: test_non_positive_computed_distances

def test_non_positive_computed_distances():
    # Computed distance matrices must be positive.
    def metric(x, y):
        return -1

    tsne = TSNE(metric=metric, method='exact')
    X = np.array([[0.0, 0.0], [1.0, 1.0]])
    assert_raises_regexp(ValueError, "All distances .*metric given.*",
                         tsne.fit_transform, X)
开发者ID:BasilBeirouti,项目名称:scikit-learn,代码行数:9,代码来源:test_t_sne.py


示例15: test_lda_transform_mismatch

def test_lda_transform_mismatch():
    # test `n_features` mismatch in partial_fit and transform
    rng = np.random.RandomState(0)
    X = rng.randint(4, size=(20, 10))
    X_2 = rng.randint(4, size=(10, 8))

    n_topics = rng.randint(3, 6)
    lda = LatentDirichletAllocation(n_topics=n_topics, random_state=rng)
    lda.partial_fit(X)
    assert_raises_regexp(ValueError, r"^The provided data has", lda.partial_fit, X_2)
开发者ID:andaag,项目名称:scikit-learn,代码行数:10,代码来源:test_online_lda.py


示例16: test_lda_partial_fit_dim_mismatch

def test_lda_partial_fit_dim_mismatch():
    # test `n_features` mismatch in `partial_fit`
    rng = np.random.RandomState(0)
    n_topics = rng.randint(3, 6)
    n_col = rng.randint(6, 10)
    X_1 = np.random.randint(4, size=(10, n_col))
    X_2 = np.random.randint(4, size=(10, n_col + 1))
    lda = LatentDirichletAllocation(n_topics=n_topics, learning_offset=5.,
                                    total_samples=20, random_state=rng)
    lda.partial_fit(X_1)
    assert_raises_regexp(ValueError, r"^The provided data has", lda.partial_fit, X_2)
开发者ID:andaag,项目名称:scikit-learn,代码行数:11,代码来源:test_online_lda.py


示例17: test_ovr_partial_fit_exceptions

def test_ovr_partial_fit_exceptions():
    ovr = OneVsRestClassifier(MultinomialNB())
    X = np.abs(np.random.randn(14, 2))
    y = [1, 1, 1, 1, 2, 3, 3, 0, 0, 2, 3, 1, 2, 3]
    ovr.partial_fit(X[:7], y[:7], np.unique(y))
    # A new class value which was not in the first call of partial_fit
    # It should raise ValueError
    y1 = [5] + y[7:-1]
    assert_raises_regexp(ValueError, r"Mini-batch contains \[.+\] while "
                                     r"classes must be subset of \[.+\]",
                         ovr.partial_fit, X=X[7:], y=y1)
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:11,代码来源:test_multiclass.py


示例18: test_invalid_params

def test_invalid_params():
    # test `_check_params` method
    X = np.ones((5, 10))

    invalid_models = (
        ('n_topics', LatentDirichletAllocation(n_topics=0)),
        ('learning_method', LatentDirichletAllocation(learning_method='unknown')),
        ('total_samples', LatentDirichletAllocation(total_samples=0)),
        ('learning_offset', LatentDirichletAllocation(learning_offset=-1)),
    )
    for param, model in invalid_models:
        regex = r"^Invalid %r parameter" % param
        assert_raises_regexp(ValueError, regex, model.fit, X)
开发者ID:andaag,项目名称:scikit-learn,代码行数:13,代码来源:test_online_lda.py


示例19: test_ransac_resid_thresh_no_inliers

def test_ransac_resid_thresh_no_inliers():
    # When residual_threshold=0.0 there are no inliers and a
    # ValueError with a message should be raised
    base_estimator = LinearRegression()
    ransac_estimator = RANSACRegressor(base_estimator, min_samples=2,
                                       residual_threshold=0.0, random_state=0,
                                       max_trials=5)

    msg = ("RANSAC could not find a valid consensus set")
    assert_raises_regexp(ValueError, msg, ransac_estimator.fit, X, y)
    assert_equal(ransac_estimator.n_skips_no_inliers_, 5)
    assert_equal(ransac_estimator.n_skips_invalid_data_, 0)
    assert_equal(ransac_estimator.n_skips_invalid_model_, 0)
开发者ID:AlexisMignon,项目名称:scikit-learn,代码行数:13,代码来源:test_ransac.py


示例20: test_partial_fit_weight_class_auto

 def test_partial_fit_weight_class_auto(self):
     # partial_fit with class_weight='auto' not supported
     assert_raises_regexp(ValueError,
                          "class_weight 'auto' is not supported for "
                          "partial_fit. In order to use 'auto' weights, "
                          "use compute_class_weight\('auto', classes, y\). "
                          "In place of y you can us a large enough sample "
                          "of the full training set target to properly "
                          "estimate the class frequency distributions. "
                          "Pass the resulting weights as the class_weight "
                          "parameter.",
                          self.factory(class_weight='auto').partial_fit,
                          X, Y, classes=np.unique(Y))
开发者ID:1TTT9,项目名称:scikit-learn,代码行数:13,代码来源:test_sgd.py



注:本文中的sklearn.utils.testing.assert_raises_regexp函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python testing.assert_true函数代码示例发布时间:2022-05-27
下一篇:
Python testing.assert_raises_regex函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap