0

I am using numpy to generate eigenvectors and eigenvalues. A problem arises when forming tuples of them, and attempting to sort the pairs. I get the error message: TypeError: only length-1 arrays can be converted to python scalars.

Here is the code:

import numpy as np
import pandas as pd


df=\
pd.read_csv(r'C:\Users\james\Desktop\Documents\Downloads\bpAFTPosWords.csv'
#df.head()

#Drop columns whose sum is less than 30
df.sum(axis = 0, skipna = True)
df_to_save = df.loc[:, (df.sum(axis=0, skipna=True) >= 30)]
#df_to_save.head()

#Standardize the data
from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(df_to_save)

#Compute correlations
cor_mat1 = np.corrcoef(X_std.T)
#Produce PCA eigenvector and eigenvalues
eig_vals, eig_vecs = np.linalg.eig(cor_mat1)

#print('Eigenvectors \n%s' %eig_vecs)
#print('\nEigenvalues \n%s' %eig_vals)

# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = np.array(list(zip(eig_vals,eig_vecs)))
eig_pairs = eig_pairs[
        eig_pairs[:,0].argsort()[::-1]]

# Visually confirm that the list is correctly sorted by decreasing
print('Eigenvalues in descending order:')
for i in eig_pairs:
print(i[0])

#Here is the context for the error:
TypeError Traceback (most recent call last)
<ipython-input-7-2342d13b7710> in <module>
21
22 # Make a list of (eigenvalue, eigenvector) tuples
---> 23 eig_pairs = np.array(list(zip(eig_vals,eig_vecs)))
24
25 eig_pairs = eig_pairs[
TypeError: only length-1 arrays can be converted to Python scalars

In case my data would aid your problem-solving, here is the .csv file:

https://docs.google.com/spreadsheets/d/1GRPbfHHB1mbO5Eo26B6crTl7FN1cNnLoU-oRQCEu7v8/edit?usp=sharing

A second question I have is how output to a file the loadings of each row on each eigenvector. Have not yet been able to figure this out from googling and documentation.

Thanks for your help!

1 Answers1

0

I couldn't reproduce your error, but here's a solution based on numpy's sort.

import numpy as np

X_std = np.random.random((8,8))

cor_mat1 = np.corrcoef(X_std.T)
eig_vals, eig_vecs = np.linalg.eig(cor_mat1)

print('Eigenvectors \n%s' %eig_vecs)
print('\nEigenvalues \n%s' %eig_vals)

# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = np.array(list(zip(eig_vals,eig_vecs)))

eig_pairs = eig_pairs[
        eig_pairs[:,0].argsort()[::-1]
                 ]

# Visually confirm that the list is correctly sorted by decreasing 
print('Eigenvalues in descending order:')
for i in eig_pairs:
    print(i[0])

Here you can read about zip function and here about argsort

Hope that helps.

Arkady. A
  • 545
  • 3
  • 9
  • Thanks for your help. Your code with the random data works, but when I try to use my data, I get an error. The problem description above is edited to include a link to my data, and my code. Hoping you can debug. Thank you! – James Danowski May 19 '19 at 20:13