mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-23 20:11:13 +00:00
get principal eigvec
This commit is contained in:
parent
5da40c30c0
commit
a908a343af
20
useful_scripts/principal_eigenvector.py
Normal file
20
useful_scripts/principal_eigenvector.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# Select a principal eigenvector via NumPy
|
||||||
|
# to be used as a template (copy & paste) script
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# set A to be your matrix
|
||||||
|
A = np.array([[1, 2, 3],
|
||||||
|
[4, 5, 6],
|
||||||
|
[7, 8, 9]])
|
||||||
|
|
||||||
|
|
||||||
|
eig_vals, eig_vecs = np.linalg.eig(A)
|
||||||
|
idx = np.absolute(eig_vals).argsort()[::-1] # decreasing order
|
||||||
|
sorted_eig_vals = eig_vals[idx]
|
||||||
|
sorted_eig_vecs = eig_vecs[:, idx]
|
||||||
|
|
||||||
|
principal_eig_vec = sorted_eig_vecs[:, 0] # eigvec with largest eigval
|
||||||
|
|
||||||
|
normalized_pr_eig_vec = np.real(principal_eig_vec / np.sum(principal_eig_vec))
|
||||||
|
print(normalized_pr_eig_vec) # eigvec that sums up to one
|
Loading…
Reference in New Issue
Block a user