Python numpy.linalg.eig() Examples
The following are 30
code examples of numpy.linalg.eig().
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 also want to check out all available functions/classes of the module
numpy.linalg
, or try the search function
.
Example #1
Source File: pca.py From imylu with Apache License 2.0 | 6 votes |
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]
Example #2
Source File: test_linalg.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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))
Example #3
Source File: test_linalg.py From recruit with Apache License 2.0 | 6 votes |
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))
Example #4
Source File: test_linalg.py From ImageFusion with MIT License | 6 votes |
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))
Example #5
Source File: PolyMesh.py From laplacian-meshes with GNU General Public License v3.0 | 6 votes |
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
Example #6
Source File: test_regression.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: simu_hawkes.py From tick with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #8
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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))
Example #9
Source File: test_regression.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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)
Example #10
Source File: radial_utils.py From pyxem with GNU General Public License v3.0 | 6 votes |
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
Example #11
Source File: test_regression.py From mxnet-lambda with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: test_linalg.py From lambda-packs with MIT License | 6 votes |
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))
Example #13
Source File: test_linalg.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
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))
Example #14
Source File: test_regression.py From GraphicDesignPatternByPython with MIT License | 6 votes |
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)
Example #15
Source File: test_regression.py From Computable with MIT License | 6 votes |
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)
Example #16
Source File: test_regression.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #17
Source File: test_linalg.py From pySINDy with MIT License | 6 votes |
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))
Example #18
Source File: test_linalg.py From vnpy_crypto with MIT License | 6 votes |
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))
Example #19
Source File: entropy_estimators.py From NPEET with MIT License | 6 votes |
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
Example #20
Source File: test_regression.py From pySINDy with MIT License | 6 votes |
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)
Example #21
Source File: test_linalg.py From auto-alt-text-lambda-api with MIT License | 6 votes |
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))
Example #22
Source File: test_regression.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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)
Example #23
Source File: test_linalg.py From mxnet-lambda with Apache License 2.0 | 6 votes |
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))
Example #24
Source File: test_linalg.py From pySINDy with MIT License | 5 votes |
def do(self, a, b, tags): # note that eigenvalue arrays returned by eig must be sorted since # their order isn't guaranteed. ev = linalg.eigvalsh(a, 'L') evalues, evectors = linalg.eig(a) evalues.sort(axis=-1) assert_allclose(ev, evalues, rtol=get_rtol(ev.dtype)) ev2 = linalg.eigvalsh(a, 'U') assert_allclose(ev2, evalues, rtol=get_rtol(ev.dtype))
Example #25
Source File: ergodic.py From nelpy with MIT License | 5 votes |
def steady_state(P): """ Calculates the steady state probability vector for a regular Markov transition matrix P Parameters ---------- P : matrix (kxk) an ergodic Markov transition probability matrix Returns ------- implicit : matrix (kx1) steady state distribution Examples -------- Taken from Kemeny and Snell. [1]_ Land of Oz example where the states are Rain, Nice and Snow - so there is 25 percent chance that if it rained in Oz today, it will snow tomorrow, while if it snowed today in Oz there is a 50 percent chance of snow again tomorrow and a 25 percent chance of a nice day (nice, like when the witch with the monkeys is melting). >>> import numpy as np >>> p=np.matrix([[.5, .25, .25],[.5,0,.5],[.25,.25,.5]]) >>> steady_state(p) matrix([[ 0.4], [ 0.2], [ 0.4]]) Thus, the long run distribution for Oz is to have 40 percent of the days classified as Rain, 20 percent as Nice, and 40 percent as Snow (states are mutually exclusive). """ v,d=la.eig(np.transpose(P)) # for a regular P maximum eigenvalue will be 1 mv=max(v) # find its position i=v.tolist().index(mv) # normalize eigenvector corresponding to the eigenvalue 1 return d[:,i]/sum(d[:,i])
Example #26
Source File: epipolar_geometry.py From py3DRec with MIT License | 5 votes |
def get_epipole(self,F): ''' Method to return the epipole from the fundamental matrix Args: F: the fundamental matrix Retruns: the epipole ''' u,v=eig(F) dd=np.square(u); min_index=np.argmin(np.abs(dd)) # SOS: These are complex numbers, so compare their modulus epipole=v[:,min_index] epipole=epipole/epipole[2] return epipole.real
Example #27
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def do(self, a, b, tags): # note that eigenvalue arrays returned by eig must be sorted since # their order isn't guaranteed. ev = linalg.eigvalsh(a, 'L') evalues, evectors = linalg.eig(a) evalues.sort(axis=-1) assert_allclose(ev, evalues, rtol=get_rtol(ev.dtype)) ev2 = linalg.eigvalsh(a, 'U') assert_allclose(ev2, evalues, rtol=get_rtol(ev.dtype))
Example #28
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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))
Example #29
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def do(self, a, b, tags): ev = linalg.eigvals(a) evalues, evectors = linalg.eig(a) assert_almost_equal(ev, evalues)
Example #30
Source File: test_linalg.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def do(self, a, b, tags): evalues, evectors = linalg.eig(a) assert_allclose(dot_generalized(a, evectors), np.asarray(evectors) * np.asarray(evalues)[..., None, :], rtol=get_rtol(evalues.dtype)) assert_(consistent_subclass(evectors, a))