Python sklearn.dummy.DummyClassifier() Examples
The following are 30
code examples of sklearn.dummy.DummyClassifier().
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.dummy
, or try the search function
.
Example #1
Source File: _lagrangian.py From fairlearn with MIT License | 7 votes |
def _call_oracle(self, lambda_vec): signed_weights = self.obj.signed_weights() + self.constraints.signed_weights(lambda_vec) redY = 1 * (signed_weights > 0) redW = signed_weights.abs() redW = self.n * redW / redW.sum() redY_unique = np.unique(redY) classifier = None if len(redY_unique) == 1: logger.debug("redY had single value. Using DummyClassifier") classifier = DummyClassifier(strategy='constant', constant=redY_unique[0]) self.n_oracle_calls_dummy_returned += 1 else: classifier = pickle.loads(self.pickled_estimator) oracle_call_start_time = time() classifier.fit(self.X, redY, sample_weight=redW) self.oracle_execution_times.append(time() - oracle_call_start_time) self.n_oracle_calls += 1 return classifier
Example #2
Source File: __init__.py From sklearn2pmml with GNU Affero General Public License v3.0 | 6 votes |
def test_fit_predict(self): df = DataFrame([[-1, 0], [0, 0], [-1, -1], [1, 1], [-1, -1]], columns = ["X", "y"]) X = df[["X"]] y = df["y"] classifier = clone(SelectFirstClassifier([ ("negative", DummyClassifier(strategy = "most_frequent"), "X[0] < 0"), ("positive", DummyClassifier(strategy = "most_frequent"), "X[0] > 0"), ("zero", DummyClassifier(strategy = "constant", constant = 0), str(True)) ])) params = classifier.get_params(deep = True) self.assertEqual("most_frequent", params["negative__strategy"]) self.assertEqual("most_frequent", params["positive__strategy"]) self.assertEqual("constant", params["zero__strategy"]) self.assertEqual(0, params["zero__constant"]) classifier.fit(X, y) preds = classifier.predict(X) self.assertEqual([-1, 0, -1, 1, -1], preds.tolist()) pred_probs = classifier.predict_proba(X) self.assertEqual((5, 2), pred_probs.shape)
Example #3
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_uniform_strategy_sparse_target_warning(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[2, 1], [2, 2], [1, 4], [4, 2], [1, 1]])) clf = DummyClassifier(strategy="uniform", random_state=0) assert_warns_message(UserWarning, "the uniform strategy would not save memory", clf.fit, X, y) X = [[0]] * 500 y_pred = clf.predict(X) for k in range(y.shape[1]): p = np.bincount(y_pred[:, k]) / float(len(X)) assert_almost_equal(p[1], 1 / 3, decimal=1) assert_almost_equal(p[2], 1 / 3, decimal=1) assert_almost_equal(p[4], 1 / 3, decimal=1)
Example #4
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_constant_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[0, 1], [4, 0], [1, 1], [1, 4], [1, 1]])) n_samples = len(X) clf = DummyClassifier(strategy="constant", random_state=0, constant=[1, 0]) clf.fit(X, y) y_pred = clf.predict(X) assert_true(sp.issparse(y_pred)) assert_array_equal(y_pred.toarray(), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))]))
Example #5
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_constant_strategy_multioutput(): X = [[0], [0], [0], [0]] # ignored y = np.array([[2, 3], [1, 3], [2, 3], [2, 0]]) n_samples = len(X) clf = DummyClassifier(strategy="constant", random_state=0, constant=[1, 0]) clf.fit(X, y) assert_array_equal(clf.predict(X), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))])) _check_predict_proba(clf, X, y)
Example #6
Source File: rq1_cnn_1d.py From DeepLearningSmells with Apache License 2.0 | 6 votes |
def measure_performance_dummy_classifier(): outfile = get_out_file("dummy_classifier") write_result(outfile, "smell,auc,precision,recall,f1,average_precision\n") for smell in smell_list: data_path = os.path.join(os.path.join(TOKENIZER_OUT_PATH, smell), DIM) input_data = get_all_data(data_path, smell) # clf = DummyClassifier(strategy='stratified', random_state=0) clf = DummyClassifier(strategy='most_frequent', random_state=0) inverted_train_labels = inputs.invert_labels(input_data.train_labels) clf.fit(input_data.train_data, inverted_train_labels) y_pred = clf.predict(input_data.eval_data) auc, precision, recall, f1, average_precision, fpr, tpr = \ metrics_util.get_all_metrics_(input_data.eval_labels, y_pred) write_result(outfile, smell + "," + str(auc) + "," + str(precision) + "," + str(recall) + "," + str(f1) + "," + str( average_precision) + "\n")
Example #7
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_uniform_strategy_multioutput(): X = [[0]] * 4 # ignored y = np.array([[2, 1], [2, 2], [1, 2], [1, 1]]) clf = DummyClassifier(strategy="uniform", random_state=0) clf.fit(X, y) X = [[0]] * 500 y_pred = clf.predict(X) for k in range(y.shape[1]): p = np.bincount(y_pred[:, k]) / float(len(X)) assert_almost_equal(p[1], 0.5, decimal=1) assert_almost_equal(p[2], 0.5, decimal=1) _check_predict_proba(clf, X, y) _check_behavior_2d(clf)
Example #8
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_stratified_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[4, 1], [0, 0], [1, 1], [1, 4], [1, 1]])) clf = DummyClassifier(strategy="stratified", random_state=0) clf.fit(X, y) X = [[0]] * 500 y_pred = clf.predict(X) assert_true(sp.issparse(y_pred)) y_pred = y_pred.toarray() for k in range(y.shape[1]): p = np.bincount(y_pred[:, k]) / float(len(X)) assert_almost_equal(p[1], 3. / 5, decimal=1) assert_almost_equal(p[0], 1. / 5, decimal=1) assert_almost_equal(p[4], 1. / 5, decimal=1)
Example #9
Source File: test_weight_boosting.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_multidimensional_X(): """ Check that the AdaBoost estimators can work with n-dimensional data matrix """ from sklearn.dummy import DummyClassifier, DummyRegressor rng = np.random.RandomState(0) X = rng.randn(50, 3, 3) yc = rng.choice([0, 1], 50) yr = rng.randn(50) boost = AdaBoostClassifier(DummyClassifier(strategy='most_frequent')) boost.fit(X, yc) boost.predict(X) boost.predict_proba(X) boost = AdaBoostRegressor(DummyRegressor()) boost.fit(X, yr) boost.predict(X)
Example #10
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[1, 0], [1, 3], [4, 0], [0, 1], [1, 0]])) n_samples = len(X) y_expected = np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))]) for strategy in ("most_frequent", "prior"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) y_pred = clf.predict(X) assert_true(sp.issparse(y_pred)) assert_array_equal(y_pred.toarray(), y_expected)
Example #11
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy_multioutput(): X = [[0], [0], [0], [0]] # ignored y = np.array([[1, 0], [2, 0], [1, 0], [1, 3]]) n_samples = len(X) for strategy in ("prior", "most_frequent"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) assert_array_equal(clf.predict(X), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))])) _check_predict_proba(clf, X, y) _check_behavior_2d(clf)
Example #12
Source File: rq1_rnn_emb_lstm.py From DeepLearningSmells with Apache License 2.0 | 6 votes |
def measure_performance_dummy_classifier(): outfile = get_out_file("dummy_classifier") write_result(outfile, "smell,auc,precision,recall,f1,average_precision\n") for smell in smell_list: data_path = os.path.join(os.path.join(TOKENIZER_OUT_PATH, smell), DIM) input_data = get_all_data(data_path, smell) # clf = DummyClassifier(strategy='stratified', random_state=0) clf = DummyClassifier(strategy='most_frequent', random_state=0) inverted_train_labels = inputs.invert_labels(input_data.train_labels) # clf.fit(input_data.train_data, input_data.train_labels) clf.fit(input_data.train_data, inverted_train_labels) y_pred = clf.predict(input_data.eval_data) auc, precision, recall, f1, average_precision, fpr, tpr = \ metrics_util.get_all_metrics_(input_data.eval_labels, y_pred) write_result(outfile, smell + "," + str(auc) + "," + str(precision) + "," + str(recall) + "," + str(f1) + "," + str( average_precision) + "\n")
Example #13
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy(): X = [[0], [0], [0], [0]] # ignored y = [1, 2, 1, 1] for strategy in ("most_frequent", "prior"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) assert_array_equal(clf.predict(X), np.ones(len(X))) _check_predict_proba(clf, X, y) if strategy == "prior": assert_array_almost_equal(clf.predict_proba([X[0]]), clf.class_prior_.reshape((1, -1))) else: assert_array_almost_equal(clf.predict_proba([X[0]]), clf.class_prior_.reshape((1, -1)) > 0.5)
Example #14
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_constant_strategy_multioutput(): X = [[0], [0], [0], [0]] # ignored y = np.array([[2, 3], [1, 3], [2, 3], [2, 0]]) n_samples = len(X) clf = DummyClassifier(strategy="constant", random_state=0, constant=[1, 0]) clf.fit(X, y) assert_array_equal(clf.predict(X), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))])) _check_predict_proba(clf, X, y)
Example #15
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_constant_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[0, 1], [4, 0], [1, 1], [1, 4], [1, 1]])) n_samples = len(X) clf = DummyClassifier(strategy="constant", random_state=0, constant=[1, 0]) clf.fit(X, y) y_pred = clf.predict(X) assert sp.issparse(y_pred) assert_array_equal(y_pred.toarray(), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))]))
Example #16
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_stratified_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[4, 1], [0, 0], [1, 1], [1, 4], [1, 1]])) clf = DummyClassifier(strategy="stratified", random_state=0) clf.fit(X, y) X = [[0]] * 500 y_pred = clf.predict(X) assert sp.issparse(y_pred) y_pred = y_pred.toarray() for k in range(y.shape[1]): p = np.bincount(y_pred[:, k]) / float(len(X)) assert_almost_equal(p[1], 3. / 5, decimal=1) assert_almost_equal(p[0], 1. / 5, decimal=1) assert_almost_equal(p[4], 1. / 5, decimal=1)
Example #17
Source File: test_dummy.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy_sparse_target(): X = [[0]] * 5 # ignored y = sp.csc_matrix(np.array([[1, 0], [1, 3], [4, 0], [0, 1], [1, 0]])) n_samples = len(X) y_expected = np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))]) for strategy in ("most_frequent", "prior"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) y_pred = clf.predict(X) assert sp.issparse(y_pred) assert_array_equal(y_pred.toarray(), y_expected)
Example #18
Source File: test_partial_dependence.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_warning_recursion_non_constant_init(): # make sure that passing a non-constant init parameter to a GBDT and using # recursion method yields a warning. gbc = GradientBoostingClassifier(init=DummyClassifier(), random_state=0) gbc.fit(X, y) with pytest.warns( UserWarning, match='Using recursion method with a non-constant init predictor'): partial_dependence(gbc, X, [0], method='recursion') with pytest.warns( UserWarning, match='Using recursion method with a non-constant init predictor'): partial_dependence(gbc, X, [0], method='recursion')
Example #19
Source File: methods.py From rumour-classification with GNU Lesser General Public License v3.0 | 6 votes |
def get_methods_multitask(tasks_number, header, random_restarts=-1): FEATURES_BOW, FEATURES_BROWN, index_task, _=extract_feature_indices(header) GPCONSTRUCTOR=lambda kernel_constructor, name, random_restarts: MCGP(kernel_constructor=kernel_constructor, labels=LABELS, name=name, random_restarts=random_restarts) methodsmultitask=[ lambda: SklearnBaseline(lambda: DummyClassifier("most_frequent"), "MostFrequentPooled", [0]), lambda: GPCONSTRUCTOR(kernel_constructor=lambda: single_task_kernel(FEATURES_BOW, False, "FEATURES_BOW"), name="BOWGPjoinedfeaturesPooledLIN", random_restarts=random_restarts), lambda: GPCONSTRUCTOR(kernel_constructor=lambda: single_task_kernel(FEATURES_BROWN, False, "FEATURES_BROWN"), name="BROWNGPjoinedfeaturesPooledLIN", random_restarts=random_restarts), lambda: GPCONSTRUCTOR(kernel_constructor=lambda: multi_task_kernel(tasks_number, index_task, single_task_kernel(FEATURES_BROWN, False, "FEATURES_BROWN")), name="BROWNGPjoinedfeaturesICMLIN", random_restarts=random_restarts), lambda: GPCONSTRUCTOR(kernel_constructor=lambda: multi_task_kernel(tasks_number, index_task, single_task_kernel(FEATURES_BOW, False, "FEATURES_BOW")), name="BOWGPjoinedfeaturesICMLIN", random_restarts=random_restarts), ] return methodsmultitask, map(lambda x: x().name, methodsmultitask)
Example #20
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_stratified_strategy_multioutput(): X = [[0]] * 5 # ignored y = np.array([[2, 1], [2, 2], [1, 1], [1, 2], [1, 1]]) clf = DummyClassifier(strategy="stratified", random_state=0) clf.fit(X, y) X = [[0]] * 500 y_pred = clf.predict(X) for k in range(y.shape[1]): p = np.bincount(y_pred[:, k]) / float(len(X)) assert_almost_equal(p[1], 3. / 5, decimal=1) assert_almost_equal(p[2], 2. / 5, decimal=1) _check_predict_proba(clf, X, y) _check_behavior_2d(clf)
Example #21
Source File: dummy_clf.py From 2020plus with Apache License 2.0 | 6 votes |
def __init__(self, df, strategy='most_frequent', weight=False, min_ct=0): self.logger = logging.getLogger(__name__) super(DummyClf, self).__init__() # call base constructor #self.set_min_count(min_ct) self.is_weighted_sample = False # process data #df = self._filter_rows(df) # filter out low count rows df = df.fillna(df.mean()) self.x, self.y = futils.randomize(df) # setup classifier self.clf = DummyClassifier(strategy=strategy)
Example #22
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy_multioutput(): X = [[0], [0], [0], [0]] # ignored y = np.array([[1, 0], [2, 0], [1, 0], [1, 3]]) n_samples = len(X) for strategy in ("prior", "most_frequent"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) assert_array_equal(clf.predict(X), np.hstack([np.ones((n_samples, 1)), np.zeros((n_samples, 1))])) _check_predict_proba(clf, X, y) _check_behavior_2d(clf)
Example #23
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_most_frequent_and_prior_strategy(): X = [[0], [0], [0], [0]] # ignored y = [1, 2, 1, 1] for strategy in ("most_frequent", "prior"): clf = DummyClassifier(strategy=strategy, random_state=0) clf.fit(X, y) assert_array_equal(clf.predict(X), np.ones(len(X))) _check_predict_proba(clf, X, y) if strategy == "prior": assert_array_equal(clf.predict_proba([X[0]]), clf.class_prior_.reshape((1, -1))) else: assert_array_equal(clf.predict_proba([X[0]]), clf.class_prior_.reshape((1, -1)) > 0.5)
Example #24
Source File: test_optimization.py From sports-betting with MIT License | 6 votes |
def test_apply_backtesting(): """Test backtesting function.""" # Input data bettor = Bettor(classifier=DummyClassifier(), targets=['D', 'H']) param_grid = {'classifier__strategy': ['uniform', 'stratified']} risk_factors = [0.0, 0.2, 0.4] random_state = 0 X = np.random.random((100, 2)) scores = np.repeat([1, 0], 50), np.repeat([0, 1], 50), np.repeat([1, 0], 50), np.repeat([0, 1], 50) odds = np.repeat([2.0, 2.0], 100).reshape(-1, 2) cv = TimeSeriesSplit(2, 0.3) n_runs = 3 n_jobs = -1 # Output results = apply_backtesting(bettor, param_grid, risk_factors, X, scores, odds, cv, random_state, n_runs, n_jobs) assert list(results.columns) == ['parameters', 'risk_factor', 'coverage', 'mean_yield', 'std_yield', 'std_mean_yield'] assert len(results) == len(risk_factors) * len(ParameterGrid(param_grid))
Example #25
Source File: test_optimization.py From sports-betting with MIT License | 6 votes |
def test_fit_bet(): """Test fit and bet function.""" # Input data bettor = Bettor(classifier=DummyClassifier(), targets=['D', 'H']) params = {'classifier__strategy': 'constant', 'classifier__constant': 'H'} risk_factors = [0.0] random_state = 0 X = np.random.random((100, 2)) scores = np.repeat([1, 0], 50), np.repeat([0, 1], 50), np.repeat([1, 0], 50), np.repeat([0, 1], 50) train_indices, test_indices = np.arange(0, 25), np.arange(25, 100) odds = np.repeat([2.0, 2.0], 100).reshape(-1, 2) # Output data = fit_bet(bettor, params, risk_factors, random_state, X, scores, odds, train_indices, test_indices) # Expected output expected_yields = np.concatenate([np.repeat(1.0, 25), np.repeat(-1.0, 50)]) expected_data = pd.DataFrame([[str(params), random_state, risk_factors[0], expected_yields]], columns=['parameters', 'experiment', 'risk_factor', 'yields']) pd.testing.assert_frame_equal(expected_data, data)
Example #26
Source File: rq1_cnn_2d.py From DeepLearningSmells with Apache License 2.0 | 6 votes |
def measure_performance_dummy_classifier(): outfile = get_out_file("dummy_classifier") write_result(outfile, "smell,auc,precision,recall,f1,average_precision\n") for smell in smell_list: data_path = os.path.join(os.path.join(TOKENIZER_OUT_PATH, smell), DIM) input_data = get_all_data(data_path, smell) # clf = DummyClassifier(strategy='stratified', random_state=0) clf = DummyClassifier(strategy='most_frequent', random_state=0) inverted_train_labels = inputs.invert_labels(input_data.train_labels) clf.fit(input_data.train_data, inverted_train_labels) # clf.fit(input_data.train_data, input_data.train_labels) y_pred = clf.predict(input_data.eval_data) auc, precision, recall, f1, average_precision, fpr, tpr = \ metrics_util.get_all_metrics_(input_data.eval_labels, y_pred) write_result(outfile, smell +"," + str(auc) +"," + str(precision) +"," + str(recall) +"," + str(f1) +"," + str(average_precision) + "\n")
Example #27
Source File: test_optimization.py From sports-betting with MIT License | 5 votes |
def test_bettor_fit(): """Test fit method of bettor.""" bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['H', 'D']).fit(X, score1, score2, odds) np.testing.assert_array_equal(bettor.classifier_.classes_, np.array(['-', 'D', 'H'])) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'under_2.5']).fit(X, score1, score2, odds) np.testing.assert_array_equal(bettor.classifier_.classes_, np.array(['over_2.5', 'under_2.5'])) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'A']).fit(X, score1, score2, odds) np.testing.assert_array_equal(np.unique(bettor.classifier_.classes_), np.array(['A', 'over_2.5']))
Example #28
Source File: test_dummy.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_dummy_classifier_on_nan_value(): X = [[np.NaN]] y = [1] y_expected = [1] clf = DummyClassifier() clf.fit(X, y) y_pred = clf.predict(X) assert_array_equal(y_pred, y_expected)
Example #29
Source File: test_optimization.py From sports-betting with MIT License | 5 votes |
def test_bettor_predict(): """Test predict method of bettor.""" bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['H', 'D']).fit(X, score1, score2, odds) np.testing.assert_array_equal(np.unique(bettor.predict(X)), np.array(['-', 'D', 'H'])) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'under_2.5']).fit(X, score1, score2, odds) np.testing.assert_array_equal(np.unique(bettor.predict(X)), np.array(['over_2.5', 'under_2.5'])) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'A']).fit(X, score1, score2, odds) np.testing.assert_array_equal(np.unique(bettor.predict(X)), np.array(['A', 'over_2.5']))
Example #30
Source File: test_optimization.py From sports-betting with MIT License | 5 votes |
def test_bettor_predict_proba(): """Test predict probabilities method of bettor.""" bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['H', 'D']).fit(X, score1, score2, odds) assert bettor.predict_proba(X).shape == (30, 3) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'under_2.5']).fit(X, score1, score2, odds) assert bettor.predict_proba(X).shape == (30, 2) bettor = Bettor(classifier=DummyClassifier(random_state=0), targets=['over_2.5', 'A']).fit(X, score1, score2, odds) assert bettor.predict_proba(X).shape == (30, 2)