def _init_coefs(X, method='corrcoef'):
if method == 'corrcoef':
return np.corrcoef(X, rowvar=False), 1.0
elif method == 'cov':
init_cov = np.cov(X, rowvar=False)
return init_cov, np.max(np.abs(np.triu(init_cov)))
elif method == 'spearman':
return spearman_correlation(X, rowvar=False), 1.0
elif method == 'kendalltau':
return kendalltau_correlation(X, rowvar=False), 1.0
elif callable(method):
return method(X)
else:
raise ValueError(
("initialize_method must be 'corrcoef' or 'cov', "
"passed \'{}\' .".format(method))
)
python类triu()的实例源码
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
def potential_numba_array(cluster):
d = distances_numba_array(cluster)
# Original: dtri = np.triu(d)
# np.triu is not supported; so write my own loop to clear the
# lower triangle
for i in range(d.shape[0]):
for j in range(d.shape[1]):
if i > j:
d[i, j] = 0
# Original: lj_numba_array(d[d > 1e-6]).sum()
# d[d > 1e-6] is not supported due to the indexing with boolean
# array. Replace with custom loop.
energy = 0.0
for v in d.flat:
if v > 1e-6:
energy += lj_numba_array(v)
return energy
def pairwise_expansion(x, func, reflexive=True):
"""Computes func(xi, xj) over all possible indices i and j, where func is an arbitrary function
if reflexive == False, only pairs with i != j are considered
"""
x_height, x_width = x.shape
if reflexive:
k = 0
else:
k = 1
mask = numpy.triu(numpy.ones((x_width, x_width)), k) > 0.5
# mask = mask.reshape((1,x_width,x_width))
y1 = x.reshape(x_height, x_width, 1)
y2 = x.reshape(x_height, 1, x_width)
yexp = func(y1, y2)
# print "yexp.shape=", yexp.shape
# print "mask.shape=", mask.shape
out = yexp[:, mask]
# print "out.shape=", out.shape
# yexp.reshape((x_height, N*N))
return out
def products_2(x, func, k=0):
"""Computes func(xi, xj) over all possible indices i and j constrained to j >= i+k.
func is an arbitrary function, and k >= 0 is an integer
"""
x_height, x_width = x.shape
mask = numpy.triu(numpy.ones((x_width, x_width)), k) > 0.5
z1 = x.reshape(x_height, x_width, 1)
z2 = x.reshape(x_height, 1, x_width)
yexp = func(z1, z2) # twice computation, but performance gain due to lack of loops
out = yexp[:, mask]
return out
tangentspace.py 文件源码
项目:decoding-brain-challenge-2016
作者: alexandrebarachant
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def tangent_space(covmats, Cref):
"""Project a set of covariance matrices in the tangent space according to the given reference point Cref
:param covmats: Covariance matrices set, Ntrials X Nchannels X Nchannels
:param Cref: The reference covariance matrix
:returns: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
"""
Nt, Ne, Ne = covmats.shape
Cm12 = invsqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
T = numpy.empty((Nt, Ne * (Ne + 1) / 2))
coeffs = (
numpy.sqrt(2) *
numpy.triu(
numpy.ones(
(Ne,
Ne)),
1) +
numpy.eye(Ne))[idx]
for index in range(Nt):
tmp = numpy.dot(numpy.dot(Cm12, covmats[index, :, :]), Cm12)
tmp = logm(tmp)
T[index, :] = numpy.multiply(coeffs, tmp[idx])
return T
tangentspace.py 文件源码
项目:decoding-brain-challenge-2016
作者: alexandrebarachant
项目源码
文件源码
阅读 39
收藏 0
点赞 0
评论 0
def untangent_space(T, Cref):
"""Project a set of Tangent space vectors in the manifold according to the given reference point Cref
:param T: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
:param Cref: The reference covariance matrix
:returns: A set of Covariance matrix, Ntrials X Nchannels X Nchannels
"""
Nt, Nd = T.shape
Ne = int((numpy.sqrt(1 + 8 * Nd) - 1) / 2)
C12 = sqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
covmats = numpy.empty((Nt, Ne, Ne))
covmats[:, idx[0], idx[1]] = T
for i in range(Nt):
covmats[i] = numpy.diag(numpy.diag(covmats[i])) + numpy.triu(
covmats[i], 1) / numpy.sqrt(2) + numpy.triu(covmats[i], 1).T / numpy.sqrt(2)
covmats[i] = expm(covmats[i])
covmats[i] = numpy.dot(numpy.dot(C12, covmats[i]), C12)
return covmats
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
def has_approx_support(m, m_hat, prob=0.01):
"""Returns 1 if model selection error is less than or equal to prob rate,
0 else.
NOTE: why does np.nonzero/np.flatnonzero create so much problems?
"""
m_nz = np.flatnonzero(np.triu(m, 1))
m_hat_nz = np.flatnonzero(np.triu(m_hat, 1))
upper_diagonal_mask = np.flatnonzero(np.triu(np.ones(m.shape), 1))
not_m_nz = np.setdiff1d(upper_diagonal_mask, m_nz)
intersection = np.in1d(m_hat_nz, m_nz) # true positives
not_intersection = np.in1d(m_hat_nz, not_m_nz) # false positives
true_positive_rate = 0.0
if len(m_nz):
true_positive_rate = 1. * np.sum(intersection) / len(m_nz)
true_negative_rate = 1. - true_positive_rate
false_positive_rate = 0.0
if len(not_m_nz):
false_positive_rate = 1. * np.sum(not_intersection) / len(not_m_nz)
return int(np.less_equal(true_negative_rate + false_positive_rate, prob))
def read_mongodb_matrix(tickers, matrix_name):
mis = MatrixItem.objects(i__in = tickers,
j__in = tickers,
matrix_name = matrix_name)
n = len(tickers)
available_tickers = set([mi.i for mi in mis])
np.random.seed(n)
a = np.absolute(np.random.normal(0, 0.001, [n, n]))
a_triu = np.triu(a, k=0)
a_tril = np.tril(a, k=0)
a_diag = np.diag(np.diag(a))
a_sym_triu = a_triu + a_triu.T - a_diag
matrix = pd.DataFrame(a_sym_triu,
index = tickers,
columns = tickers)
for mi in mis:
if abs(mi.v) > 10:
mi.v = 0.001
matrix.set_value(mi.i, mi.j, mi.v)
matrix.set_value(mi.j, mi.i, mi.v)
matrix = matrix.round(6)
return matrix
def test_preserve_trace_ground_state(self, dm):
dm.hadamard(2)
assert np.allclose(dm.trace(), 1)
dm.hadamard(4)
assert np.allclose(dm.trace(), 1)
dm.hadamard(0)
assert np.allclose(dm.trace(), 1)
# @pytest.mark.skip
# def test_squares_to_one(self, dm_random):
# dm = dm_random
# a0 = dm.to_array()
# dm.hadamard(4)
# dm.hadamard(4)
# # dm.hadamard(2)
# # dm.hadamard(2)
# # dm.hadamard(0)
# # dm.hadamard(0)
# a1 = dm.to_array()
# assert np.allclose(np.triu(a0), np.triu(a1))
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
def make_symmetric_lower(mat):
'''
Copies the matrix entries below the main diagonal to the upper triangle
half of the matrix. Leaves the diagonal unchanged. Returns a `NumPy` matrix
object.
**mat** : `numpy.matrix`
A lower diagonal matrix.
returns : `numpy.matrix`
The lower triangle matrix.
'''
# extract lower triangle from matrix (including diagonal)
tmp_mat = np.tril(mat)
# if the matrix given wasn't a lower triangle matrix, raise an error
if (mat != tmp_mat).all():
raise Exception('Matrix to symmetrize is not a lower diagonal matrix.')
# add its transpose to itself, zeroing the diagonal to avoid doubling
tmp_mat += np.triu(tmp_mat.transpose(), 1)
return np.asmatrix(tmp_mat)
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
def _init_topics_assignement(self):
dim = (self.J, self.J, 2)
alpha_0 = self.alpha_0
# Poisson way
#z = np.array( [poisson(alpha_0, size=dim) for dim in data_dims] )
# Random way
K = self.K_init
z = np.random.randint(0, K, (dim))
if self.likelihood._symmetric:
z[:, :, 0] = np.triu(z[:, :, 0]) + np.triu(z[:, :, 0], 1).T
z[:, :, 1] = np.triu(z[:, :, 1]) + np.triu(z[:, :, 1], 1).T
# LDA way
# improve local optima ?
#theta_j = dirichlet([1, gmma])
#todo ?
return z
def get_data_prop(self):
prop = super(frontendNetwork, self).get_data_prop()
if self.is_symmetric():
nnz = np.triu(self.data).sum()
else:
nnz = self.data.sum()
_nnz = self.data.sum(axis=1)
d = {'instances': self.data.shape[1],
'nnz': nnz,
'nnz_mean': _nnz.mean(),
'nnz_var': _nnz.var(),
'density': self.density(),
'diameter': self.diameter(),
'clustering_coef': self.clustering_coefficient(),
'modularity': self.modularity(),
'communities': self.clusters_len(),
'features': self.get_nfeat(),
'directed': not self.is_symmetric()
}
prop.update(d)
return prop
def setUp(self):
self.nwalkers = 100
self.ndim = 5
self.ntemp = 20
self.N = 1000
self.mean = np.zeros(self.ndim)
self.cov = 0.5 - np.random.rand(self.ndim ** 2) \
.reshape((self.ndim, self.ndim))
self.cov = np.triu(self.cov)
self.cov += self.cov.T - np.diag(self.cov.diagonal())
self.cov = np.dot(self.cov, self.cov)
self.icov = np.linalg.inv(self.cov)
self.p0 = [0.1 * np.random.randn(self.ndim)
for i in range(self.nwalkers)]
self.truth = np.random.multivariate_normal(self.mean, self.cov, 100000)
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
def verify_solve_grad(self, m, n, A_structure, lower, rng):
# ensure diagonal elements of A relatively large to avoid numerical
# precision issues
A_val = (rng.normal(size=(m, m)) * 0.5 +
numpy.eye(m)).astype(config.floatX)
if A_structure == 'lower_triangular':
A_val = numpy.tril(A_val)
elif A_structure == 'upper_triangular':
A_val = numpy.triu(A_val)
if n is None:
b_val = rng.normal(size=m).astype(config.floatX)
else:
b_val = rng.normal(size=(m, n)).astype(config.floatX)
eps = None
if config.floatX == "float64":
eps = 2e-8
solve_op = Solve(A_structure=A_structure, lower=lower)
utt.verify_grad(solve_op, [A_val, b_val], 3, rng, eps=eps)
def test_as_spin_response(self):
response = self.response_factory()
num_samples = 100
num_variables = 200
samples = np.triu(np.ones((num_samples, num_variables))) * 2 - 1
energies = np.zeros((num_samples,))
response.add_samples_from_array(samples, energies)
dimod_response = response.as_spin_response()
for s, t in zip(response, dimod_response):
self.assertEqual(s, t)
dimod_response = response.as_spin_response(data_copy=True)
for (__, dat), (__, dat0) in zip(response.samples(data=True),
dimod_response.samples(data=True)):
self.assertNotEqual(id(dat), id(dat0))
def test_as_binary_response(self):
response = self.response_factory()
num_samples = 100
num_variables = 200
samples = np.triu(np.ones((num_samples, num_variables)))
energies = np.zeros((num_samples,))
response.add_samples_from_array(samples, energies)
dimod_response = response.as_binary_response()
for s, t in zip(response, dimod_response):
self.assertEqual(s, t)
dimod_response = response.as_binary_response(data_copy=True)
for (__, dat), (__, dat0) in zip(response.samples(data=True),
dimod_response.samples(data=True)):
self.assertNotEqual(id(dat), id(dat0))
tangentspace.py 文件源码
项目:decoding_challenge_cortana_2016_3rd
作者: kingjr
项目源码
文件源码
阅读 40
收藏 0
点赞 0
评论 0
def tangent_space(covmats, Cref):
"""Project a set of covariance matrices in the tangent space according to the given reference point Cref
:param covmats: Covariance matrices set, Ntrials X Nchannels X Nchannels
:param Cref: The reference covariance matrix
:returns: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
"""
Nt, Ne, Ne = covmats.shape
Cm12 = invsqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
T = numpy.empty((Nt, Ne * (Ne + 1) / 2))
coeffs = (
numpy.sqrt(2) *
numpy.triu(
numpy.ones(
(Ne,
Ne)),
1) +
numpy.eye(Ne))[idx]
for index in range(Nt):
tmp = numpy.dot(numpy.dot(Cm12, covmats[index, :, :]), Cm12)
tmp = logm(tmp)
T[index, :] = numpy.multiply(coeffs, tmp[idx])
return T
tangentspace.py 文件源码
项目:decoding_challenge_cortana_2016_3rd
作者: kingjr
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def untangent_space(T, Cref):
"""Project a set of Tangent space vectors in the manifold according to the given reference point Cref
:param T: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2)
:param Cref: The reference covariance matrix
:returns: A set of Covariance matrix, Ntrials X Nchannels X Nchannels
"""
Nt, Nd = T.shape
Ne = int((numpy.sqrt(1 + 8 * Nd) - 1) / 2)
C12 = sqrtm(Cref)
idx = numpy.triu_indices_from(Cref)
covmats = numpy.empty((Nt, Ne, Ne))
covmats[:, idx[0], idx[1]] = T
for i in range(Nt):
covmats[i] = numpy.diag(numpy.diag(covmats[i])) + numpy.triu(
covmats[i], 1) / numpy.sqrt(2) + numpy.triu(covmats[i], 1).T / numpy.sqrt(2)
covmats[i] = expm(covmats[i])
covmats[i] = numpy.dot(numpy.dot(C12, covmats[i]), C12)
return covmats
def test_tril_triu_ndim3():
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
a = np.array([
[[1, 1], [1, 1]],
[[1, 1], [1, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_tril_desired = np.array([
[[1, 0], [1, 1]],
[[1, 0], [1, 0]],
[[1, 0], [0, 0]],
], dtype=dtype)
a_triu_desired = np.array([
[[1, 1], [0, 1]],
[[1, 1], [0, 0]],
[[1, 1], [0, 0]],
], dtype=dtype)
a_triu_observed = np.triu(a)
a_tril_observed = np.tril(a)
yield assert_array_equal, a_triu_observed, a_triu_desired
yield assert_array_equal, a_tril_observed, a_tril_desired
yield assert_equal, a_triu_observed.dtype, a.dtype
yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype():
# Issue 4916
# tril and triu should return the same dtype as input
for c in np.typecodes['All']:
if c == 'V':
continue
arr = np.zeros((3, 3), dtype=c)
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
# check special cases
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
['2004-01-01T12:00', '2003-01-03T13:45']],
dtype='datetime64')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)
arr = np.zeros((3,3), dtype='f4,f4')
assert_equal(np.triu(arr).dtype, arr.dtype)
assert_equal(np.tril(arr).dtype, arr.dtype)