本文整理匯總了Python中numpy.linalg.eig方法的典型用法代碼示例。如果您正苦於以下問題:Python linalg.eig方法的具體用法?Python linalg.eig怎麽用?Python linalg.eig使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.linalg
的用法示例。
在下文中一共展示了linalg.eig方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getPrincipalAxes
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def getPrincipalAxes(self):
X = self.VPos - self.getCentroid()
XTX = (X.T).dot(X)
(lambdas, axes) = linalg.eig(XTX)
#Put the eigenvalues in decreasing order
idx = lambdas.argsort()[::-1]
lambdas = lambdas[idx]
axes = axes[:, idx]
T = X.dot(axes)
maxProj = T.max(0)
minProj = T.min(0)
axes = axes.T #Put each axis on each row to be consistent with everything else
return (axes, maxProj, minProj)
#Delete the parts of the mesh below "plane". If fillHoles
#is true, plug up the holes that result from the cut
示例2: test_0_size
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def test_0_size(self):
# Check that all kinds of 0-sized arrays work
class ArraySubclass(np.ndarray):
pass
a = np.zeros((0, 1, 1), dtype=np.int_).view(ArraySubclass)
res, res_v = linalg.eig(a)
assert_(res_v.dtype.type is np.float64)
assert_(res.dtype.type is np.float64)
assert_equal(a.shape, res_v.shape)
assert_equal((0, 1), res.shape)
# This is just for documentation, it might make sense to change:
assert_(isinstance(a, np.ndarray))
a = np.zeros((0, 0), dtype=np.complex64).view(ArraySubclass)
res, res_v = linalg.eig(a)
assert_(res_v.dtype.type is np.complex64)
assert_(res.dtype.type is np.complex64)
assert_equal(a.shape, res_v.shape)
assert_equal((0,), res.shape)
# This is just for documentation, it might make sense to change:
assert_(isinstance(a, np.ndarray))
示例3: test_eig_build
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def test_eig_build(self):
# Ticket #652
rva = array([1.03221168e+02 + 0.j,
-1.91843603e+01 + 0.j,
-6.04004526e-01 + 15.84422474j,
-6.04004526e-01 - 15.84422474j,
-1.13692929e+01 + 0.j,
-6.57612485e-01 + 10.41755503j,
-6.57612485e-01 - 10.41755503j,
1.82126812e+01 + 0.j,
1.06011014e+01 + 0.j,
7.80732773e+00 + 0.j,
-7.65390898e-01 + 0.j,
1.51971555e-15 + 0.j,
-1.51308713e-15 + 0.j])
a = arange(13 * 13, dtype=float64)
a.shape = (13, 13)
a = a % 17
va, ve = linalg.eig(a)
va.sort()
rva.sort()
assert_array_almost_equal(va, rva)
示例4: do
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def do(self, a, b):
# note that eigenvalue arrays returned by eig must be sorted since
# their order isn't guaranteed.
ev, evc = linalg.eigh(a)
evalues, evectors = linalg.eig(a)
evalues.sort(axis=-1)
assert_almost_equal(ev, evalues)
assert_allclose(dot_generalized(a, evc),
np.asarray(ev)[..., None, :] * np.asarray(evc),
rtol=get_rtol(ev.dtype))
ev2, evc2 = linalg.eigh(a, 'U')
assert_almost_equal(ev2, evalues)
assert_allclose(dot_generalized(a, evc2),
np.asarray(ev2)[..., None, :] * np.asarray(evc2),
rtol=get_rtol(ev.dtype), err_msg=repr(a))
示例5: test_eig_build
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def test_eig_build(self, level = rlevel):
"""Ticket #652"""
rva = array([1.03221168e+02 +0.j,
-1.91843603e+01 +0.j,
-6.04004526e-01+15.84422474j,
-6.04004526e-01-15.84422474j,
-1.13692929e+01 +0.j,
-6.57612485e-01+10.41755503j,
-6.57612485e-01-10.41755503j,
1.82126812e+01 +0.j,
1.06011014e+01 +0.j,
7.80732773e+00 +0.j,
-7.65390898e-01 +0.j,
1.51971555e-15 +0.j,
-1.51308713e-15 +0.j])
a = arange(13*13, dtype = float64)
a.shape = (13, 13)
a = a%17
va, ve = linalg.eig(a)
va.sort()
rva.sort()
assert_array_almost_equal(va, rva)
示例6: spectral_radius
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def spectral_radius(self):
"""Compute the spectral radius of the matrix of l1 norm of Hawkes
kernels.
Notes
-----
If the spectral radius is greater that 1, the hawkes process is not
stable
"""
get_norm = np.vectorize(lambda kernel: kernel.get_norm())
norms = get_norm(self.kernels)
# It might happens that eig returns a complex number but with a
# negligible complex part, in this case we keep only the real part
spectral_radius = max(eig(norms)[0])
spectral_radius = np.real_if_close(spectral_radius)
return spectral_radius
示例7: lnc_correction
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def lnc_correction(tree, points, k, alpha):
e = 0
n_sample = points.shape[0]
for point in points:
# Find k-nearest neighbors in joint space, p=inf means max norm
knn = tree.query(point[None, :], k=k+1, return_distance=False)[0]
knn_points = points[knn]
# Substract mean of k-nearest neighbor points
knn_points = knn_points - knn_points[0]
# Calculate covariance matrix of k-nearest neighbor points, obtain eigen vectors
covr = knn_points.T @ knn_points / k
_, v = la.eig(covr)
# Calculate PCA-bounding box using eigen vectors
V_rect = np.log(np.abs(knn_points @ v).max(axis=0)).sum()
# Calculate the volume of original box
log_knn_dist = np.log(np.abs(knn_points).max(axis=0)).sum()
# Perform local non-uniformity checking and update correction term
if V_rect < log_knn_dist + np.log(alpha):
e += (log_knn_dist - V_rect) / n_sample
return e
# DISCRETE ESTIMATORS
示例8: _get_top_eigen_vectors
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def _get_top_eigen_vectors(data: ndarray, n_components: int) -> ndarray:
"""The eigen vectors according to top n_components large eigen values.
Arguments:
data {ndarray} -- Training data.
n_components {int} -- Number of components to keep.
Returns:
ndarray -- eigen vectors with shape(n_cols, n_components).
"""
# Calculate eigen values and eigen vectors of covariance matrix.
eigen_values, eigen_vectors = eig(data)
# The indexes of top n_components large eigen values.
_indexes = heapq.nlargest(n_components, enumerate(eigen_values),
key=lambda x: x[1])
indexes = [x[0] for x in _indexes]
return eigen_vectors[:, indexes]
示例9: test_eig_build
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def test_eig_build(self, level=rlevel):
# Ticket #652
rva = array([1.03221168e+02 + 0.j,
-1.91843603e+01 + 0.j,
-6.04004526e-01 + 15.84422474j,
-6.04004526e-01 - 15.84422474j,
-1.13692929e+01 + 0.j,
-6.57612485e-01 + 10.41755503j,
-6.57612485e-01 - 10.41755503j,
1.82126812e+01 + 0.j,
1.06011014e+01 + 0.j,
7.80732773e+00 + 0.j,
-7.65390898e-01 + 0.j,
1.51971555e-15 + 0.j,
-1.51308713e-15 + 0.j])
a = arange(13 * 13, dtype=float64)
a.shape = (13, 13)
a = a % 17
va, ve = linalg.eig(a)
va.sort()
rva.sort()
assert_array_almost_equal(va, rva)
示例10: _fit_ellipse_to_xy_points
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def _fit_ellipse_to_xy_points(x, y):
"""Fit an ellipse to a list of x and y points.
Parameters
----------
x, y : NumPy 2D array
Returns
-------
ellipse_parameters : NumPy array
"""
xx = x * x
yy = y * y
xy = x * y
ones = np.ones_like(x)
D = np.vstack((xx, xy, yy, x, y, ones))
S = np.dot(D, D.T)
C = np.zeros((6, 6))
C[0, 2], C[2, 0] = 2, 2
C[1, 1] = -1
A, B = la.eig(np.dot(la.inv(S), C))
i = np.argmax(np.abs(A))
g = B[:, i]
return g
示例11: do
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def do(self, a, b):
# note that eigenvalue arrays must be sorted since
# their order isn't guaranteed.
ev, evc = linalg.eigh(a)
evalues, evectors = linalg.eig(a)
ev.sort(axis=-1)
evalues.sort(axis=-1)
assert_almost_equal(ev, evalues)
assert_allclose(dot_generalized(a, evc),
np.asarray(ev)[...,None,:] * np.asarray(evc),
rtol=get_rtol(ev.dtype))
ev2, evc2 = linalg.eigh(a, 'U')
ev2.sort(axis=-1)
assert_almost_equal(ev2, evalues)
assert_allclose(dot_generalized(a, evc2),
np.asarray(ev2)[...,None,:] * np.asarray(evc2),
rtol=get_rtol(ev.dtype), err_msg=repr(a))
示例12: B2q
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def B2q(B, tol=None):
''' Estimate q vector from input B matrix `B`
We assume the input `B` is symmetric positive definite.
Because the solution is a square root, the sign of the returned
vector is arbitrary. We set the vector to have a positive x
component by convention.
Parameters
----------
B : (3,3) array-like
B matrix - symmetric. We do not check the symmetry.
tol : None or float
absolute tolerance below which to consider eigenvalues of the B
matrix to be small enough not to worry about them being negative,
in check for positive semi-definite-ness. None (default) results
in a fairly tight numerical threshold proportional the maximum
eigenvalue
Returns
-------
q : (3,) vector
Estimated q vector from B matrix `B`
'''
B = np.asarray(B)
w, v = npl.eig(B)
if tol is None:
tol = np.abs(w.max() * np.finfo(w.dtype).eps)
non_trivial = np.abs(w) > tol
if np.any(w[non_trivial] < 0):
raise ValueError('B not positive semi-definite')
inds = np.argsort(w)[::-1]
max_ind = inds[0]
vector = v[:,max_ind]
# because the factor is a sqrt, the sign of the vector is arbitrary.
# We arbitrarily set it to have a positive x value.
if vector[0] < 0:
vector *= -1
return vector * w[max_ind]
示例13: do
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def do(self, a, b, tags):
ev = linalg.eigvals(a)
evalues, evectors = linalg.eig(a)
assert_almost_equal(ev, evalues)
示例14: test_types
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def test_types(self, dtype):
x = np.array([[1, 0.5], [0.5, 1]], dtype=dtype)
w, v = np.linalg.eig(x)
assert_equal(w.dtype, dtype)
assert_equal(v.dtype, dtype)
x = np.array([[1, 0.5], [-1, 1]], dtype=dtype)
w, v = np.linalg.eig(x)
assert_equal(w.dtype, get_complex_dtype(dtype))
assert_equal(v.dtype, get_complex_dtype(dtype))
示例15: solve_spectral
# 需要導入模塊: from numpy import linalg [as 別名]
# 或者: from numpy.linalg import eig [as 別名]
def solve_spectral(prob, *args, **kwargs):
"""Solve the spectral relaxation with lambda = 1.
"""
# TODO: do this efficiently without SDP lifting
# lifted variables and semidefinite constraint
X = cvx.Semidef(prob.n + 1)
W = prob.f0.homogeneous_form()
rel_obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(W, X)))
W1 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '<='])
W2 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '=='])
rel_prob = cvx.Problem(
rel_obj,
[
cvx.sum_entries(cvx.mul_elemwise(W1, X)) <= 0,
cvx.sum_entries(cvx.mul_elemwise(W2, X)) == 0,
X[-1, -1] == 1
]
)
rel_prob.solve(*args, **kwargs)
if rel_prob.status not in [cvx.OPTIMAL, cvx.OPTIMAL_INACCURATE]:
raise Exception("Relaxation problem status: %s" % rel_prob.status)
(w, v) = LA.eig(X.value)
return np.sqrt(np.max(w))*np.asarray(v[:-1, np.argmax(w)]).flatten(), rel_prob.value