From a908a343afe1bd0eff420b15d93706dde9c123ea Mon Sep 17 00:00:00 2001 From: rasbt Date: Wed, 16 May 2018 01:06:58 -0400 Subject: [PATCH] get principal eigvec --- useful_scripts/principal_eigenvector.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 useful_scripts/principal_eigenvector.py diff --git a/useful_scripts/principal_eigenvector.py b/useful_scripts/principal_eigenvector.py new file mode 100644 index 0000000..913cf62 --- /dev/null +++ b/useful_scripts/principal_eigenvector.py @@ -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