本文整理汇总了Python中numpy.lexsort方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.lexsort方法的具体用法?Python numpy.lexsort怎么用?Python numpy.lexsort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.lexsort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_duplicates
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def get_duplicates(M):
res = []
I = np.lexsort([M[:, i] for i in reversed(range(0, M.shape[1]))])
S = M[I, :]
i = 0
while i < S.shape[0] - 1:
l = []
while np.all(S[i, :] == S[i + 1, :]):
l.append(I[i])
i += 1
if len(l) > 0:
l.append(I[i])
res.append(l)
i += 1
return res
示例2: do
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def do(self):
# set the random seed if it is provided
if self.seed is not None:
np.random.seed(self.seed)
if self.n_dim == 1:
return np.array([[1.0]])
else:
val = self._do()
if isinstance(val, tuple):
ref_dirs, other = val[0], val[1:]
else:
ref_dirs = val
if self.scaling is not None:
ref_dirs = scale_reference_directions(ref_dirs, self.scaling)
# do ref_dirs is desired
if self.lexsort:
I = np.lexsort([ref_dirs[:, j] for j in range(ref_dirs.shape[1])][::-1])
ref_dirs = ref_dirs[I]
return ref_dirs
示例3: test_groupsort_indexer
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def test_groupsort_indexer():
a = np.random.randint(0, 1000, 100).astype(np.int64)
b = np.random.randint(0, 1000, 100).astype(np.int64)
result = libalgos.groupsort_indexer(a, 1000)[0]
# need to use a stable sort
# np.argsort returns int, groupsort_indexer
# always returns int64
expected = np.argsort(a, kind='mergesort')
expected = expected.astype(np.int64)
tm.assert_numpy_array_equal(result, expected)
# compare with lexsort
# np.lexsort returns int, groupsort_indexer
# always returns int64
key = a * 1000 + b
result = libalgos.groupsort_indexer(key, 1000000)[0]
expected = np.lexsort((b, a))
expected = expected.astype(np.int64)
tm.assert_numpy_array_equal(result, expected)
示例4: is_monotonic_increasing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def is_monotonic_increasing(self):
"""
return if the index is monotonic increasing (only equal or
increasing) values.
"""
# reversed() because lexsort() wants the most significant key last.
values = [self._get_level_values(i).values
for i in reversed(range(len(self.levels)))]
try:
sort_order = np.lexsort(values)
return Index(sort_order).is_monotonic
except TypeError:
# we have mixed types and np.lexsort is not happy
return Index(self.values).is_monotonic
示例5: order
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def order(self, order_):
# update the value itself
self._order = order_
# and the other stuff which is cached
self._perm = np.lexsort(order_.T)
# use advanced numpy indexing...
self._mps2lat_vals_idx = np.empty(self.shape, np.intp)
self._mps2lat_vals_idx[tuple(order_.T)] = np.arange(self.N_sites)
# versions for fixed u
self._mps_fix_u = []
self._mps2lat_vals_idx_fix_u = []
for u in range(len(self.unit_cell)):
mps_fix_u = np.nonzero(order_[:, -1] == u)[0]
self._mps_fix_u.append(mps_fix_u)
mps2lat_vals_idx = np.empty(self.Ls, np.intp)
mps2lat_vals_idx[tuple(order_[mps_fix_u, :-1].T)] = np.arange(self.N_cells)
self._mps2lat_vals_idx_fix_u.append(mps2lat_vals_idx)
self._mps_fix_u = tuple(self._mps_fix_u)
示例6: isort_qdata
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def isort_qdata(self):
"""(Lexiographically) sort ``self._qdata``; in place.
Lexsort ``self._qdata`` and ``self._data`` and set ``self._qdata_sorted = True``.
"""
if self._qdata_sorted:
return
if len(self._qdata) < 2:
self._qdata_sorted = True
return
perm = np.lexsort(self._qdata.T)
self._qdata = self._qdata[perm, :]
self._data = [self._data[p] for p in perm]
self._qdata_sorted = True
# reshaping ===============================================================
示例7: test_possible_couplings
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def test_possible_couplings():
lat_reg = lattice.Honeycomb(2,
3, [None, None],
order="snake",
bc="periodic",
bc_MPS="infinite")
lat_irreg = lattice.IrregularLattice(lat_reg, remove=[[0, 0, 0]])
u0, u1 = 0, 1
for lat in [lat_reg, lat_irreg]:
for dx in [(0, 0), (0, 1), (2, 1), (-1, -1)]:
print("dx =", dx)
mps0, mps1, lat_indices, coupling_shape = lat.possible_couplings(u0, u1, dx)
ops = [(None, [0, 0], u0), (None, dx, u1)]
m_ijkl, m_lat_indices, m_coupling_shape = lat.possible_multi_couplings(ops)
assert coupling_shape == m_coupling_shape
if len(lat_indices) == 0:
continue
sort = np.lexsort(lat_indices.T)
mps0, mps1, lat_indices = mps0[sort], mps1[sort], lat_indices[sort, :]
assert m_ijkl.shape == (len(mps0), 2)
m_sort = np.lexsort(m_lat_indices.T)
m_ijkl, m_lat_indices = m_ijkl[m_sort, :], m_lat_indices[m_sort, :]
npt.assert_equal(m_lat_indices, lat_indices)
npt.assert_equal(mps0, m_ijkl[:, 0])
npt.assert_equal(mps1, m_ijkl[:, 1])
示例8: _sum_duplicates
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def _sum_duplicates(self, row, col, data):
# Assumes (data, row, col) not in canonical format.
if len(data) == 0:
return row, col, data
order = np.lexsort((row, col))
row = row[order]
col = col[order]
data = data[order]
unique_mask = ((row[1:] != row[:-1]) |
(col[1:] != col[:-1]))
unique_mask = np.append(True, unique_mask)
row = row[unique_mask]
col = col[unique_mask]
unique_inds, = np.nonzero(unique_mask)
data = np.add.reduceat(data, unique_inds, dtype=self.dtype)
return row, col, data
示例9: test_resample_group_info
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def test_resample_group_info(self): # GH10914
for n, k in product((10000, 100000), (10, 100, 1000)):
dr = date_range(start='2015-08-27', periods=n // 10, freq='T')
ts = Series(np.random.randint(0, n // k, n).astype('int64'),
index=np.random.choice(dr, n))
left = ts.resample('30T').nunique()
ix = date_range(start=ts.index.min(), end=ts.index.max(),
freq='30T')
vals = ts.values
bins = np.searchsorted(ix.values, ts.index, side='right')
sorter = np.lexsort((vals, bins))
vals, bins = vals[sorter], bins[sorter]
mask = np.r_[True, vals[1:] != vals[:-1]]
mask |= np.r_[True, bins[1:] != bins[:-1]]
arr = np.bincount(bins[mask] - 1,
minlength=len(ix)).astype('int64', copy=False)
right = Series(arr, index=ix)
assert_series_equal(left, right)
示例10: gini
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def gini(actual, pred):
assert (len(actual) == len(pred))
all = np.asarray(np.c_[actual, pred, np.arange(len(actual))], dtype=np.float)
all = all[np.lexsort((all[:, 2], -1 * all[:, 1]))]
totalLosses = all[:, 0].sum()
giniSum = all[:, 0].cumsum().sum() / totalLosses
giniSum -= (len(actual) + 1) / 2.
return giniSum / len(actual)
示例11: get_all_exons
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def get_all_exons(self):
exons = np.vstack([i.exons for i in self.trans])
exons = np.unique(exons, axis=0)
ind = np.lexsort((exons[:,1],exons[:,0]))
if self.strand == '-':
ind = ind[::-1]
exons = exons[ind]
return exons
示例12: get_all_introns
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def get_all_introns(self):
for j in range(len(self.trans)):
self.trans[j].add_introns()
introns = np.vstack([i.introns for i in self.trans])
introns = np.unique(introns, axis=0)
ind = np.lexsort((introns[:,1],introns[:,0]))
if self.strand == '-':
ind = ind[::-1]
introns = introns[ind]
return introns
示例13: _get_spliceSites
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def _get_spliceSites(self, gene):
''' Get splice site sequence for all transcripts of a single gene.
Applied for normal gtf annotation.
'''
spliceSites = []
for transcript in gene.trans:
exons = transcript.exons
ind = np.lexsort((exons[:, 1], exons[:, 0]))
if len(exons) > 1:
if gene.strand == "+":
seq_ranges = exons[:-1, 1].reshape(-1, 1) + np.array([-self.overhang + 1, self.overhang])
else:
ind = ind[::-1]
exons = exons[ind]
seq_ranges = exons[:-1, 0].reshape(-1, 1) + np.array([-self.overhang, self.overhang - 1])
for i in range(seq_ranges.shape[0]):
spliceSite = SpliceSite(gene.chrom,
seq_ranges[i, 0],
seq_ranges[i, 1],
gene.strand,
transcript.tranID,
gene.geneID,
gene.biotype,
i)
# can call get_seq later in iterator to save memory
# spliceSite.seq = spliceSite.get_seq(self.fasta)
spliceSites.append(spliceSite)
return spliceSites
示例14: _get_spliceSites
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def _get_spliceSites(self, gene):
''' Get splice site sequence for all transcripts of a single gene.
Applied for normal gtf annotation.
'''
spliceSites = []
for transcript in gene.trans:
exons = transcript.exons
ind = np.lexsort((exons[:, 1], exons[:, 0]))
if len(exons) > 1:
if self.side == "5prime":
if gene.strand == "+":
seq_ranges = exons[:-1, 1].reshape(-1, 1) + np.array([-self.overhang_l + 1, self.overhang_r])
else:
ind = ind[::-1]
exons = exons[ind]
seq_ranges = exons[:-1, 0].reshape(-1, 1) + np.array([-self.overhang_r, self.overhang_l - 1])
else:
if gene.strand == "+":
seq_ranges = exons[1:, 0].reshape(-1, 1) + np.array([-self.overhang_r, self.overhang_l - 1])
else:
ind = ind[::-1]
exons = exons[ind]
seq_ranges = exons[1:, 1].reshape(-1, 1) + np.array([-self.overhang_l + 1, self.overhang_r])
for i in range(seq_ranges.shape[0]):
spliceSite = SpliceSite(gene.chrom,
seq_ranges[i, 0],
seq_ranges[i, 1],
gene.strand,
transcript.tranID,
gene.geneID,
gene.biotype,
i)
# can call get_seq later in iterator to save memory
# spliceSite.seq = spliceSite.get_seq(self.fasta)
spliceSites.append(spliceSite)
return spliceSites
示例15: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import lexsort [as 别名]
def __init__(self, n_dim, scaling=None, lexsort=True, verbose=False, seed=None, **kwargs) -> None:
super().__init__()
self.n_dim = n_dim
self.scaling = scaling
self.lexsort = lexsort
self.verbose = verbose
self.seed = seed