本文整理汇总了Python中numpy.partition方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.partition方法的具体用法?Python numpy.partition怎么用?Python numpy.partition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.partition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_argequivalent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def test_argequivalent(self):
""" Test it translates from arg<func> to <func> """
from numpy.random import rand
a = rand(3, 4, 5)
funcs = [
(np.sort, np.argsort, dict()),
(_add_keepdims(np.min), _add_keepdims(np.argmin), dict()),
(_add_keepdims(np.max), _add_keepdims(np.argmax), dict()),
(np.partition, np.argpartition, dict(kth=2)),
]
for func, argfunc, kwargs in funcs:
for axis in list(range(a.ndim)) + [None]:
a_func = func(a, axis=axis, **kwargs)
ai_func = argfunc(a, axis=axis, **kwargs)
assert_equal(a_func, take_along_axis(a, ai_func, axis=axis))
示例2: _get_scores
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def _get_scores(self):
dataset = self.dataset
self.model.train(dataset)
unlabeled_entry_ids, X_pool = dataset.get_unlabeled_entries()
if isinstance(self.model, ProbabilisticModel):
dvalue = self.model.predict_proba(X_pool)
elif isinstance(self.model, ContinuousModel):
dvalue = self.model.predict_real(X_pool)
if self.method == 'lc': # least confident
score = -np.max(dvalue, axis=1)
elif self.method == 'sm': # smallest margin
if np.shape(dvalue)[1] > 2:
# Find 2 largest decision values
dvalue = -(np.partition(-dvalue, 2, axis=1)[:, :2])
score = -np.abs(dvalue[:, 0] - dvalue[:, 1])
elif self.method == 'entropy':
score = np.sum(-dvalue * np.log(dvalue), axis=1)
return zip(unlabeled_entry_ids, score)
示例3: test_partition_cdtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def test_partition_cdtype(self):
d = array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
('Lancelot', 1.9, 38)],
dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
tgt = np.sort(d, order=['age', 'height'])
assert_array_equal(np.partition(d, range(d.size),
order=['age', 'height']),
tgt)
assert_array_equal(d[np.argpartition(d, range(d.size),
order=['age', 'height'])],
tgt)
for k in range(d.size):
assert_equal(np.partition(d, k, order=['age', 'height'])[k],
tgt[k])
assert_equal(d[np.argpartition(d, k, order=['age', 'height'])][k],
tgt[k])
d = array(['Galahad', 'Arthur', 'zebra', 'Lancelot'])
tgt = np.sort(d)
assert_array_equal(np.partition(d, range(d.size)), tgt)
for k in range(d.size):
assert_equal(np.partition(d, k)[k], tgt[k])
assert_equal(d[np.argpartition(d, k)][k], tgt[k])
示例4: argpartition
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def argpartition(a, kth, axis=-1):
"""Returns the indices that would partially sort an array.
Args:
a (cupy.ndarray): Array to be sorted.
kth (int or sequence of ints): Element index to partition by. If
supplied with a sequence of k-th it will partition all elements
indexed by k-th of them into their sorted position at once.
axis (int or None): Axis along which to sort. Default is -1, which
means sort along the last axis. If None is supplied, the array is
flattened before sorting.
Returns:
cupy.ndarray: Array of the same type and shape as ``a``.
.. note::
For its implementation reason, `cupy.argpartition` fully sorts the
given array as `cupy.argsort` does. It also does not support ``kind``
and ``order`` parameters that ``numpy.argpartition`` supports.
.. seealso:: :func:`numpy.argpartition`
"""
return a.argpartition(kth, axis=axis)
示例5: merge_outputs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def merge_outputs(self, detections):
results = {}
for i in range(1, self.num_classes + 1):
results[i] = np.concatenate([detection[i] for detection in detections], axis=0).astype(np.float32)
if len(self.scales) > 1 or self.opt.nms:
soft_nms(results[i], Nt=0.5, method=2)
scores = np.hstack([results[i][:,4] for i in range(1, self.num_classes + 1)])
if len(scores) > self.max_per_image:
kth = len(scores) - self.max_per_image
thresh = np.partition(scores, kth)[kth]
for i in range(1, self.num_classes + 1):
keep_inds = (results[i][:, 4] >= thresh)
results[i] = results[i][keep_inds]
return results
示例6: _proba_margin
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def _proba_margin(proba: np.ndarray) -> np.ndarray:
"""
Calculates the margin of the prediction probabilities.
Args:
proba: Prediction probabilities.
Returns:
Margin of the prediction probabilities.
"""
if proba.shape[1] == 1:
return np.zeros(shape=len(proba))
part = np.partition(-proba, 1, axis=1)
margin = - part[:, 0] + part[:, 1]
return margin
示例7: classifier_margin
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def classifier_margin(classifier: BaseEstimator, X: modALinput, **predict_proba_kwargs) -> np.ndarray:
"""
Classification margin uncertainty of the classifier for the provided samples. This uncertainty measure takes the
first and second most likely predictions and takes the difference of their probabilities, which is the margin.
Args:
classifier: The classifier for which the prediction margin is to be measured.
X: The samples for which the prediction margin of classification is to be measured.
**predict_proba_kwargs: Keyword arguments to be passed for the :meth:`predict_proba` of the classifier.
Returns:
Margin uncertainty, which is the difference of the probabilities of first and second most likely predictions.
"""
try:
classwise_uncertainty = classifier.predict_proba(X, **predict_proba_kwargs)
except NotFittedError:
return np.zeros(shape=(X.shape[0], ))
if classwise_uncertainty.shape[1] == 1:
return np.zeros(shape=(classwise_uncertainty.shape[0],))
part = np.partition(-classwise_uncertainty, 1, axis=1)
margin = - part[:, 0] + part[:, 1]
return margin
示例8: merge_outputs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def merge_outputs(self, detections):
results = {}
for j in range(1, self.num_classes + 1):
results[j] = np.concatenate(
[detection[j] for detection in detections], axis=0).astype(np.float32)
if len(self.scales) > 1 or self.opt.nms:
soft_nms(results[j], Nt=0.5, method=2)
scores = np.hstack(
[results[j][:, 4] for j in range(1, self.num_classes + 1)])
if len(scores) > self.max_per_image:
kth = len(scores) - self.max_per_image
thresh = np.partition(scores, kth)[kth]
for j in range(1, self.num_classes + 1):
keep_inds = (results[j][:, 4] >= thresh)
results[j] = results[j][keep_inds]
return results
示例9: merge_outputs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def merge_outputs(detections, num_classes):
# print(detections)
results = {}
max_per_image = 100
for j in range(1, num_classes + 1):
results[j] = np.concatenate(
[detection[j] for detection in detections], axis=0).astype(np.float32)
# if len(self.scales) > 1 or self.opt.nms:
results[j] = soft_nms(results[j], Nt=0.5, method=2, threshold=0.01)
# print(results)
scores = np.hstack([results[j][:, 4] for j in range(1, num_classes + 1)])
if len(scores) > max_per_image:
kth = len(scores) - max_per_image
thresh = np.partition(scores, kth)[kth]
for j in range(1, num_classes + 1):
keep_inds = (results[j][:, 4] >= thresh)
results[j] = results[j][keep_inds]
# print("after merge out\n", results)
return results2coco_boxes(results, num_classes)
示例10: merge_outputs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def merge_outputs(detections):
# print(detections)
results = {}
max_per_image = 100
for j in range(1, num_classes + 1):
results[j] = np.concatenate(
[detection[j] for detection in detections], axis=0).astype(np.float32)
# if len(self.scales) > 1 or self.opt.nms:
results[j] = soft_nms(results[j], Nt=0.5, method=2, threshold=0.001)
# print(results)
scores = np.hstack(
[results[j][:, 4] for j in range(1, num_classes + 1)])
if len(scores) > max_per_image:
kth = len(scores) - max_per_image
thresh = np.partition(scores, kth)[kth]
for j in range(1, num_classes + 1):
keep_inds = (results[j][:, 4] >= thresh)
results[j] = results[j][keep_inds]
# print("after merge out\n", results)
return results2coco_boxes(results)
示例11: find_threshold
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def find_threshold(self, np_predict, np_target):
# downsample 1/8
factor = self.factor
predict = nd.zoom(np_predict, (1.0, 1.0, 1.0/factor, 1.0/factor), order=1)
target = nd.zoom(np_target, (1.0, 1.0/factor, 1.0/factor), order=0)
n, c, h, w = predict.shape
min_kept = self.min_kept // (factor*factor) #int(self.min_kept_ratio * n * h * w)
input_label = target.ravel().astype(np.int32)
input_prob = np.rollaxis(predict, 1).reshape((c, -1))
valid_flag = input_label != self.ignore_label
valid_inds = np.where(valid_flag)[0]
label = input_label[valid_flag]
num_valid = valid_flag.sum()
if min_kept >= num_valid:
threshold = 1.0
elif num_valid > 0:
prob = input_prob[:,valid_flag]
pred = prob[label, np.arange(len(label), dtype=np.int32)]
threshold = self.thresh
if min_kept > 0:
k_th = min(len(pred), min_kept)-1
new_array = np.partition(pred, k_th)
new_threshold = new_array[k_th]
if new_threshold > self.thresh:
threshold = new_threshold
return threshold
示例12: test_partition_matrix_none
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def test_partition_matrix_none():
# gh-4301
# 2018-04-29: moved here from core.tests.test_multiarray
a = np.matrix([[2, 1, 0]])
actual = np.partition(a, 1, axis=None)
expected = np.matrix([[0, 1, 2]])
assert_equal(actual, expected)
assert_(type(expected) is np.matrix)
示例13: testPartitionIndicesExecution
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def testPartitionIndicesExecution(self):
# only 1 chunk when axis = -1
raw = np.random.rand(100, 10)
x = tensor(raw, chunk_size=10)
kth = [2, 5, 9]
r = partition(x, kth, return_index=True)
pr, pi = self.executor.execute_tensors(r)
np.testing.assert_array_equal(pr, np.take_along_axis(raw, pi, axis=-1))
np.testing.assert_array_equal(np.sort(raw)[:, kth], pr[:, kth])
x = tensor(raw, chunk_size=(22, 4))
r = partition(x, kth, return_index=True)
pr, pi = self.executor.execute_tensors(r)
np.testing.assert_array_equal(pr, np.take_along_axis(raw, pi, axis=-1))
np.testing.assert_array_equal(np.sort(raw)[:, kth], pr[:, kth])
raw = np.random.rand(100)
x = tensor(raw, chunk_size=23)
r = partition(x, kth, axis=0, return_index=True)
pr, pi = self.executor.execute_tensors(r)
np.testing.assert_array_equal(pr, np.take_along_axis(raw, pi, axis=-1))
np.testing.assert_array_equal(np.sort(raw)[kth], pr[kth])
示例14: top_proportions_sparse_csr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def top_proportions_sparse_csr(data, indptr, n):
values = np.zeros((indptr.size - 1, n), dtype=np.float64)
for i in numba.prange(indptr.size - 1):
start, end = indptr[i], indptr[i + 1]
vec = np.zeros(n, dtype=np.float64)
if end - start <= n:
vec[:end - start] = data[start:end]
total = vec.sum()
else:
vec[:] = -(np.partition(-data[start:end], n - 1)[:n])
total = (data[start:end]).sum() # Is this not just vec.sum()?
vec[::-1].sort()
values[i, :] = vec.cumsum() / total
return values
示例15: top_segment_proportions_dense
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import partition [as 别名]
def top_segment_proportions_dense(
mtx: Union[np.array, spmatrix], ns: Collection[int]
) -> np.ndarray:
# Currently ns is considered to be 1 indexed
ns = np.sort(ns)
sums = mtx.sum(axis=1)
partitioned = np.apply_along_axis(np.partition, 1, mtx, mtx.shape[1] - ns)[:, ::-1][:, :ns[-1]]
values = np.zeros((mtx.shape[0], len(ns)))
acc = np.zeros((mtx.shape[0]))
prev = 0
for j, n in enumerate(ns):
acc += partitioned[:, prev:n].sum(axis=1)
values[:, j] = acc
prev = n
return values / sums[:, None]