# Python numpy.triu_indices() Examples

The following are 30 code examples for showing how to use numpy.triu_indices(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module , or try the search function .

Example 1
```def _do_continue(self, algorithm):
do_continue = self.default.do_continue(algorithm)

# if the default says do not continue just follow that
if not do_continue:
return False

# additionally check for degenerated simplex
else:
X = algorithm.pop.get("X")

# degenerated simplex - get all edges and minimum and maximum length
D = vectorized_cdist(X, X)
val = D[np.triu_indices(len(X), 1)]
min_e, max_e = val.min(), val.max()

# either if the maximum length is very small or the ratio is degenerated
is_degenerated = max_e < 1e-16 or min_e / max_e < 1e-16

return not is_degenerated ```
Example 2
```def _do_continue(self, algorithm):
pop, problem = algorithm.pop, algorithm.problem

X, F = pop.get("X", "F")

ftol = np.abs(F[1:] - F[0]).max() <= self.ftol

# if the problem has bounds we can normalize the x space to to be more accurate
if problem.has_bounds():
xtol = np.abs((X[1:] - X[0]) / (problem.xu - problem.xl)).max() <= self.xtol
else:
xtol = np.abs(X[1:] - X[0]).max() <= self.xtol

# degenerated simplex - get all edges and minimum and maximum length
D = vectorized_cdist(X, X)
val = D[np.triu_indices(len(pop), 1)]
min_e, max_e = val.min(), val.max()

# either if the maximum length is very small or the ratio is degenerated
is_degenerated = max_e < 1e-16 or min_e / max_e < 1e-16

max_iter = algorithm.n_gen > self.n_max_iter
max_evals = algorithm.evaluator.n_eval > self.n_max_evals

return not (ftol or xtol or max_iter or max_evals or is_degenerated) ```
Example 3
```def __init__(self, features, using_cache=False, identity_init=True, eps=1e-3):
super().__init__(features, using_cache)

self.eps = eps

self.lower_indices = np.tril_indices(features, k=-1)
self.upper_indices = np.triu_indices(features, k=1)
self.diag_indices = np.diag_indices(features)

n_triangular_entries = ((features - 1) * features) // 2

self.lower_entries = nn.Parameter(torch.zeros(n_triangular_entries))
self.upper_entries = nn.Parameter(torch.zeros(n_triangular_entries))
self.unconstrained_upper_diag = nn.Parameter(torch.zeros(features))

self._initialize(identity_init) ```
Example 4
```def ut_dense(preds_ut, diagonal_offset):
"""Construct dense prediction matrix from upper triangular."""
ut_len, num_targets = preds_ut.shape

# infer original sequence length
seq_len = int(np.sqrt(2*ut_len + 0.25) - 0.5)
seq_len += diagonal_offset

# get triu indexes
ut_indexes = np.triu_indices(seq_len, diagonal_offset)
assert(len(ut_indexes[0]) == ut_len)

# assign to dense matrix
preds_dense = np.zeros(shape=(seq_len,seq_len,num_targets), dtype=preds_ut.dtype)
preds_dense[ut_indexes] = preds_ut

# symmetrize
preds_dense += np.transpose(preds_dense, axes=[1,0,2])

return preds_dense ```
Example 5
```def pairwise_distance_np(feature, squared=False):
"""Computes the pairwise distance matrix in numpy.
Args:
feature: 2-D numpy array of size [number of data, feature dimension]
squared: Boolean. If true, output is the pairwise squared euclidean
distance matrix; else, output is the pairwise euclidean distance matrix.
Returns:
pairwise_distances: 2-D numpy array of size
[number of data, number of data].
"""
triu = np.triu_indices(feature.shape[0], 1)
upper_tri_pdists = np.linalg.norm(feature[triu[1]] - feature[triu[0]], axis=1)
if squared:
upper_tri_pdists **= 2.
num_data = feature.shape[0]
pairwise_distances = np.zeros((num_data, num_data))
pairwise_distances[np.triu_indices(num_data, 1)] = upper_tri_pdists
# Make symmetrical.
pairwise_distances = pairwise_distances + pairwise_distances.T - np.diag(
pairwise_distances.diagonal())
return pairwise_distances ```
Example 6
```def _get_lvec(labels):
"""
Constructs a label vector for an arbitrary number of labels
Assumes that our model is quadratic in the labels

Parameters
----------
labels: numpy ndarray
pivoted label values for one star

Returns
-------
lvec: numpy ndarray
label vector
"""
nlabels = len(labels)
# specialized to second-order model
linear_terms = labels
linear_terms)[np.triu_indices(nlabels)]
return lvec ```
Example 7
```def compare_ordered(vals, alpha):
'''simple ordered sequential comparison of means

vals : array_like
means or rankmeans for independent groups

incomplete, no return, not used yet
'''
vals = np.asarray(vals)
alphaf = alpha  # Notation ?
sortind = np.argsort(vals)
pvals = vals[sortind]
sortrevind = sortind.argsort()
ntests = len(vals)
#alphacSidak = 1 - np.power((1. - alphaf), 1./ntests)
#alphacBonf = alphaf / float(ntests)
v1, v2 = np.triu_indices(ntests, 1)
#v1,v2 have wrong sequence
for i in range(4):
for j in range(4,i, -1):
print(i,j) ```
Example 8
```def _hessian_geom(self, params):
exog = self.exog
y = self.endog[:,None]
mu = self.predict(params)[:,None]

# for dl/dparams dparams
dim = exog.shape[1]
hess_arr = np.empty((dim, dim))
const_arr = mu*(1+y)/(mu+1)**2
for i in range(dim):
for j in range(dim):
if j > i:
continue
hess_arr[i,j] = np.sum(-exog[:,i,None] * exog[:,j,None] *
const_arr, axis=0)
tri_idx = np.triu_indices(dim, k=1)
hess_arr[tri_idx] = hess_arr.T[tri_idx]
return hess_arr ```
Example 9
```def _helmert_contrast(self, levels):
n = len(levels)
#http://www.ats.ucla.edu/stat/sas/webbooks/reg/chapter5/sasreg5.htm#HELMERT
#contr = np.eye(n - 1)
#int_range = np.arange(n - 1., 1, -1)
#denom = np.repeat(int_range, np.arange(n - 2, 0, -1))
#contr[np.tril_indices(n - 1, -1)] = -1. / denom

#http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm#HELMERT
#contr = np.zeros((n - 1., n - 1))
#int_range = np.arange(n, 1, -1)
#denom = np.repeat(int_range[:-1], np.arange(n - 2, 0, -1))
#contr[np.diag_indices(n - 1)] = (int_range - 1.) / int_range
#contr[np.tril_indices(n - 1, -1)] = -1. / denom
#contr = np.vstack((contr, -1./int_range))

#r-like
contr = np.zeros((n, n - 1))
contr[1:][np.diag_indices(n - 1)] = np.arange(1, n)
contr[np.triu_indices(n - 1)] = -1
return contr ```
Example 10
```def _diff_contrast(self, levels):
nlevels = len(levels)
contr = np.zeros((nlevels, nlevels-1))
int_range = np.arange(1, nlevels)
upper_int = np.repeat(int_range, int_range)
row_i, col_i = np.triu_indices(nlevels-1)
# we want to iterate down the columns not across the rows
# it would be nice if the index functions had a row/col order arg
col_order = np.argsort(col_i)
contr[row_i[col_order],
col_i[col_order]] = (upper_int-nlevels)/float(nlevels)
lower_int = np.repeat(int_range, int_range[::-1])
row_i, col_i = np.tril_indices(nlevels-1)
# we want to iterate down the columns not across the rows
col_order = np.argsort(col_i)
contr[row_i[col_order]+1, col_i[col_order]] = lower_int/float(nlevels)
return contr ```
Example 11
```def pairwise_distances(feature, squared=True):
"""Computes the pairwise distance matrix in numpy.

Args:
feature: 2-D numpy array of size [number of data, feature dimension]
squared: Boolean. If true, output is the pairwise squared euclidean
distance matrix; else, output is the pairwise euclidean distance matrix.

Returns:
pdists: 2-D numpy array of size
[number of data, number of data].
"""
triu = np.triu_indices(feature.shape[0], 1)
upper_tri_pdists = np.linalg.norm(feature[triu[1]] - feature[triu[0]], axis=1)
if squared:
upper_tri_pdists **= 2.
num_data = feature.shape[0]
pdists = np.zeros((num_data, num_data))
pdists[np.triu_indices(num_data, 1)] = upper_tri_pdists
# Make symmetrical.
pdists = pdists + pdists.T - np.diag(
pdists.diagonal())
return pdists ```
Example 12
```def test_idxiter():
n_channels = data.shape[0]
# Upper-triangular part, including diag
idx0, idx1 = np.triu_indices(n_channels)
triu_indices = np.array([np.arange(idx0.size), idx0, idx1])
triu_indices2 = np.array(list(_idxiter(n_channels, include_diag=True)))
# Upper-triangular part, without diag
idx2, idx3 = np.triu_indices(n_channels, 1)
triu_indices_nodiag = np.array([np.arange(idx2.size), idx2, idx3])
triu_indices2_nodiag = np.array(list(_idxiter(n_channels,
include_diag=False)))
assert_almost_equal(triu_indices, triu_indices2.transpose())
assert_almost_equal(triu_indices_nodiag, triu_indices2_nodiag.transpose())
# Upper and lower-triangular parts, without diag
expected = [(i, j) for _, (i, j) in
enumerate(np.ndindex((n_channels, n_channels))) if i != j]
assert_equal(np.array([(i, j) for _, i, j in _idxiter(n_channels,
triu=False)]),
expected) ```
Example 13
```def pairwise_distance_np(feature, squared=False):
"""Computes the pairwise distance matrix in numpy.

Args:
feature: 2-D numpy array of size [number of data, feature dimension]
squared: Boolean. If true, output is the pairwise squared euclidean
distance matrix; else, output is the pairwise euclidean distance matrix.

Returns:
pairwise_distances: 2-D numpy array of size
[number of data, number of data].
"""
triu = np.triu_indices(feature.shape[0], 1)
upper_tri_pdists = np.linalg.norm(feature[triu[1]] - feature[triu[0]], axis=1)
if squared:
upper_tri_pdists **= 2.
num_data = feature.shape[0]
pairwise_distances = np.zeros((num_data, num_data))
pairwise_distances[np.triu_indices(num_data, 1)] = upper_tri_pdists
# Make symmetrical.
pairwise_distances = pairwise_distances + pairwise_distances.T - np.diag(
pairwise_distances.diagonal())
return pairwise_distances ```
Example 14
```def pairwise_distances(feature, squared=True):
"""Computes the pairwise distance matrix in numpy.

Args:
feature: 2-D numpy array of size [number of data, feature dimension]
squared: Boolean. If true, output is the pairwise squared euclidean
distance matrix; else, output is the pairwise euclidean distance matrix.

Returns:
pdists: 2-D numpy array of size
[number of data, number of data].
"""
triu = np.triu_indices(feature.shape[0], 1)
upper_tri_pdists = np.linalg.norm(feature[triu[1]] - feature[triu[0]], axis=1)
if squared:
upper_tri_pdists **= 2.
num_data = feature.shape[0]
pdists = np.zeros((num_data, num_data))
pdists[np.triu_indices(num_data, 1)] = upper_tri_pdists
# Make symmetrical.
pdists = pdists + pdists.T - np.diag(
pdists.diagonal())
return pdists ```
Example 15
```def from_tri_2_sym(tri, dim):
"""convert a upper triangular matrix in 1D format
to 2D symmetric matrix

Parameters
----------

tri: 1D array
Contains elements of upper triangular matrix

dim : int
The dimension of target matrix.

Returns
-------

symm : 2D array
Symmetric matrix in shape=[dim, dim]
"""
symm = np.zeros((dim, dim))
symm[np.triu_indices(dim)] = tri
return symm ```
Example 16
```def compute_rand_index(emb, labels):
"""
https://en.wikipedia.org/wiki/Rand_index
"""
n = len(emb)
k = np.unique(labels).size

m = KMeans(k)
m.fit(emb)
emb_labels = m.predict(emb)

agreements = 0
for i, j in zip(*np.triu_indices(n, 1)):
emb_same = emb_labels[i] == emb_labels[j]
gt_same = labels[i] == labels[j]

if emb_same == gt_same:
agreements += 1

return float(agreements) / (n * (n-1) / 2) ```
Example 17
```def predict(self, m, s=None, t=None):
D = m.shape[0]
if t is not None:
self.t = t
t = self.t

u, z, I, L = self.u_nominal, self.z_nominal, self.I_, self.L_

if s is None:
s = np.zeros((D, D))

# construct flattened state covariance vector
z_t = tt_.concatenate([m.flatten(), s[self.triu_indices]])
# compute control
u_t = u[t] + I[t] + L[t].dot(z_t - z[t])

# limit the controller output
#u_t = tt.clip(u_t, -self.maxU,  self.maxU)

U = u_t.shape[0]
self.t += 1
return u_t, tt_.zeros((U, U)), tt_.zeros((D, U)) ```
Example 18
```def calc_dist(self, pop, other=None):
X = self.func(pop)

if other is None:
D = cdist(X, X)
D[np.triu_indices(len(X))] = np.inf
else:
_X = self.func(other)
D = cdist(X, _X)

return D ```
Example 19
```def calc_potential_energy(A, d):
i, j = anp.triu_indices(len(A), 1)
D = anp.sqrt(squared_dist(A, A)[i, j])
energy = anp.log((1 / D ** d).mean())
return energy ```
Example 20
```def calc_potential_energy_with_grad(x, d, return_mutual_dist=False):
diff = (x[:, None] - x[None, :])
# calculate the squared euclidean from each point to another
dist = np.sqrt((diff ** 2).sum(axis=2))

# make sure the distance to itself does not count
np.fill_diagonal(dist, np.inf)

# epsilon which is the smallest distance possible to avoid an overflow during gradient calculation
eps = 10 ** (-320 / (d + 2))
b = dist < eps
dist[b] = eps

# select only upper triangular matrix to have each mutual distance once
mutual_dist = dist[np.triu_indices(len(x), 1)]

# calculate the energy by summing up the squared distances
energy = (1 / mutual_dist ** d).sum()
log_energy = - np.log(len(mutual_dist)) + np.log(energy)

grad = (-d * diff) / (dist ** (d + 2))[..., None]

if return_mutual_dist:
ret.append(mutual_dist)

return tuple(ret) ```
Example 21
```def average_distance_to_other_points(ref_dirs):
D = vectorized_cdist(ref_dirs, ref_dirs)
D = D[np.triu_indices(len(ref_dirs), 1)]
return D.mean() ```
Example 22
```def potential_energy(x):
_x = x / x.sum(axis=1)[:, None]
D = ((_x[:, None] - _x[None, :]) ** 2).sum(axis=2)
D = D[anp.triu_indices(len(_x), 1)]
return (1 / D).mean() ```
Example 23
```def calc_potential_energy(A):
d = (A.shape[1] ** 2)
i, j = np.triu_indices(len(A), 1)
D = np.sqrt(squared_dist(A, A)[i, j])
energy = np.log((1 / D ** d).mean())
return energy ```
Example 24
```def _parse(plines):
nelecs = [ int(nelec) for nelec in plines.pop(0).split() ]
rnc_ppl = plines.pop(0).split()
rloc = float(rnc_ppl[0])
nexp = int(rnc_ppl[1])
cexp = [ float(c) for c in rnc_ppl[2:] ]
nproj_types = int(plines.pop(0))
r = []
nproj = []
hproj = []
for p in range(nproj_types):
rnh_ppnl = plines.pop(0).split()
r.append(float(rnh_ppnl[0]))
nproj.append(int(rnh_ppnl[1]))
hproj_p_ij = []
for h in rnh_ppnl[2:]:
hproj_p_ij.append(float(h))
for i in range(1,nproj[-1]):
for h in plines.pop(0).split():
hproj_p_ij.append(float(h))
hproj_p = np.zeros((nproj[-1],nproj[-1]))
hproj_p[np.triu_indices(nproj[-1])] = [ h for h in hproj_p_ij ]
hproj_p_symm = hproj_p + hproj_p.T - np.diag(hproj_p.diagonal())
hproj.append(hproj_p_symm.tolist())

pseudo_params = [nelecs,
rloc, nexp, cexp,
nproj_types]
for ri,ni,hi in zip(r,nproj,hproj):
pseudo_params.append([ri, ni, hi])
return pseudo_params ```
Example 25
```def __init__(self, features, num_householder, using_cache=False):
super().__init__(features, using_cache)

# Parameterization for R
self.upper_indices = np.triu_indices(features, k=1)
self.diag_indices = np.diag_indices(features)
n_triangular_entries = ((features - 1) * features) // 2
self.upper_entries = nn.Parameter(torch.zeros(n_triangular_entries))
self.log_upper_diag = nn.Parameter(torch.zeros(features))

# Parameterization for Q
self.orthogonal = transforms.HouseholderSequence(
features=features, num_transforms=num_householder)

self._initialize() ```
Example 26
```def make2DarrayFrom1DupperTriangle(upperTriangle1D,N,includesDiagnol=False):
a = np.zeros([N,N])
n=N if includesDiagnol else N-1
indices = list(np.triu_indices(n))
if not includesDiagnol: indices[1]+=1
a[indices] = a[indices[::-1]] = upperTriangle1D
return a

########################## plot topos with average branch lengths ```
Example 27
```def test_upper_tri_coulomb_matrix(self):
"""
Test upper triangular CoulombMatrix.
"""
f = cm.CoulombMatrix(self.mol.GetNumAtoms(), upper_tri=True)
rval = f([self.mol])
size = np.triu_indices(self.mol.GetNumAtoms())[0].size
assert rval.shape == (1, self.mol.GetNumConformers(), size) ```
Example 28
```def test_upper_tri_coulomb_matrix_padding(self):
"""
Test upper triangular CoulombMatrix with padding.
"""
f = cm.CoulombMatrix(max_atoms=self.mol.GetNumAtoms() * 2, upper_tri=True)
rval = f([self.mol])
size = np.triu_indices(self.mol.GetNumAtoms() * 2)[0].size
assert rval.shape == (1, self.mol.GetNumConformers(), size) ```
Example 29
```def test_coulomb_matrix_no_hydrogens(self):
"""
Test hydrogen removal.
"""
from rdkit import Chem
mol = Chem.RemoveHs(self.mol)
assert mol.GetNumAtoms() < self.mol.GetNumAtoms()
f = cm.CoulombMatrix(
max_atoms=mol.GetNumAtoms(), remove_hydrogens=True, upper_tri=True)
rval = f([self.mol])  # use the version with hydrogens
size = np.triu_indices(mol.GetNumAtoms())[0].size
assert rval.shape == (1, mol.GetNumConformers(), size) ```
Example 30
```def test_coulomb_matrix_hydrogens(self):