Python sklearn.utils.testing.assert_raises() Examples
The following are 30
code examples of sklearn.utils.testing.assert_raises().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sklearn.utils.testing
, or try the search function
.
Example #1
Source File: test_base.py From combo with BSD 2-Clause "Simplified" License | 6 votes |
def test_init(self): """ Test base class initialization :return: """ self.dummy_clf = Dummy1() self.dummy_clf = Dummy1(base_estimators=[DecisionTreeClassifier(), DecisionTreeClassifier()]) # assert_equal(self.dummy_clf.base_estimators, # [DecisionTreeClassifier(), LogisticRegression()]) # # self.dummy_clf = Dummy1( # base_estimators=[LogisticRegression(), DecisionTreeClassifier()]) # assert_equal(self.dummy_clf.base_estimators, # [LogisticRegression(), DecisionTreeClassifier()]) # with assert_raises(ValueError): # Dummy1(base_estimators=[LogisticRegression()]) # # with assert_raises(ValueError): # Dummy1(base_estimators=0) # # with assert_raises(ValueError): # Dummy1(base_estimators=-0.5)
Example #2
Source File: test_least_angle.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_lasso_lars_ic(): # Test the LassoLarsIC object by checking that # - some good features are selected. # - alpha_bic > alpha_aic # - n_nonzero_bic < n_nonzero_aic lars_bic = linear_model.LassoLarsIC('bic') lars_aic = linear_model.LassoLarsIC('aic') rng = np.random.RandomState(42) X = diabetes.data X = np.c_[X, rng.randn(X.shape[0], 5)] # add 5 bad features lars_bic.fit(X, y) lars_aic.fit(X, y) nonzero_bic = np.where(lars_bic.coef_)[0] nonzero_aic = np.where(lars_aic.coef_)[0] assert_greater(lars_bic.alpha_, lars_aic.alpha_) assert_less(len(nonzero_bic), len(nonzero_aic)) assert_less(np.max(nonzero_bic), diabetes.data.shape[1]) # test error on unknown IC lars_broken = linear_model.LassoLarsIC('<unknown>') assert_raises(ValueError, lars_broken.fit, X, y)
Example #3
Source File: test_split.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_stratified_shuffle_split_init(): X = np.arange(7) y = np.asarray([0, 1, 1, 1, 2, 2, 2]) # Check that error is raised if there is a class with only one sample assert_raises(ValueError, next, StratifiedShuffleSplit(3, 0.2).split(X, y)) # Check that error is raised if the test set size is smaller than n_classes assert_raises(ValueError, next, StratifiedShuffleSplit(3, 2).split(X, y)) # Check that error is raised if the train set size is smaller than # n_classes assert_raises(ValueError, next, StratifiedShuffleSplit(3, 3, 2).split(X, y)) X = np.arange(9) y = np.asarray([0, 0, 0, 1, 1, 1, 2, 2, 2]) # Train size or test size too small assert_raises(ValueError, next, StratifiedShuffleSplit(train_size=2).split(X, y)) assert_raises(ValueError, next, StratifiedShuffleSplit(test_size=2).split(X, y))
Example #4
Source File: test_sod.py From pyod with BSD 2-Clause "Simplified" License | 6 votes |
def test_check_parameters(self): with assert_raises(ValueError): SOD(n_neighbors=None, ref_set=10, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=None, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=10, alpha=None) with assert_raises(ValueError): SOD(n_neighbors=-1, ref_set=10, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=-1, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=10, alpha=-1) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=25, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors='not int', ref_set=25, alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set='not int', alpha=0.8) with assert_raises(ValueError): SOD(n_neighbors=20, ref_set=25, alpha='not float')
Example #5
Source File: test_hbos.py From pyod with BSD 2-Clause "Simplified" License | 6 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something') # def test_score(self): # self.clf.score(self.X_test, self.y_test) # self.clf.score(self.X_test, self.y_test, scoring='roc_auc_score') # self.clf.score(self.X_test, self.y_test, scoring='prc_n_score') # with assert_raises(NotImplementedError): # self.clf.score(self.X_test, self.y_test, scoring='something')
Example #6
Source File: test_base.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_get_params(self): test = T(K(), K()) assert ('a__d' in test.get_params(deep=True)) assert ('a__d' not in test.get_params(deep=False)) test.set_params(a__d=2) assert (test.a.d == 2) assert_raises(ValueError, test.set_params, a__a=2)
Example #7
Source File: test_utility.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_normalization(self): # test when X_t is presented and no scalar norm_X_train, norm_X_test = standardizer(self.X_train, self.X_test) assert_allclose(norm_X_train.mean(), 0, atol=0.05) assert_allclose(norm_X_train.std(), 1, atol=0.05) assert_allclose(norm_X_test.mean(), 0, atol=0.05) assert_allclose(norm_X_test.std(), 1, atol=0.05) # test when X_t is not presented and no scalar norm_X_train = standardizer(self.X_train) assert_allclose(norm_X_train.mean(), 0, atol=0.05) assert_allclose(norm_X_train.std(), 1, atol=0.05) # test when X_t is presented and the scalar is kept norm_X_train, norm_X_test, scalar = standardizer(self.X_train, self.X_test, keep_scalar=True) assert_allclose(norm_X_train.mean(), 0, atol=0.05) assert_allclose(norm_X_train.std(), 1, atol=0.05) assert_allclose(norm_X_test.mean(), 0, atol=0.05) assert_allclose(norm_X_test.std(), 1, atol=0.05) if not hasattr(scalar, 'fit') or not hasattr(scalar, 'transform'): raise AttributeError("%s is not a detector instance." % (scalar)) # test when X_t is not presented and the scalar is kept norm_X_train, scalar = standardizer(self.X_train, keep_scalar=True) assert_allclose(norm_X_train.mean(), 0, atol=0.05) assert_allclose(norm_X_train.std(), 1, atol=0.05) if not hasattr(scalar, 'fit') or not hasattr(scalar, 'transform'): raise AttributeError("%s is not a detector instance." % (scalar)) # test shape difference with assert_raises(ValueError): standardizer(self.X_train, self.X_test_diff)
Example #8
Source File: test_score_comb.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_aom_static_n_buckets(self): with assert_raises(ValueError): aom(self.scores, 5, method='static', bootstrap_estimators=False, random_state=42) # TODO: add more complicated testcases
Example #9
Source File: test_common.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_thresholded_invariance_string_vs_numbers_labels(name): # Ensure that thresholded metrics with string labels are invariant random_state = check_random_state(0) y1 = random_state.randint(0, 2, size=(20, )) y2 = random_state.randint(0, 2, size=(20, )) y1_str = np.array(["eggs", "spam"])[y1] pos_label_str = "spam" with ignore_warnings(): metric = THRESHOLDED_METRICS[name] if name not in METRIC_UNDEFINED_BINARY: # Ugly, but handle case with a pos_label and label metric_str = metric if name in METRICS_WITH_POS_LABEL: metric_str = partial(metric_str, pos_label=pos_label_str) measure_with_number = metric(y1, y2) measure_with_str = metric_str(y1_str, y2) assert_array_equal(measure_with_number, measure_with_str, err_msg="{0} failed string vs number " "invariance test".format(name)) measure_with_strobj = metric_str(y1_str.astype('O'), y2) assert_array_equal(measure_with_number, measure_with_strobj, err_msg="{0} failed string object vs number " "invariance test".format(name)) else: # TODO those metrics doesn't support string label yet assert_raises(ValueError, metric, y1_str, y2) assert_raises(ValueError, metric, y1_str.astype('O'), y2)
Example #10
Source File: test_detector_comb.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, proba_method='something')
Example #11
Source File: test_classifier_des.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict(self): with assert_raises(NotImplementedError): y_train_predicted = self.clf.fit_predict(self.X_train, self.y_train)
Example #12
Source File: test_utility.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_inconsistent_length(self): with assert_raises(ValueError): get_label_n(self.y, self.labels_short_)
Example #13
Source File: test_split.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_repeated_cv_value_errors(): # n_repeats is not integer or <= 0 for cv in (RepeatedKFold, RepeatedStratifiedKFold): assert_raises(ValueError, cv, n_repeats=0) assert_raises(ValueError, cv, n_repeats=1.5)
Example #14
Source File: test_classifier_dcs.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict(self): with assert_raises(NotImplementedError): y_train_predicted = self.clf.fit_predict(self.X_train, self.y_train)
Example #15
Source File: test_common.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def _check_averaging(metric, y_true, y_pred, y_true_binarize, y_pred_binarize, is_multilabel): n_samples, n_classes = y_true_binarize.shape # No averaging label_measure = metric(y_true, y_pred, average=None) assert_allclose(label_measure, [metric(y_true_binarize[:, i], y_pred_binarize[:, i]) for i in range(n_classes)]) # Micro measure micro_measure = metric(y_true, y_pred, average="micro") assert_allclose(micro_measure, metric(y_true_binarize.ravel(), y_pred_binarize.ravel())) # Macro measure macro_measure = metric(y_true, y_pred, average="macro") assert_allclose(macro_measure, np.mean(label_measure)) # Weighted measure weights = np.sum(y_true_binarize, axis=0, dtype=int) if np.sum(weights) != 0: weighted_measure = metric(y_true, y_pred, average="weighted") assert_allclose(weighted_measure, np.average(label_measure, weights=weights)) else: weighted_measure = metric(y_true, y_pred, average="weighted") assert_allclose(weighted_measure, 0) # Sample measure if is_multilabel: sample_measure = metric(y_true, y_pred, average="samples") assert_allclose(sample_measure, np.mean([metric(y_true_binarize[i], y_pred_binarize[i]) for i in range(n_samples)])) assert_raises(ValueError, metric, y_true, y_pred, average="unknown") assert_raises(ValueError, metric, y_true, y_pred, average="garbage")
Example #16
Source File: test_common.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_raise_value_error_multilabel_sequences(name): # make sure the multilabel-sequence format raises ValueError multilabel_sequences = [ [[0, 1]], [[1], [2], [0, 1]], [(), (2), (0, 1)], [[]], [()], np.array([[], [1, 2]], dtype='object')] metric = ALL_METRICS[name] for seq in multilabel_sequences: assert_raises(ValueError, metric, seq, seq)
Example #17
Source File: test_detector_comb.py From combo with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, proba_method='something')
Example #18
Source File: test_common.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_multioutput_number_of_output_differ(name): y_true = np.array([[1, 0, 0, 1], [0, 1, 1, 1], [1, 1, 0, 1]]) y_pred = np.array([[0, 0], [1, 0], [0, 0]]) metric = ALL_METRICS[name] assert_raises(ValueError, metric, y_true, y_pred)
Example #19
Source File: test_base.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_clone_buggy(): # Check that clone raises an error on buggy estimators. buggy = Buggy() buggy.a = 2 assert_raises(RuntimeError, clone, buggy) no_estimator = NoEstimator() assert_raises(TypeError, clone, no_estimator) varg_est = VargEstimator() assert_raises(RuntimeError, clone, varg_est) est = ModifyInitParams() assert_raises(RuntimeError, clone, est)
Example #20
Source File: test_so_gaal.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')
Example #21
Source File: test_so_gaal.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, method='something')
Example #22
Source File: test_loci.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, method='something')
Example #23
Source File: test_mo_gaal.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')
Example #24
Source File: test_mo_gaal.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, method='something')
Example #25
Source File: test_cblof.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')
Example #26
Source File: test_cblof.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, method='something')
Example #27
Source File: test_lmdd.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')
Example #28
Source File: test_lmdd.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_prediction_proba_parameter(self): with assert_raises(ValueError): self.clf.predict_proba(self.X_test, method='something')
Example #29
Source File: test_xgbod.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')
Example #30
Source File: test_feature_bagging.py From pyod with BSD 2-Clause "Simplified" License | 5 votes |
def test_fit_predict_score(self): self.clf.fit_predict_score(self.X_test, self.y_test) self.clf.fit_predict_score(self.X_test, self.y_test, scoring='roc_auc_score') self.clf.fit_predict_score(self.X_test, self.y_test, scoring='prc_n_score') with assert_raises(NotImplementedError): self.clf.fit_predict_score(self.X_test, self.y_test, scoring='something')