本文整理汇总了Python中numpy.tril_indices_from函数的典型用法代码示例。如果您正苦于以下问题:Python tril_indices_from函数的具体用法?Python tril_indices_from怎么用?Python tril_indices_from使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tril_indices_from函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pairplot_reg
def test_pairplot_reg(self):
vars = ["x", "y", "z"]
g = ag.pairplot(self.df, diag_kind="hist", kind="reg")
for ax in g.diag_axes:
nt.assert_equal(len(ax.patches), 10)
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
nt.assert_equal(len(ax.lines), 1)
nt.assert_equal(len(ax.collections), 2)
for i, j in zip(*np.tril_indices_from(g.axes, -1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
nt.assert_equal(len(ax.lines), 1)
nt.assert_equal(len(ax.collections), 2)
for i, j in zip(*np.diag_indices_from(g.axes)):
ax = g.axes[i, j]
nt.assert_equal(len(ax.collections), 0)
示例2: test_pairplot
def test_pairplot(self):
vars = ["x", "y", "z"]
g = ag.pairplot(self.df)
for ax in g.diag_axes:
assert len(ax.patches) > 1
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.tril_indices_from(g.axes, -1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.diag_indices_from(g.axes)):
ax = g.axes[i, j]
nt.assert_equal(len(ax.collections), 0)
g = ag.pairplot(self.df, hue="a")
n = len(self.df.a.unique())
for ax in g.diag_axes:
assert len(ax.lines) == n
assert len(ax.collections) == n
示例3: transform_covars_grad
def transform_covars_grad(self, internal_grad):
grad = np.empty((self.num_latent, self.get_covar_size()), dtype=np.float32)
for j in range(self.num_latent):
tmp = self._theano_transform_covars_grad(internal_grad[0, j], self.covars_cholesky[j])
tmp[np.diag_indices_from(tmp)] *= self.covars_cholesky[j][np.diag_indices_from(tmp)]
grad[j] = tmp[np.tril_indices_from(self.covars_cholesky[j])]
return grad.flatten()
示例4: test_map_diag_and_offdiag
def test_map_diag_and_offdiag(self):
vars = ["x", "y", "z"]
g = ag.PairGrid(self.df)
g.map_offdiag(plt.scatter)
g.map_diag(plt.hist)
for ax in g.diag_axes:
nt.assert_equal(len(ax.patches), 10)
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.tril_indices_from(g.axes, -1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.diag_indices_from(g.axes)):
ax = g.axes[i, j]
nt.assert_equal(len(ax.collections), 0)
示例5: _get_raw_covars
def _get_raw_covars(self):
flattened_covars = np.empty([self.num_latent, self.get_covar_size()], dtype=np.float32)
for i in xrange(self.num_latent):
raw_covars = self.covars_cholesky[i].copy()
raw_covars[np.diag_indices_from(raw_covars)] = np.log(raw_covars[np.diag_indices_from(raw_covars)])
flattened_covars[i] = raw_covars[np.tril_indices_from(raw_covars)]
return flattened_covars.flatten()
示例6: map_lower
def map_lower(self, func, **kwargs):
"""Plot with a bivariate function on the lower diagonal subplots.
Parameters
----------
func : callable plotting function
Must take x, y arrays as positional arguments and draw onto the
"currently active" matplotlib Axes.
"""
kw_color = kwargs.pop("color", None)
for i, j in zip(*np.tril_indices_from(self.axes, -1)):
hue_grouped = self.data.groupby(self.hue_vals)
for k, (label_k, data_k) in enumerate(hue_grouped):
ax = self.axes[i, j]
plt.sca(ax)
x_var = self.x_vars[j]
y_var = self.y_vars[i]
color = self.palette[k] if kw_color is None else kw_color
func(data_k[x_var], data_k[y_var], label=label_k,
color=color, **kwargs)
self._clean_axis(ax)
self._update_legend_data(ax)
if kw_color is not None:
kwargs["color"] = kw_color
self._add_axis_labels()
示例7: test_pairplot
def test_pairplot(self):
vars = ["x", "y", "z"]
g = pairplot(self.df)
for ax in g.diag_axes:
nt.assert_equal(len(ax.patches), 10)
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.tril_indices_from(g.axes, -1)):
ax = g.axes[i, j]
x_in = self.df[vars[j]]
y_in = self.df[vars[i]]
x_out, y_out = ax.collections[0].get_offsets().T
npt.assert_array_equal(x_in, x_out)
npt.assert_array_equal(y_in, y_out)
for i, j in zip(*np.diag_indices_from(g.axes)):
ax = g.axes[i, j]
nt.assert_equal(len(ax.collections), 0)
plt.close("all")
示例8: net_sample_multinomial
def net_sample_multinomial(A, minEdges, edgesPerSample=1, *args, **kwargs):
""" NETWORK SAMPLING ALGORITHM:
sample networks ties from multinomial distribution
defined as 1/AAT[i,j] normalized by sum(AAT[i>j])
problem: doesn't sufficiently cluster the resulting network
doesn't return exact number of ties, only at least as many as
specified minEdges
"""
draws = int(np.ceil(minEdges*1.2))
# pairwise distances between observations
dist = pdist(A) # what matrix to use: pdist(A) or just tril(AAT) directly?
invdist = dist
invdist[invdist != 0] = 1/invdist[invdist!=0] # prevent division by 0
thetavec = invdist / np.sum(invdist)
theta = squareform(thetavec)
# multinomial sample
n = np.shape(theta)[0]
Z = np.zeros((n,n))
# samp = sampleLinks(q=thetavec, edgesToDraw=1, draws=draws)
y = np.random.multinomial(edgesPerSample, thetavec, draws)
samp = np.asarray([np.mean([y[draw][item] for draw in np.arange(draws)]) for item in np.arange(len(thetavec))])
samp = np.ceil(samp)
# repeat until reaching enough network ties
while np.sum(samp) < minEdges:
draws = int(np.ceil(draws * 1.1)) #increase number of draws and try again
#samp = sampleLinks(q=thetavec,edgesToDraw=1,draws=draws)
y = np.random.multinomial(edgesPerSample, thetavec, draws)
samp = np.asarray([np.mean([y[draw][item] for draw in np.arange(draws)]) for item in np.arange(len(thetavec))])
samp = np.ceil(samp)
Z[np.tril_indices_from(Z, k =-1)] = samp
return (theta, Z)
示例9: set_params
def set_params(self, values):
self.lengthscales = values[:-1]
self.variance = values[-1]
L = np.zeros((self.num_dim, self.num_dim))
L[np.tril_indices_from(L)] = self.lengthscales
self.L_inv = inv(L)
self.projection = np.dot(self.L_inv.T, self.L_inv)
示例10: __init__
def __init__(self, lengthscale_mat, variance=1.0):
lengthscale_mat = np.asarray(lengthscale_mat)
assert lengthscale_mat.shape[0] == lengthscale_mat.shape[1]
self.num_dim = lengthscale_mat.shape[0]
self.params = np.concatenate((
lengthscale_mat[np.tril_indices_from(lengthscale_mat)],
np.array([variance])))
示例11: shepard
def shepard(self, xax=1, yax=2):
coords = self.U[:,[xax-1, yax-1]]
reducedD = np.zeros((coords.shape[0], coords.shape[0]))
for i in xrange(coords.shape[0]):
for j in xrange(coords.shape[0]):
d = coords[i,:] - coords[j,:]
reducedD[i, j] = np.sqrt( d.dot(d) )
reducedD = reducedD[np.tril_indices_from(reducedD, k=-1)]
originalD = self.y2[np.tril_indices_from(self.y2, k=-1)]
xmin = np.min(reducedD)
xmax = np.max(reducedD)
f, ax = py.subplots()
ax.plot(reducedD, originalD, 'ko')
ax.plot([xmin, xmax], [xmin, xmax], 'r--')
ax.set_xlabel('Distances in Reduced Space')
ax.set_ylabel('Distances in Original Matrix')
py.show()
示例12: set_covars
def set_covars(self, raw_covars):
raw_covars = raw_covars.reshape([self.num_latent, self.get_covar_size()])
for j in xrange(self.num_latent):
cholesky = np.zeros([self.num_dim, self.num_dim], dtype=np.float32)
cholesky[np.tril_indices_from(cholesky)] = raw_covars[j]
cholesky[np.diag_indices_from(cholesky)] = np.exp(cholesky[np.diag_indices_from(cholesky)])
self.covars_cholesky[j] = cholesky
self.covars[j] = mdot(self.covars_cholesky[j], self.covars_cholesky[j].T)
示例13: find_smallest_index
def find_smallest_index(matrice):
"""Return smallest number i,j index in a matrice
A Tuple (i,j) is returned.
Warning, the diagonal should have the largest number so it will never be choose
"""
index = np.tril_indices_from(matrice, -1)
return np.vstack(index)[:, matrice[index].argmin()]
示例14: _band_infinite
def _band_infinite():
'''Suppress the diagonal+- of a distance matrix'''
band = np.empty( (t, t) )
band[:] = np.inf
band[np.triu_indices_from(band, width)] = 0
band[np.tril_indices_from(band, -width)] = 0
return band
示例15: from_vector
def from_vector(x):
# Solution to the equation len(x) = n * (n + 1) / 2
n = int((math.sqrt(len(x) * 8 + 1) - 1) / 2)
result = np.zeros((n, n))
result[np.tril_indices_from(result, -1)] = x[n:]
result += result.transpose()
result[np.diag_indices_from(result)] = x[:n]
return result