本文整理汇总了Python中sklearn.utils.extmath.weighted_mode方法的典型用法代码示例。如果您正苦于以下问题:Python extmath.weighted_mode方法的具体用法?Python extmath.weighted_mode怎么用?Python extmath.weighted_mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils.extmath
的用法示例。
在下文中一共展示了extmath.weighted_mode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_redop
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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
示例2: test_random_weights
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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))
示例3: test_uniform_weights
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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)
示例4: _distance_weighting
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
def _distance_weighting(neighbor_vals, neighbor_dist):
neighbor_weights = 1 / neighbor_dist
X = weighted_mode(neighbor_vals, neighbor_weights, axis=1)
return X
示例5: _custom_weighting
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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
示例6: majority_vote
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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()
示例7: predict
# 需要导入模块: from sklearn.utils import extmath [as 别名]
# 或者: from sklearn.utils.extmath import weighted_mode [as 别名]
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