Python sklearn.utils.extmath.weighted_mode() Examples
The following are 9
code examples of sklearn.utils.extmath.weighted_mode().
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.extmath
, or try the search function
.
Example #1
Source File: parcellation.py From BrainSpace with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_redop(red_op, weights=None, axis=None): if red_op in ['mean', 'average']: if weights is None: def fred(x, w): return np.mean(x, axis=axis) else: def fred(x, w): return np.average(x, weights=w, axis=axis) elif red_op == 'median': def fred(x, w): return np.median(x, axis=axis) elif red_op == 'mode': if weights is None: def fred(x, w): return mode(x, axis=axis)[0].ravel() else: def fred(x, w): return weighted_mode(x, w, axis=axis) elif red_op == 'sum': def fred(x, w): return np.sum(x if w is None else w * x, axis=axis) elif red_op == 'max': def fred(x, w): return np.max(x, axis=axis) elif red_op == 'min': def fred(x, w): return np.min(x, axis=axis) else: raise ValueError("Unknown reduction operation '{0}'".format(red_op)) return fred
Example #2
Source File: test_extmath.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_random_weights(): # set this up so that each row should have a weighted mode of 6, # with a score that is easily reproduced mode_result = 6 rng = np.random.RandomState(0) x = rng.randint(mode_result, size=(100, 10)) w = rng.random_sample(x.shape) x[:, :5] = mode_result w[:, :5] += 1 mode, score = weighted_mode(x, w, axis=1) assert_array_equal(mode, mode_result) assert_array_almost_equal(score.ravel(), w[:, :5].sum(1))
Example #3
Source File: test_extmath.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_random_weights(): # set this up so that each row should have a weighted mode of 6, # with a score that is easily reproduced mode_result = 6 rng = np.random.RandomState(0) x = rng.randint(mode_result, size=(100, 10)) w = rng.random_sample(x.shape) x[:, :5] = mode_result w[:, :5] += 1 mode, score = weighted_mode(x, w, axis=1) assert_array_equal(mode, mode_result) assert_array_almost_equal(score.ravel(), w[:, :5].sum(1))
Example #4
Source File: test_extmath.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_uniform_weights(): # with uniform weights, results should be identical to stats.mode rng = np.random.RandomState(0) x = rng.randint(10, size=(10, 5)) weights = np.ones(x.shape) for axis in (None, 0, 1): mode, score = stats.mode(x, axis) mode2, score2 = weighted_mode(x, weights, axis) assert_array_equal(mode, mode2) assert_array_equal(score, score2)
Example #5
Source File: estimators.py From Pyspatialml with GNU General Public License v3.0 | 5 votes |
def _distance_weighting(neighbor_vals, neighbor_dist): neighbor_weights = 1 / neighbor_dist X = weighted_mode(neighbor_vals, neighbor_weights, axis=1) return X
Example #6
Source File: estimators.py From Pyspatialml with GNU General Public License v3.0 | 5 votes |
def _custom_weighting(self, neighbor_vals, neighbor_dist): neighbor_weights = self.weights(neighbor_dist) new_X = weighted_mode(neighbor_vals, neighbor_weights, axis=1) return new_X
Example #7
Source File: test_extmath.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_uniform_weights(): # with uniform weights, results should be identical to stats.mode rng = np.random.RandomState(0) x = rng.randint(10, size=(10, 5)) weights = np.ones(x.shape) for axis in (None, 0, 1): mode, score = stats.mode(x, axis) mode2, score2 = weighted_mode(x, weights, axis) assert_array_equal(mode, mode2) assert_array_equal(score, score2)
Example #8
Source File: score_comb.py From combo with BSD 2-Clause "Simplified" License | 4 votes |
def majority_vote(scores, n_classes=2, weights=None): """Combination method to merge the scores from multiple estimators by majority vote. Parameters ---------- scores : numpy array of shape (n_samples, n_estimators) Score matrix from multiple estimators on the same samples. n_classes : int, optional (default=2) The number of classes in scores matrix weights : numpy array of shape (1, n_estimators) If specified, using weighted majority weight. Returns ------- combined_scores : numpy array of shape (n_samples, ) The combined scores. """ scores = check_array(scores) # assert only discrete scores are combined with majority vote check_classification_targets(scores) n_samples, n_estimators = scores.shape[0], scores.shape[1] vote_results = np.zeros([n_samples, ]) if weights is not None: assert_equal(scores.shape[1], weights.shape[1]) # equal weights if not set else: weights = np.ones([1, n_estimators]) for i in range(n_samples): vote_results[i] = weighted_mode(scores[i, :], weights)[0][0] return vote_results.ravel()
Example #9
Source File: _time_series_neighbors.py From sktime with BSD 3-Clause "New" or "Revised" License | 4 votes |
def predict(self, X): """Predict the class labels for the provided data Parameters ---------- X : sktime-format pandas dataframe or array-like, shape (n_query, n_features), \ or (n_query, n_indexed) if metric == 'precomputed' Test samples. Returns ------- y : array of shape [n_samples] or [n_samples, n_outputs] Class labels for each data sample. """ self.check_is_fitted() temp = check_array.__wrapped__.__code__ check_array.__wrapped__.__code__ = _check_array_ts.__code__ neigh_dist, neigh_ind = self.kneighbors(X) classes_ = self.classes_ _y = self._y if not self.outputs_2d_: _y = self._y.reshape((-1, 1)) classes_ = [self.classes_] n_outputs = len(classes_) n_samples = X.shape[0] weights = _get_weights(neigh_dist, self.weights) y_pred = np.empty((n_samples, n_outputs), dtype=classes_[0].dtype) for k, classes_k in enumerate(classes_): if weights is None: mode, _ = stats.mode(_y[neigh_ind, k], axis=1) else: mode, _ = weighted_mode(_y[neigh_ind, k], weights, axis=1) mode = np.asarray(mode.ravel(), dtype=np.intp) y_pred[:, k] = classes_k.take(mode) if not self.outputs_2d_: y_pred = y_pred.ravel() check_array.__wrapped__.__code__ = temp return y_pred