本文整理汇总了Python中numpy.argpartition方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.argpartition方法的具体用法?Python numpy.argpartition怎么用?Python numpy.argpartition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.argpartition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_n
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def remove_n(self, n):
"""Get n items for removal."""
assert self.init_length + n <= self.cur_size
if self.eviction_strategy == 'rand':
# random removal
idxs = random.sample(xrange(self.init_length, self.cur_size), n)
elif self.eviction_strategy == 'fifo':
# overwrite elements in cyclical fashion
idxs = [
self.init_length +
(self.remove_idx + i) % (self.max_size - self.init_length)
for i in xrange(n)]
self.remove_idx = idxs[-1] + 1 - self.init_length
elif self.eviction_strategy == 'rank':
# remove lowest-priority indices
idxs = np.argpartition(self.priorities, n)[:n]
return idxs
示例2: _get_init_guess
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def _get_init_guess(na, nb, nroots, hdiag):
'''Initial guess is the single Slater determinant
'''
# The "nroots" lowest determinats based on energy expectation value.
ci0 = []
try:
addrs = numpy.argpartition(hdiag, nroots-1)[:nroots]
except AttributeError:
addrs = numpy.argsort(hdiag)[:nroots]
for addr in addrs:
x = numpy.zeros((na*nb))
x[addr] = 1
ci0.append(x.ravel())
# Add noise
ci0[0][0 ] += 1e-5
ci0[0][-1] -= 1e-5
return ci0
示例3: analyze
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def analyze(output_data):
#Results from the engine are returned as a list of 5D numpy arrays:
# (Number of Batches x Batch Size x C x H x W)
output = output_data.reshape(len(LABELS))
# Get result
top = np.argmax(output)
top = LABELS[top]
# Get top5
top5 = np.argpartition(output, -5, axis=-1)[-5:]
top5 = top5[np.argsort(output[top5])][::-1]
top5_classes = []
for i in top5:
top5_classes.append((LABELS[i], output[i]))
return [top, top5_classes]
#Arguments to create lite engine
示例4: analyze
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def analyze(output_data):
#Results from the engine are returned as a list of 5D numpy arrays:
# (Number of Batches x Batch Size x C x H x W)
output = output_data.reshape(len(LABELS))
# Get result
top = np.argmax(output)
top = LABELS[top]
# Get top5
top5 = np.argpartition(output, -5, axis=-1)[-5:]
top5 = top5[np.argsort(output[top5])][::-1]
top5_classes = []
for i in top5:
top5_classes.append((LABELS[i], output[i]))
return [top, top5_classes]
#Arguments to create lite engine
示例5: test_argequivalent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [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))
示例6: closest_docs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def closest_docs(self, query, k=1):
"""Closest docs by dot product between query and documents
in tfidf weighted word vector space.
"""
spvec = self.text2spvec(query)
res = spvec * self.doc_mat
if len(res.data) <= k:
o_sort = np.argsort(-res.data)
else:
o = np.argpartition(-res.data, k)[0:k]
o_sort = o[np.argsort(-res.data[o])]
doc_scores = res.data[o_sort]
doc_ids = [self.get_doc_id(i) for i in res.indices[o_sort]]
return doc_ids, doc_scores
示例7: iterate_eos_scores
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def iterate_eos_scores(new_scores, eos_idx, existing_cases = None, beam_width=None)->Tuple[Sequence, Sequence, Sequence]:
"""
Return the indices and scores corresponding to the eos word.
Meaning of returned values is the same as for iterate_best_score
"""
nb_cases, v_size = new_scores.shape
num_cases = np.arange(nb_cases, dtype=np.int32)
scores = -cuda.to_cpu(new_scores[:, eos_idx])
if existing_cases is not None:
need_to_return = np.logical_not(np.isin(num_cases, existing_cases))
num_cases = num_cases[need_to_return]
scores = scores[need_to_return]
idx_in_cases = np.full(num_cases.shape[0], eos_idx, dtype=np.int32)
if beam_width is not None:
if beam_width < len(scores):
idx_to_keep = np.argpartition(scores, beam_width)[:beam_width]
scores = scores[idx_to_keep]
num_cases = num_cases[idx_to_keep]
idx_in_cases = idx_in_cases[idx_to_keep]
return num_cases, idx_in_cases, scores
示例8: get_mAPs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def get_mAPs(q_output, q_labels, db_output, db_labels, Rs, dist_type):
dist = distance(q_output, db_output, dist_type=dist_type, pair=True)
unsorted_ids = np.argpartition(dist, Rs - 1)[:, :Rs]
APx = []
for i in range(dist.shape[0]):
label = q_labels[i, :]
label[label == 0] = -1
idx = unsorted_ids[i, :]
idx = idx[np.argsort(dist[i, :][idx])]
imatch = np.sum(np.equal(db_labels[idx[0: Rs], :], label), 1) > 0
rel = np.sum(imatch)
Lx = np.cumsum(imatch)
Px = Lx.astype(float) / np.arange(1, Rs + 1, 1)
if rel != 0:
APx.append(np.sum(Px * imatch) / rel)
return np.mean(np.array(APx))
示例9: _initialize_medoids
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def _initialize_medoids(self, D, n_clusters, random_state_):
"""Select initial mediods when beginning clustering."""
if self.init == "random": # Random initialization
# Pick random k medoids as the initial ones.
medoids = random_state_.choice(len(D), n_clusters)
elif self.init == "k-medoids++":
medoids = self._kpp_init(D, n_clusters, random_state_)
elif self.init == "heuristic": # Initialization by heuristic
# Pick K first data points that have the smallest sum distance
# to every other point. These are the initial medoids.
medoids = np.argpartition(np.sum(D, axis=1), n_clusters - 1)[
:n_clusters
]
else:
raise ValueError(f"init value '{self.init}' not recognized")
return medoids
# Copied from sklearn.cluster.k_means_._k_init
示例10: get_pairs
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def get_pairs(self, embeddings, labels):
if self.cpu:
embeddings = embeddings.cpu()
distance_matrix = pdist(embeddings)
labels = labels.cpu().data.numpy()
all_pairs = np.array(list(combinations(range(len(labels)), 2)))
all_pairs = torch.LongTensor(all_pairs)
positive_pairs = all_pairs[(labels[all_pairs[:, 0]] == labels[all_pairs[:, 1]]).nonzero()]
negative_pairs = all_pairs[(labels[all_pairs[:, 0]] != labels[all_pairs[:, 1]]).nonzero()]
negative_distances = distance_matrix[negative_pairs[:, 0], negative_pairs[:, 1]]
negative_distances = negative_distances.cpu().data.numpy()
top_negatives = np.argpartition(negative_distances, len(positive_pairs))[:len(positive_pairs)]
top_negative_pairs = negative_pairs[torch.LongTensor(top_negatives)]
return positive_pairs, top_negative_pairs
示例11: format_lines
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def format_lines(video_ids, predictions, labels, top_k):
batch_size = len(video_ids)
for video_index in range(batch_size):
n_recall = max(int(numpy.sum(labels[video_index])), 1)
# labels
label_indices = numpy.argpartition(labels[video_index], -n_recall)[-n_recall:]
label_predictions = [(class_index, predictions[video_index][class_index])
for class_index in label_indices]
label_predictions = sorted(label_predictions, key=lambda p: -p[1])
label_str = "\t".join(["%d\t%f"%(x,y) for x,y in label_predictions])
# predictions
top_k_indices = numpy.argpartition(predictions[video_index], -top_k)[-top_k:]
top_k_predictions = [(class_index, predictions[video_index][class_index])
for class_index in top_k_indices]
top_k_predictions = sorted(top_k_predictions, key=lambda p: -p[1])
top_k_str = "\t".join(["%d\t%f"%(x,y) for x,y in top_k_predictions])
# compute PERR
top_n_indices = numpy.argpartition(predictions[video_index], -n_recall)[-n_recall:]
positives = [labels[video_index][class_index]
for class_index in top_n_indices]
perr = sum(positives) / float(n_recall)
# URL
url = "https://www.youtube.com/watch?v=" + video_ids[video_index].decode('utf-8')
yield url + "\t" + str(1-perr) + "\t" + top_k_str + "\t" + label_str + "\n"
示例12: test_partition_cdtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [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])
示例13: _dominant_set_sparse
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def _dominant_set_sparse(s, k, is_thresh=False, norm=False):
"""Compute dominant set for a sparse matrix."""
if is_thresh:
mask = s > k
idx, data = np.where(mask), s[mask]
s = ssp.coo_matrix((data, idx), shape=s.shape)
else: # keep top k
nr, nc = s.shape
idx = np.argpartition(s, nc - k, axis=1)
col = idx[:, -k:].ravel() # idx largest
row = np.broadcast_to(np.arange(nr)[:, None], (nr, k)).ravel()
data = s[row, col].ravel()
s = ssp.coo_matrix((data, (row, col)), shape=s.shape)
if norm:
s.data /= s.sum(axis=1).A1[s.row]
return s.tocsr(copy=False)
示例14: _dominant_set_dense
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def _dominant_set_dense(s, k, is_thresh=False, norm=False, copy=True):
"""Compute dominant set for a dense matrix."""
if is_thresh:
s = s.copy() if copy else s
s[s <= k] = 0
else: # keep top k
nr, nc = s.shape
idx = np.argpartition(s, nc - k, axis=1)
row = np.arange(nr)[:, None]
if copy:
col = idx[:, -k:] # idx largest
data = s[row, col]
s = np.zeros_like(s)
s[row, col] = data
else:
col = idx[:, :-k] # idx smallest
s[row, col] = 0
if norm:
s /= np.nansum(s, axis=1, keepdims=True)
return s
示例15: csls_sparse
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import argpartition [as 别名]
def csls_sparse(X, Y, idx_x, idx_y, knn = 10):
def mean_similarity_sparse(X, Y, seeds, knn, axis = 1, metric = 'cosine'):
if axis == 1:
dists = sp.spatial.distance.cdist(X[seeds,:], Y, metric=metric)
else:
dists = sp.spatial.distance.cdist(X, Y[seeds,:], metric=metric).T
nghbs = np.argpartition(dists, knn, axis = 1) # for rows #[-k:] # argpartition returns top k not in order but it's efficient (doesnt sort all rows)
nghbs = nghbs[:,:knn]
nghbs_dists = np.concatenate([row[indices] for row, indices in zip(dists, nghbs)]).reshape(nghbs.shape)
nghbs_sims = 1 - nghbs_dists
return nghbs_sims.mean(axis = 1)
src_ms = mean_similarity_sparse(X, Y, idx_x, knn, axis = 1)
trg_ms = mean_similarity_sparse(X, Y, idx_y, knn, axis = 0)
sims = 1 - sp.spatial.distance.cdist(X[idx_x,:], Y[idx_y,:])
normalized_sims = ((2*sims - trg_ms).T - src_ms).T
print(normalized_sims)
nn = normalized_sims.argmax(axis=1).tolist()
return nn