0
votes

I have a matrix of ternary values (2 observations, 11 variables) for which I calculate the eigenvectors using np.linalg.eig() from Numpy. The matrix is (0 values are not used for this example):

v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11
1  1  1  1  1  1  1  1  1  -1  -1
1  1  1  1  1  1  1  1  1  -1  -1

Result of the eigenvector from largest eigenvalue:

[ 0.33333333  0.          0.33333333  0.          0.33333333  0.33333333
0.33333333  0.33333333  0.33333333  0.33333333  0.33333333]

I am not sure about the order of these coefficients. Are they following the order of the variables expressed in the matrix (i.e. first 0.33333333 is weight coefficient of v1, 0.0 is weight coefficient of v2, etc...)?

Last part of my code is:

# Matrix with rounded values
Mtx = np.matrix.round(Mtx,3)

# Cross product of Mtx
Mtx_CrossProduct = (Mtx.T).dot(Mtx)

# Calculation of eigenvectors               
eigen_Value, eigen_Vector = np.linalg.eig(Mtx_CrossProduct)
eigen_Vector = np.absolute(eigen_Vector)

# Listing (eigenvalue, eigenvector) and sorting of eigenvalues to get PC1
eig_pairs = [(np.absolute(eigen_Value[i]), eigen_Vector[i,:]) for i in range(len(eigen_Value))]
eig_pairs.sort(key=lambda tup: tup[0],reverse=True)

# Getting largest eigenvector
eig_Vector_Main = np.zeros((11,))
for i in range(len(eig_pairs)):
    eig_Vector_Main[i] = eig_pairs[i][1][0]
1
Your example doesn't make sense, np.linalg.eig works on square matrices.Julien
I am using cross-product matrix.JrCaspian

1 Answers

2
votes

The dimensions of each vector are the same as the dimensions of your original matrix (i.e. they follow the order as you say).

I've not figured out exactly what you're doing with your lambda and 'standard' python list but you can probably do the same thing more elegantly and quickly by sticking to numpy i.e.

eigen_Value, eigen_Vector = np.linalg.eig(Mtx_CrossProduct)
eigen_Vector = np.absolute(eigen_Vector)
ix = np.argsort(eigen_Value)[::-1] # reverse sorted index
eig_Vector_Main = eigen_Vector[ix]